Skip to content

Commit

Permalink
lib: send the argv to the ESM worker thread loader
Browse files Browse the repository at this point in the history
When the `--conditions` is sent to a worker thread, it should affect
the worker thread with the ESM loader on it. This patch sends the
`execArgv` to the ESM worker.

Signed-off-by: Juan José Arboleda <[email protected]>
Fixes: #50885
  • Loading branch information
juanarbol committed Nov 26, 2023
1 parent ed5cb37 commit 95155c4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,17 @@ class HooksProxy {

#isReady = false;

constructor() {
constructor(execArgv) {
const { InternalWorker } = require('internal/worker');
MessageChannel ??= require('internal/worker/io').MessageChannel;

const lock = new SharedArrayBuffer(SHARED_MEMORY_BYTE_LENGTH);
this.#lock = new Int32Array(lock);

// This needs to inherits the parent's argc/argv
// See: https://github.com/nodejs/node/issues/50885
this.#worker = new InternalWorker(loaderWorkerId, {
execArgv,
stderr: false,
stdin: false,
stdout: false,
Expand Down
13 changes: 8 additions & 5 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@ class ModuleLoader {
// `CustomizedModuleLoader` is defined at the bottom of this file and
// available well before this line is ever invoked. This is here in
// order to preserve the git diff instead of moving the class.

// Send the `process.execArgv`
// See: https://github.com/nodejs/node/issues/50885
// eslint-disable-next-line no-use-before-define
this.setCustomizations(new CustomizedModuleLoader());
this.setCustomizations(new CustomizedModuleLoader(process.execArgv));
}
return this.#customizations.register(`${specifier}`, `${parentURL}`, data, transferList);
}
Expand Down Expand Up @@ -460,8 +463,8 @@ class CustomizedModuleLoader {
/**
* Instantiate a module loader that uses user-provided custom loader hooks.
*/
constructor() {
getHooksProxy();
constructor(execArgv) {
getHooksProxy(execArgv);
}

/**
Expand Down Expand Up @@ -565,10 +568,10 @@ function createModuleLoader() {
* Get the HooksProxy instance. If it is not defined, then create a new one.
* @returns {HooksProxy}
*/
function getHooksProxy() {
function getHooksProxy(execArgv) {
if (!hooksProxy) {
const { HooksProxy } = require('internal/modules/esm/hooks');
hooksProxy = new HooksProxy();
hooksProxy = new HooksProxy(execArgv);
}

return hooksProxy;
Expand Down
1 change: 1 addition & 0 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class Worker extends EventEmitter {
setupPortReferencing(this[kPublicPort], this, 'message');
this[kPort].postMessage({
argv,
execArgv: options.execArgv,
type: messageTypes.LOAD_SCRIPT,
filename,
doEval,
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-worker-execargv.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ if (!process.env.HAS_STARTED_WORKER) {
);
}));

// Refs: https://github.com/nodejs/node/issues/50885
new Worker(
"require('worker_threads').parentPort.postMessage(process.execArgv)",
{ eval: true, execArgv: ['--trace-warnings'] })
{ eval: true, execArgv: ['--trace-warnings', '--conditions', 'meow'] })
.on('message', common.mustCall((data) => {
assert.deepStrictEqual(data, ['--trace-warnings']);
assert.deepStrictEqual(data, ['--trace-warnings', '--conditions', 'meow']);
}));
} else {
process.emitWarning('some warning');
Expand Down

0 comments on commit 95155c4

Please sign in to comment.