watch: strip watch flags from NODE_OPTIONS in child process#62143
Open
marcopiraccini wants to merge 4 commits intonodejs:mainfrom
Open
watch: strip watch flags from NODE_OPTIONS in child process#62143marcopiraccini wants to merge 4 commits intonodejs:mainfrom
marcopiraccini wants to merge 4 commits intonodejs:mainfrom
Conversation
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #62143 +/- ##
=======================================
Coverage 89.65% 89.65%
=======================================
Files 676 676
Lines 206543 206563 +20
Branches 39547 39558 +11
=======================================
+ Hits 185184 185202 +18
+ Misses 13480 13471 -9
- Partials 7879 7890 +11
🚀 New features to boost your workflow:
|
Renegade334
reviewed
Mar 8, 2026
lib/internal/main/watch_mode.js
Outdated
| let cleanNodeOptions = kNodeOptions; | ||
| if (kNodeOptions != null) { | ||
| const nodeOptionsArgs = []; | ||
| const parts = RegExpPrototypeSymbolSplit(/\s+/, kNodeOptions); |
Member
There was a problem hiding this comment.
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.
Contributor
Author
There was a problem hiding this comment.
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.
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
NODE_OPTIONScontains--watch, the watch mode parent process spawns a child that inherits the sameNODE_OPTIONS, causing the child to also enter watch mode and spawn another child, creating an infinite loop of process spawning.This PR strips watch-related flags (
--watch,--watch-path,--watch-preserve-output,--watch-kill-signal) fromNODE_OPTIONSbefore passing the environment to the child process. This mirrors the existing logic that already strips these flags fromprocess.execArgv.Non-watch flags in
NODE_OPTIONS(e.g.,--max-old-space-size) are preserved.To properly handle quoted strings and backslash escapes in
NODE_OPTIONSvalues (e.g.,--require "./path with --watch in name/f.js"), the C++ParseNodeOptionsEnvVartokenizer is exposed to JavaScript via a newinternalBinding('options')method. This reuses the exact same parser that Node.js uses at startup, ensuring consistent behavior.Fixes: #61740
This is actually similar to #61838 but more "surgical", it just avoid propagating
watchflags.