Skip to content

Commit 293eb86

Browse files
committed
sea: add source maps for SEA with VFS
SEA binaries built with "useVfs" can now load source maps from the bundled assets (or the "vfsArchive" archive) and apply them to stack traces and error output, because fs reads are routed through the virtual file system mounted by the SEA. Add two tests covering the inline-assets and zip-archive VFS paths, each asserting that findSourceMap() resolves against the mount point and that a thrown error's frame and source line are remapped to the original source. Also document how to enable and bundle source maps in a SEA. Signed-off-by: Matteo Collina <matteo.collina@gmail.com>
1 parent ab476ec commit 293eb86

11 files changed

Lines changed: 334 additions & 0 deletions

‎doc/api/single-executable-applications.md‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,75 @@ against its source, so it is used by the executable whenever the modules are
357357
loaded from the VFS, and ignored if they changed. The cache is
358358
platform-specific, like the code cache of the main script.
359359
360+
#### Source maps in the VFS
361+
362+
[Source Map v3][] maps work in a SEA using `"useVfs": true` exactly as they do
363+
on the real file system, provided the maps are bundled as assets (or inside
364+
the `"vfsArchive"` archive) and source map support is enabled in the generated
365+
executable. The `.map` file is read from inside the mount point, and the
366+
`"sources"` of the map and the original sources are resolved against the mount
367+
point too. As a result, [`module.findSourceMap()`][], source-mapped stack
368+
traces, and the source line shown above an uncaught error all resolve against
369+
the bundled assets rather than the real file system.
370+
371+
Source maps are enabled in a SEA the same way as in a regular Node.js process
372+
with [`--enable-source-maps`][]. The flag is not consumed when it is
373+
passed as a command-line argument to the executable: with the default
374+
`"execArgvExtension": "env"`, command-line arguments are handed to the
375+
application. Set it in `"execArgv"` (or in [`NODE_OPTIONS`][]) instead:
376+
377+
```json
378+
{
379+
"main": "sea.js",
380+
"output": "sea",
381+
"useVfs": true,
382+
"execArgv": ["--enable-source-maps"],
383+
"assets": {
384+
"sea.js.map": "dist/sea.js.map",
385+
"original.js": "src/original.js"
386+
}
387+
}
388+
```
389+
390+
The bundled source must end with a `sourceMappingURL` magic comment that points
391+
at the map. A relative path resolves against the location of the script inside
392+
the mount, so no mount-point path needs to be hard-coded:
393+
394+
```cjs
395+
// sea.js
396+
'use strict';
397+
398+
function boom() {
399+
throw new Error('boom from the original source');
400+
}
401+
402+
boom();
403+
//# sourceMappingURL=sea.js.map
404+
```
405+
406+
If the map embeds `sourcesContent`, the original source lines are taken from
407+
the map itself. Otherwise the original sources must also be bundled (as
408+
assets, or as entries in the `"vfsArchive"` archive) so that the error source
409+
preview can be read back through the VFS, as in the configuration above. With
410+
`--enable-source-maps` enabled, throwing the error above maps the stack frame
411+
back to `original.js` and shows the original source line:
412+
413+
```text
414+
…/vfs/1/original.js:5
415+
throw new Error('boom from the original source');
416+
^
417+
418+
Error: boom from the original source
419+
at boom (…/vfs/1/original.js:5:5)
420+
```
421+
422+
The `…` stands in for the reserved mount point, which is chosen at runtime and
423+
cannot exist on the real file system. When the application is transpiled, the
424+
bundler typically emits the map next to the bundled file; make sure to bundle
425+
it with `"assets"` (or include it in the `"vfsArchive"` archive), because the
426+
VFS never shadows the real file system and the `.map` file must therefore be
427+
reachable inside the mount.
428+
360429
#### Snapshot limitations
361430
362431
`"useVfs": true` cannot be used together with `"useSnapshot": true`.
@@ -844,11 +913,15 @@ to help us document them.
844913
[Generating single executable preparation blobs]: #1-generating-single-executable-preparation-blobs
845914
[Mach-O]: https://en.wikipedia.org/wiki/Mach-O
846915
[PE]: https://en.wikipedia.org/wiki/Portable_Executable
916+
[Source Map v3]: https://tc39.es/ecma426/
847917
[Using native addons in the injected main script]: #using-native-addons-in-the-injected-main-script
848918
[VFS documentation]: vfs.md
849919
[Windows SDK]: https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/
920+
[`--enable-source-maps`]: cli.md#--enable-source-maps
850921
[`--vfs-load`]: cli.md#--vfs-loadsource
922+
[`NODE_OPTIONS`]: cli.md#node_optionsoptions
851923
[`Worker`]: worker_threads.md#class-worker
924+
[`module.findSourceMap()`]: module.md#modulefindsourcemappath
852925
[`node:zlib`]: zlib.md
853926
[`process.execPath`]: process.md#processexecpath
854927
[`require()`]: modules.md#requireid
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { findSourceMap } = require('module');
5+
6+
const sm = findSourceMap(__filename);
7+
assert.ok(sm, 'source map should be loaded from the SEA VFS');
8+
console.log('source map loaded from the SEA VFS:', sm.payload.sources[0]);
9+
10+
function boom() {
11+
throw new Error('boom from the original source');
12+
}
13+
14+
boom();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"main": "sea.js",
3+
"output": "sea-prep.blob",
4+
"useVfs": true,
5+
"execArgv": ["--enable-source-maps"],
6+
"assets": {
7+
"sea.js.map": "sea.js.map",
8+
"original.js": "original.js"
9+
}
10+
}

