Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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);
Copy link
Copy Markdown
Member

@Renegade334 Renegade334 Mar 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately, this sort of approach isn't robust enough.

$ mkdir 'test for --watch parsing'
$ echo 'console.log("hi!")' > 'test for --watch parsing/test.cjs'
$ NODE_OPTIONS='--require "./test for --watch parsing/test.cjs"' node -p 'process.env.NODE_OPTIONS.split(" ")'
hi!
[ '--require', '"./test', 'for', '--watch', 'parsing/test.cjs"' ]
                                  ^^^^^^^

I suspect this is going to need to interact with the options parser in some way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.
Thinking about it, maybe adding a C++ binding that exposes ParseNodeOptionsEnvVar to JS would be more robust (being the same parser to interpret NODE_OPTIONS, in my undersanding) and then finltering out watch flags. Will amend this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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