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
28 changes: 28 additions & 0 deletions lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
RegExpPrototypeSymbolSplit,
StringPrototypeStartsWith,
} = primordials;

Expand Down Expand Up @@ -84,6 +85,32 @@ for (let i = 0; i < process.execArgv.length; i++) {

ArrayPrototypePushApply(argsWithoutWatchOptions, kCommand);

const kNodeOptions = process.env.NODE_OPTIONS;
let cleanNodeOptions = kNodeOptions;
if (kNodeOptions != null) {
const nodeOptionsArgs = [];
const parts = RegExpPrototypeSymbolSplit(/\s+/, kNodeOptions);
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (part === '') continue;
if (part === '--watch' ||
part === '--watch-preserve-output' ||
StringPrototypeStartsWith(part, '--watch=') ||
StringPrototypeStartsWith(part, '--watch-preserve-output=') ||
StringPrototypeStartsWith(part, '--watch-path=') ||
StringPrototypeStartsWith(part, '--watch-kill-signal=')) {
continue;
}
if (part === '--watch-path' || part === '--watch-kill-signal') {
// These flags take a separate value argument
i++;
continue;
}
ArrayPrototypePush(nodeOptionsArgs, part);
}
cleanNodeOptions = ArrayPrototypeJoin(nodeOptionsArgs, ' ');
}

const watcher = new FilesWatcher({ debounce: 200, mode: kShouldFilterModules ? 'filter' : 'all' });
ArrayPrototypeForEach(kWatchedPaths, (p) => watcher.watchPath(p));

Expand All @@ -99,6 +126,7 @@ function start() {
env: {
...process.env,
WATCH_REPORT_DEPENDENCIES: '1',
NODE_OPTIONS: cleanNodeOptions,
},
});
watcher.watchChildProcessModules(child);
Expand Down
33 changes: 33 additions & 0 deletions test/sequential/test-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -922,4 +922,37 @@ process.on('message', (message) => {
await done();
}
});

it('should strip all watch flags from NODE_OPTIONS in child process', async () => {
const file = createTmpFile('console.log(process.env.NODE_OPTIONS);');
const nodeOptions = [
'--watch', // bare boolean
'--watch=true', // boolean with =value
'--watch-path=./src', // string with =value
'--watch-path', './test', // String with space-separated value
'--watch-preserve-output', // bare boolean
'--watch-preserve-output=true', // boolean with =value
'--watch-kill-signal=SIGKILL', // string with =value
'--watch-kill-signal', 'SIGINT', // String with space-separated value
'--max-old-space-size=4096',
'--no-warnings',
].join(' ');
const { done, restart } = runInBackground({
args: ['--watch', file],
options: {
env: { ...process.env, NODE_OPTIONS: nodeOptions },
},
});

try {
const { stdout, stderr } = await restart();

assert.strictEqual(stderr, '');
const nodeOptionsLine = stdout.find((line) => line.includes('--max-old-space-size'));
assert.ok(nodeOptionsLine);
assert.strictEqual(nodeOptionsLine, '--max-old-space-size=4096 --no-warnings');
} finally {
await done();
}
});
});
Loading