Skip to content

Commit fc04b57

Browse files
committed
doc: require --vfs-load in a worker's execArgv
The worker paragraph for --vfs-load said only that a worker created with its own execArgv "has to be given --vfs-load again", which reads as a remark rather than as the requirement it is: such a worker inherits none of the parent's options, so it does not mount the source at all, and a script in the mount cannot be its entry point. Say that a worker given its own execArgv must carry the same --vfs-load to run a script from the mount, and that a worker whose script comes from elsewhere needs nothing added. Cover the requirement with a test: the same worker that runs with the flag fails to load without it. Signed-off-by: Philipp Dunkel <pip@pipobscure.com>
1 parent 0abd2c8 commit fc04b57

3 files changed

Lines changed: 42 additions & 8 deletions

File tree

‎doc/api/cli.md‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3822,10 +3822,16 @@ provider claims the source, Node.js exits with an error.
38223822
In worker threads `--vfs-load` mounts but does not load: a worker inherits the
38233823
mount and runs its own entry point, which may itself live in the mount.
38243824

3825-
The source is mounted at the same reserved mount point in every thread,
3826-
whatever else that thread mounts, so a path into it stays valid in a worker -
3827-
including one created with its own `execArgv`, which does not inherit the
3828-
parent's options and has to be given `--vfs-load` again.
3825+
The source is mounted at the same reserved mount point in every thread that
3826+
mounts it, whatever else that thread mounts, so a path into the mount means the
3827+
same thing in all of them.
3828+
3829+
A worker created with its own `execArgv` inherits none of the parent's options,
3830+
and so does not mount the source at all. Such a worker must be given the same
3831+
`--vfs-load` to run a script from the mount; without it, that thread has no
3832+
mount for the script to come from, and the worker fails to load it. A worker
3833+
whose script comes from anywhere else, such as the real file system, needs
3834+
nothing added.
38293835

38303836
`--vfs-load` is not permitted in [`NODE_OPTIONS`][]: which entry point runs is
38313837
the command line's decision, and the environment must not be able to redirect

‎doc/node.1‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,10 +1913,15 @@ reverse registration order, and may claim directories as well as files. If no
19131913
provider claims the source, Node.js exits with an error.
19141914
In worker threads \fB--vfs-load\fR mounts but does not load: a worker inherits the
19151915
mount and runs its own entry point, which may itself live in the mount.
1916-
The source is mounted at the same reserved mount point in every thread,
1917-
whatever else that thread mounts, so a path into it stays valid in a worker -
1918-
including one created with its own \fBexecArgv\fR, which does not inherit the
1919-
parent's options and has to be given \fB--vfs-load\fR again.
1916+
The source is mounted at the same reserved mount point in every thread that
1917+
mounts it, whatever else that thread mounts, so a path into the mount means the
1918+
same thing in all of them.
1919+
A worker created with its own \fBexecArgv\fR inherits none of the parent's options,
1920+
and so does not mount the source at all. Such a worker must be given the same
1921+
\fB--vfs-load\fR to run a script from the mount; without it, that thread has no
1922+
mount for the script to come from, and the worker fails to load it. A worker
1923+
whose script comes from anywhere else, such as the real file system, needs
1924+
nothing added.
19201925
\fB--vfs-load\fR is not permitted in \fBNODE_OPTIONS\fR: which entry point runs is
19211926
the command line's decision, and the environment must not be able to redirect
19221927
it.

‎test/parallel/test-vfs-load.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,29 @@ require('node:vfs').create().mount();
227227
assert.strictEqual(seen.size, 1, [...seen].join());
228228
}
229229

230+
// Without that flag the worker has no mount to load from, so a script in the
231+
// mount cannot be its entry point.
232+
{
233+
const dir = fixture('worker-execargv-missing');
234+
fs.mkdirSync(dir, { recursive: true });
235+
fs.writeFileSync(path.join(dir, 'index.js'), `
236+
'use strict';
237+
const path = require('path');
238+
const { Worker } = require('worker_threads');
239+
const w = new Worker(path.join(__dirname, 'worker.js'), { execArgv: [] });
240+
w.on('message', (m) => { console.log('ran:' + m); process.exit(0); });
241+
w.on('error', (e) => { console.log('failed:' + e.code); process.exit(0); });
242+
`);
243+
fs.writeFileSync(path.join(dir, 'worker.js'), `
244+
'use strict';
245+
require('worker_threads').parentPort.postMessage('unexpected');
246+
`);
247+
const res = run([`--vfs-load=${dir}`]);
248+
assert.strictEqual(res.status, 0, res.stderr);
249+
assert.match(res.stdout, /failed:/);
250+
assert.doesNotMatch(res.stdout, /ran:/);
251+
}
252+
230253
// --vfs-load names the source it loads, so it always takes a value.
231254
{
232255
const res = run(['--vfs-load']);

0 commit comments

Comments
 (0)