Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,14 @@ function executeUserEntryPoint(main = process.argv[1]) {
wrapModuleLoad(main, null, true);
} else {
const mainPath = resolvedMain || main;
const mainURL = getOptionValue('--entry-url') ? new URL(mainPath, getCWDURL()) : pathToFileURL(mainPath);
let mainURL = getOptionValue('--entry-url') ? new URL(mainPath, getCWDURL()) : pathToFileURL(mainPath);

runEntryPointWithESMLoader((cascadedLoader) => {
// A worker's inherited --vfs-load source is only mounted once --import
// has run, so an entry point inside it can only be resolved now.
if (resolvedMain === undefined && getOptionValue('--vfs-load') !== '' && !getOptionValue('--entry-url')) {
mainURL = pathToFileURL(resolveMainPath(main) || main);
}
// Note that if the graph contains unsettled TLA, this may never resolve
// even after the event loop stops running.
return cascadedLoader.import(mainURL, undefined, { __proto__: null }, undefined, true);
Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-vfs-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ require('worker_threads').parentPort.postMessage('hello from worker in mount');
assert.match(res.stdout, /hello from worker in mount/);
}

// The same with an --import preload, which defers the worker's mount until the
// preload has run: its entry point is still resolved inside the mount, with the
// extension search a file entry point gets.
{
const dir = fixture('worker-import-app');
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(path.join(dir, 'index.js'), `
'use strict';
const path = require('path');
const { Worker } = require('worker_threads');
const w = new Worker(path.join(__dirname, 'worker'));
w.on('message', (m) => { console.log(m); process.exit(0); });
w.on('error', (e) => { console.error(e); process.exit(1); });
`);
fs.writeFileSync(path.join(dir, 'worker.js'), `
'use strict';
require('worker_threads').parentPort.postMessage('hello from worker in mount');
`);
const preload = fixture('empty-preload.mjs');
fs.writeFileSync(preload, '');
const res = run(['--import', pathToFileURL(preload).href, `--vfs-load=${dir}`]);
assert.strictEqual(res.status, 0, res.stderr);
assert.match(res.stdout, /hello from worker in mount/);
}

// The same, for a worker whose nearest package.json inside the mount says
// "module": the entry point's type is read from the mount rather than from
// the real file system above the reserved mount point.
Expand Down
Loading