Skip to content

Commit 6275739

Browse files
mcollinaaduh95
authored andcommitted
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> PR-URL: #67503 Reviewed-By: Philipp Dunkel <pip@pipobscure.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b3c164d commit 6275739

11 files changed

Lines changed: 336 additions & 0 deletions

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,77 @@ 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+
<!-- eslint-disable no-restricted-syntax, @stylistic/js/spaced-comment -->
395+
396+
```cjs
397+
// sea.js
398+
'use strict';
399+
400+
function boom() {
401+
throw new Error('boom from the original source');
402+
}
403+
404+
boom();
405+
//# sourceMappingURL=sea.js.map
406+
```
407+
408+
If the map embeds `sourcesContent`, the original source lines are taken from
409+
the map itself. Otherwise the original sources must also be bundled (as
410+
assets, or as entries in the `"vfsArchive"` archive) so that the error source
411+
preview can be read back through the VFS, as in the configuration above. With
412+
`--enable-source-maps` enabled, throwing the error above maps the stack frame
413+
back to `original.js` and shows the original source line:
414+
415+
```text
416+
…/vfs/1/original.js:5
417+
throw new Error('boom from the original source');
418+
^
419+
420+
Error: boom from the original source
421+
at boom (…/vfs/1/original.js:5:5)
422+
```
423+
424+
The `…` stands in for the reserved mount point, which is chosen at runtime and
425+
cannot exist on the real file system. When the application is transpiled, the
426+
bundler typically emits the map next to the bundled file; make sure to bundle
427+
it with `"assets"` (or include it in the `"vfsArchive"` archive), because the
428+
VFS never shadows the real file system and the `.map` file must therefore be
429+
reachable inside the mount.
430+
360431
#### Snapshot limitations
361432
362433
`"useVfs": true` cannot be used together with `"useSnapshot": true`.
@@ -844,11 +915,15 @@ to help us document them.
844915
[Generating single executable preparation blobs]: #1-generating-single-executable-preparation-blobs
845916
[Mach-O]: https://en.wikipedia.org/wiki/Mach-O
846917
[PE]: https://en.wikipedia.org/wiki/Portable_Executable
918+
[Source Map v3]: https://tc39.es/ecma426/
847919
[Using native addons in the injected main script]: #using-native-addons-in-the-injected-main-script
848920
[VFS documentation]: vfs.md
849921
[Windows SDK]: https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/
922+
[`--enable-source-maps`]: cli.md#--enable-source-maps
850923
[`--vfs-load`]: cli.md#--vfs-loadsource
924+
[`NODE_OPTIONS`]: cli.md#node_optionsoptions
851925
[`Worker`]: worker_threads.md#class-worker
926+
[`module.findSourceMap()`]: module.md#modulefindsourcemappath
852927
[`node:zlib`]: zlib.md
853928
[`process.execPath`]: process.md#processexecpath
854929
[`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)