‎test/fixtures/sea/vfs-sourcemap/sea.js‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/sea/vfs-sourcemap/sea.js.map‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { findSourceMap } = require('module');
5+
6+
const sm = findSourceMap(__filename);
7+
assert.ok(sm, 'source map should be loaded from the SEA VFS');
8+
console.log('source map loaded from the SEA VFS:', sm.payload.sources[0]);
9+
10+
function boom() {
11+
throw new Error('boom from the original source');
12+
}
13+
14+
boom();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"main": "sea.js",
3+
"output": "sea-prep.blob",
4+
"useVfs": true,
5+
"vfsArchive": "assets.zip",
6+
"execArgv": ["--enable-source-maps"]
7+
}

‎test/fixtures/sea/vfs-zip-sourcemap/sea.js‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/sea/vfs-zip-sourcemap/sea.js.map‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
// This tests that source maps are correctly loaded from a Single Executable
4+
// Application when "useVfs" is enabled. The `.map` file and the original
5+
// sources are bundled as `"assets"` and served from inside the read-only
6+
// virtual file system mounted by the SEA, so `findSourceMap()` and the
7+
// source-mapped stack trace must resolve against the mount point instead of
8+
// the real file system.
9+
//
10+
// The fixture (`test/fixtures/sea/vfs-sourcemap/`) enables source maps through
11+
// `execArgv` (`--enable-source-maps`), verifies that `module.findSourceMap()`
12+
// finds the bundled map, and then throws an error. This test asserts that:
13+
// 1. the mapped stack trace for the throw frame references the ORIGINAL
14+
// source (`original.js`) rather than the generated `sea.js`, and
15+
// 2. the original source line shown above the error is read back from the
16+
// VFS (it carries the original 4-space indentation, not the 2-space
17+
// indentation of the generated `sea.js`).
18+
//
19+
// The fixture's `sea.js.map` is a source-map-v3 identity mapping that is tied
20+
// to the current content of `sea.js` and `original.js`: most lines are mapped
21+
// one-to-one, and the `throw` line maps its column from 2 (the generated
22+
// 2-space indentation) to 4 (the original 4-space indentation). If either
23+
// fixture source is edited, regenerate the map so the columns stay in sync.
24+
25+
require('../common');
26+
27+
const {
28+
buildSEA,
29+
skipIfBuildSEAIsNotSupported,
30+
} = require('../common/sea');
31+
32+
skipIfBuildSEAIsNotSupported();
33+
34+
const tmpdir = require('../common/tmpdir');
35+
const { spawnSyncAndAssert } = require('../common/child_process');
36+
const fixtures = require('../common/fixtures');
37+
38+
tmpdir.refresh();
39+
const outputFile = buildSEA(fixtures.path('sea', 'vfs-sourcemap'));
40+
41+
spawnSyncAndAssert(
42+
outputFile,
43+
{
44+
env: {
45+
...process.env,
46+
NODE_DEBUG_NATIVE: undefined,
47+
},
48+
},
49+
{
50+
// The SEA main script throws an uncaught error to produce the
51+
// source-mapped stack trace.
52+
status: 1,
53+
stdout: /source map loaded from the SEA VFS: .*\/original\.js/,
54+
stderr(stderr) {
55+
// The throw frame must be remapped to the original source bundled in
56+
// the VFS. Its column (5) matches the original 4-space indentation.
57+
if (!/at boom \(.*\/original\.js:\d+:\d+\)/.test(stderr)) {
58+
throw new Error(`the boom() frame was not source-mapped:\n${stderr}`);
59+
}
60+
// It must not still point at the generated sea.js.
61+
if (/at boom \(.*\/sea\.js:\d+:\d+\)/.test(stderr)) {
62+
throw new Error(`the boom() frame was not remapped to original.js:\n${stderr}`);
63+
}
64+
// The original source line is read from the VFS, so it shows the
65+
// original 4-space indentation (the generated sea.js uses 2 spaces).
66+
if (!/^ {4}throw new Error\('boom from the original source'\);$/m.test(stderr)) {
67+
throw new Error(`the original source line was not read from the VFS:\n${stderr}`);
68+
}
69+
},
70+
},
71+
);

0 commit comments

Comments
 (0)