From 9d14a051560c31ed04e839a9693e1becfe3d9462 Mon Sep 17 00:00:00 2001 From: Santusht kotai <115890693+santusht06@users.noreply.github.com> Date: Tue, 25 Aug 2026 18:17:06 +0530 Subject: [PATCH] module: forward execArgv to ESM loader hook worker When spawning the internal worker thread for asynchronous loader hooks in AsyncLoaderHookWorker, process.execArgv was not passed to the InternalWorker constructor options. As a result, custom CLI flags such as `--conditions ` set when creating a Worker thread were omitted from the loader hook worker thread's environment. This commit forwards `process.execArgv` to the `InternalWorker` instance so that loader hooks registered within worker threads receive custom conditions and options. Fixes: https://github.com/nodejs/node/issues/50885 Signed-off-by: Santusht kotai <115890693+santusht06@users.noreply.github.com> --- lib/internal/modules/esm/hooks.js | 1 + .../test-esm-loader-worker-conditions.mjs | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 test/es-module/test-esm-loader-worker-conditions.mjs diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index 4ced5521aca2..c7300fcbb721 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -529,6 +529,7 @@ class AsyncLoaderHookWorker { stdin: false, stdout: false, trackUnmanagedFds: false, + execArgv: process.execArgv, workerData: { lock, }, diff --git a/test/es-module/test-esm-loader-worker-conditions.mjs b/test/es-module/test-esm-loader-worker-conditions.mjs new file mode 100644 index 000000000000..825f317fb8cf --- /dev/null +++ b/test/es-module/test-esm-loader-worker-conditions.mjs @@ -0,0 +1,53 @@ +import { mustCall, mustNotCall } from '../common/index.mjs'; +import * as fixtures from '../common/fixtures.mjs'; +import assert from 'node:assert'; +import { Worker } from 'node:worker_threads'; + +const loaderURL = fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs'); +const targetURL = fixtures.fileURL('es-modules', 'conditional-exports.mjs'); + +// Test that custom conditions passed to Worker via execArgv +// are forwarded to the internal ESM loader hook worker thread when using register(). +{ + const worker = new Worker( + ` + import assert from 'node:assert'; + import { register } from 'node:module'; + register(${JSON.stringify(loaderURL.href)}); + const ns = await import(${JSON.stringify(targetURL.href)}); + assert.strictEqual(ns.default, 'from custom condition'); + `, + { + eval: true, + execArgv: ['--conditions', 'custom-condition', '--no-warnings'], + } + ); + + worker.on('error', mustNotCall()); + worker.on('exit', mustCall((code) => { + assert.strictEqual(code, 0); + })); +} + +// Test that custom conditions passed to Worker via execArgv +// are forwarded to the internal ESM loader hook worker thread when using registerHooks(). +{ + const worker = new Worker( + ` + import assert from 'node:assert'; + import { registerHooks } from 'node:module'; + registerHooks(${JSON.stringify(loaderURL.href)}); + const ns = await import(${JSON.stringify(targetURL.href)}); + assert.strictEqual(ns.default, 'from custom condition'); + `, + { + eval: true, + execArgv: ['--conditions', 'custom-condition', '--no-warnings'], + } + ); + + worker.on('error', mustNotCall()); + worker.on('exit', mustCall((code) => { + assert.strictEqual(code, 0); + })); +}