GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — watch: strip underscore aliases of watch flags · nodejs/node@dc0d947

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/main
    • watch_mode.js
  • test/sequential
    • test-watch-mode.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/main/watch_mode.js‎

Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js
+26-9Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const {
77
ArrayPrototypePushApply,
88
ArrayPrototypeSlice,
99
StringPrototypeIncludes,
10+
StringPrototypeIndexOf,
1011
StringPrototypeReplaceAll,
12+
StringPrototypeSlice,
1113
StringPrototypeStartsWith,
1214
} = primordials;
1315

@@ -42,9 +44,23 @@ const kPreserveOutput = getOptionValue('--watch-preserve-output');
4244
const kCommand = ArrayPrototypeSlice(process.argv, 1);
4345
const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' '));
4446

47+
// The C++ option parser treats `_` and `-` as equivalent in option names
48+
// (e.g. --watch_path is --watch-path), so normalize before matching.
49+
function normalizeOptionName(arg) {
50+
if (!StringPrototypeStartsWith(arg, '--')) {
51+
return arg;
52+
}
53+
const equalsIndex = StringPrototypeIndexOf(arg, '=');
54+
if (equalsIndex === -1) {
55+
return StringPrototypeReplaceAll(arg, '_', '-');
56+
}
57+
return StringPrototypeReplaceAll(StringPrototypeSlice(arg, 0, equalsIndex), '_', '-') +
58+
StringPrototypeSlice(arg, equalsIndex);
59+
}
60+
4561
const argsWithoutWatchOptions = [];
4662
for (let i = 0; i < process.execArgv.length; i++) {
47-
const arg = process.execArgv[i];
63+
const arg = normalizeOptionName(process.execArgv[i]);
4864
if (StringPrototypeStartsWith(arg, '--watch=')) {
4965
continue;
5066
}
@@ -75,7 +91,7 @@ for (let i = 0; i < process.execArgv.length; i++) {
7591
if (arg === '--experimental-default-config-file') {
7692
continue;
7793
}
78-
ArrayPrototypePush(argsWithoutWatchOptions, arg);
94+
ArrayPrototypePush(argsWithoutWatchOptions, process.execArgv[i]);
7995
}
8096

8197
ArrayPrototypePushApply(argsWithoutWatchOptions, kCommand);
@@ -89,15 +105,16 @@ if (kNodeOptions != null) {
89105
const parts = parseNodeOptionsEnvVar(kNodeOptions);
90106
for (let i = 0; i < parts.length; i++) {
91107
const part = parts[i];
92-
if (part === '--watch' ||
93-
part === '--watch-preserve-output' ||
94-
StringPrototypeStartsWith(part, '--watch=') ||
95-
StringPrototypeStartsWith(part, '--watch-preserve-output=') ||
96-
StringPrototypeStartsWith(part, '--watch-path=') ||
97-
StringPrototypeStartsWith(part, '--watch-kill-signal=')) {
108+
const name = normalizeOptionName(part);
109+
if (name === '--watch' ||
110+
name === '--watch-preserve-output' ||
111+
StringPrototypeStartsWith(name, '--watch=') ||
112+
StringPrototypeStartsWith(name, '--watch-preserve-output=') ||
113+
StringPrototypeStartsWith(name, '--watch-path=') ||
114+
StringPrototypeStartsWith(name, '--watch-kill-signal=')) {
98115
continue;
99116
}
100-
if (part === '--watch-path' || part === '--watch-kill-signal') {
117+
if (name === '--watch-path' || name === '--watch-kill-signal') {
101118
// Skip the flag and its separate value argument
102119
i++;
103120
continue;
Collapse file

‎test/sequential/test-watch-mode.mjs‎

Copy file name to clipboardExpand all lines: test/sequential/test-watch-mode.mjs
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,60 @@ process.on('message', (message) => {
10331033
}
10341034
});
10351035

1036+
it('should strip underscore aliases of watch flags from NODE_OPTIONS', async () => {
1037+
// Avoid recursively watching the repository's source and test trees.
1038+
const cwd = tmpdir.resolve('node-options-underscore');
1039+
mkdirSync(path.join(cwd, 'src'), { recursive: true });
1040+
mkdirSync(path.join(cwd, 'test'));
1041+
const file = createTmpFile('console.log(process.env.NODE_OPTIONS);');
1042+
const nodeOptions = [
1043+
'--watch_path=./src',
1044+
'--watch_path', './test',
1045+
'--watch_preserve_output',
1046+
'--watch_preserve_output=true',
1047+
'--watch_kill_signal=SIGKILL',
1048+
'--watch_kill_signal', 'SIGINT',
1049+
'--max_old_space_size=4096',
1050+
'--no-warnings',
1051+
].join(' ');
1052+
const { done, restart } = runInBackground({
1053+
args: ['--watch', file],
1054+
options: {
1055+
cwd,
1056+
env: { ...process.env, NODE_OPTIONS: nodeOptions },
1057+
},
1058+
});
1059+
1060+
try {
1061+
const { stdout, stderr } = await restart();
1062+
1063+
assert.strictEqual(stderr, '');
1064+
const nodeOptionsLine = stdout.find((line) => line.includes('--max_old_space_size'));
1065+
assert.ok(nodeOptionsLine);
1066+
assert.strictEqual(nodeOptionsLine, '--max_old_space_size=4096 --no-warnings');
1067+
} finally {
1068+
await done();
1069+
}
1070+
});
1071+
1072+
it('should strip --watch_path from execArgv in child process', async () => {
1073+
const dir = tmpdir.resolve('exec-argv-underscore');
1074+
mkdirSync(dir, { recursive: true });
1075+
const file = createTmpFile('console.log(JSON.stringify(process.execArgv));');
1076+
const { done, restart } = runInBackground({
1077+
args: [`--watch_path=${dir}`, file],
1078+
});
1079+
1080+
try {
1081+
const { stdout, stderr } = await restart();
1082+
1083+
assert.strictEqual(stderr, '');
1084+
assert.ok(stdout.includes('["--no-warnings"]'));
1085+
} finally {
1086+
await done();
1087+
}
1088+
});
1089+
10361090
it('should not strip --watch when it appears inside a quoted NODE_OPTIONS value', {
10371091
// Honoring --require from NODE_OPTIONS is required for this test.
10381092
skip: !!process.config.variables.node_without_node_options,

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.