Skip to content

Commit 60c0892

Browse files
committed
watch: re-expose watch flags via execArgv
1 parent 6248db4 commit 60c0892

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/internal/main/watch_mode.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypePushApply,
99
ArrayPrototypeSlice,
10+
JSONStringify,
1011
StringPrototypeStartsWith,
1112
} = primordials;
1213

@@ -44,18 +45,22 @@ const kCommand = ArrayPrototypeSlice(process.argv, 1);
4445
const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' '));
4546

4647
const argsWithoutWatchOptions = [];
48+
const removedWatchFlags = [];
4749
const argsFromBinding = getOptionsAsFlagsFromBinding();
4850
for (let i = 0; i < argsFromBinding.length; i++) {
4951
const arg = argsFromBinding[i];
5052
if (StringPrototypeStartsWith(arg, '--watch=')) {
53+
ArrayPrototypePush(removedWatchFlags, arg);
5154
continue;
5255
}
5356
if (arg === '--watch') {
57+
ArrayPrototypePush(removedWatchFlags, arg);
5458
const nextArg = argsFromBinding[i + 1];
5559
if (nextArg && nextArg[0] !== '-') {
5660
// If `--watch` doesn't include `=` and the next
5761
// argument is not a flag then it is interpreted as
5862
// the watch argument, so we need to skip that as well
63+
ArrayPrototypePush(removedWatchFlags, nextArg);
5964
i++;
6065
}
6166
continue;
@@ -66,7 +71,14 @@ for (let i = 0; i < argsFromBinding.length; i++) {
6671
// if --watch-path doesn't include `=` it means
6772
// that the next arg is the target path, so we
6873
// need to skip that as well
69-
i++;
74+
ArrayPrototypePush(removedWatchFlags, arg);
75+
const nextArg = argsFromBinding[i + 1];
76+
if (nextArg) {
77+
ArrayPrototypePush(removedWatchFlags, nextArg);
78+
i++;
79+
}
80+
} else {
81+
ArrayPrototypePush(removedWatchFlags, arg);
7082
}
7183
continue;
7284
}
@@ -95,12 +107,16 @@ let exited;
95107
function start() {
96108
exited = false;
97109
const stdio = kShouldFilterModules ? ['inherit', 'inherit', 'inherit', 'ipc'] : 'inherit';
110+
const env = {
111+
...process.env,
112+
WATCH_REPORT_DEPENDENCIES: '1',
113+
};
114+
if (removedWatchFlags.length > 0) {
115+
env.NODE_WATCH_ARGS = JSONStringify(removedWatchFlags);
116+
}
98117
child = spawn(process.execPath, argsWithoutWatchOptions, {
99118
stdio,
100-
env: {
101-
...process.env,
102-
WATCH_REPORT_DEPENDENCIES: '1',
103-
},
119+
env,
104120
});
105121
watcher.watchChildProcessModules(child);
106122
if (kEnvFiles.length > 0) {

lib/internal/process/pre_execution.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
'use strict';
22

33
const {
4+
ArrayIsArray,
45
ArrayPrototypeForEach,
6+
ArrayPrototypeUnshiftApply,
57
Date,
68
DatePrototypeGetDate,
79
DatePrototypeGetFullYear,
810
DatePrototypeGetHours,
911
DatePrototypeGetMinutes,
1012
DatePrototypeGetMonth,
1113
DatePrototypeGetSeconds,
14+
JSONParse,
1215
NumberParseInt,
1316
ObjectDefineProperty,
1417
ObjectFreeze,
@@ -270,6 +273,19 @@ function patchProcessObject(expandArgv1) {
270273
process._exiting = false;
271274
process.argv[0] = process.execPath;
272275

276+
const watchArgsFromLauncher = process.env.NODE_WATCH_ARGS;
277+
if (watchArgsFromLauncher !== undefined) {
278+
delete process.env.NODE_WATCH_ARGS;
279+
try {
280+
const parsed = JSONParse(watchArgsFromLauncher);
281+
if (ArrayIsArray(parsed) && parsed.length > 0) {
282+
ArrayPrototypeUnshiftApply(process.execArgv, parsed);
283+
}
284+
} catch {
285+
// Ignore malformed payloads.
286+
}
287+
}
288+
273289
/** @type {string} */
274290
let mainEntry;
275291
// If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of

0 commit comments

Comments
 (0)