GeezerVison is showing github.com — watch: strip underscore aliases of watch flags · nodejs/node@dc0d947
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js +26-9Lines changed: 26 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | ArrayPrototypePushApply, | 8 | 8 | ArrayPrototypeSlice, | 9 | 9 | StringPrototypeIncludes, | | 10 | + StringPrototypeIndexOf, | 10 | 11 | StringPrototypeReplaceAll, | | 12 | + StringPrototypeSlice, | 11 | 13 | StringPrototypeStartsWith, | 12 | 14 | } = primordials; | 13 | 15 |
| @@ -42,9 +44,23 @@ const kPreserveOutput = getOptionValue('--watch-preserve-output'); | 42 | 44 | const kCommand = ArrayPrototypeSlice(process.argv, 1); | 43 | 45 | const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' ')); | 44 | 46 |
| | 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 | + | 45 | 61 | const argsWithoutWatchOptions = []; | 46 | 62 | for (let i = 0; i < process.execArgv.length; i++) { | 47 | | - const arg = process.execArgv[i]; | | 63 | + const arg = normalizeOptionName(process.execArgv[i]); | 48 | 64 | if (StringPrototypeStartsWith(arg, '--watch=')) { | 49 | 65 | continue; | 50 | 66 | } | @@ -75,7 +91,7 @@ for (let i = 0; i < process.execArgv.length; i++) { | 75 | 91 | if (arg === '--experimental-default-config-file') { | 76 | 92 | continue; | 77 | 93 | } | 78 | | - ArrayPrototypePush(argsWithoutWatchOptions, arg); | | 94 | + ArrayPrototypePush(argsWithoutWatchOptions, process.execArgv[i]); | 79 | 95 | } | 80 | 96 |
| 81 | 97 | ArrayPrototypePushApply(argsWithoutWatchOptions, kCommand); | @@ -89,15 +105,16 @@ if (kNodeOptions != null) { | 89 | 105 | const parts = parseNodeOptionsEnvVar(kNodeOptions); | 90 | 106 | for (let i = 0; i < parts.length; i++) { | 91 | 107 | 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=')) { | 98 | 115 | continue; | 99 | 116 | } | 100 | | - if (part === '--watch-path' || part === '--watch-kill-signal') { | | 117 | + if (name === '--watch-path' || name === '--watch-kill-signal') { | 101 | 118 | // Skip the flag and its separate value argument | 102 | 119 | i++; | 103 | 120 | continue; | |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-watch-mode.mjs +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1033,6 +1033,60 @@ process.on('message', (message) => { | 1033 | 1033 | } | 1034 | 1034 | }); | 1035 | 1035 |
| | 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 | + | 1036 | 1090 | it('should not strip --watch when it appears inside a quoted NODE_OPTIONS value', { | 1037 | 1091 | // Honoring --require from NODE_OPTIONS is required for this test. | 1038 | 1092 | skip: !!process.config.variables.node_without_node_options, | |
|