Skip to content

Commit 4acb854

Browse files
watch: fix watch args not being properly filtered
currently when --watch is used, the argv arguments that the target script receives are filtered so that they don't include watch related arguments, however the current filtering logic is incorrect and it causes some watch values to incorrectly pass the filtering, the changes here address such issue PR-URL: #57936 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent c46b2b9 commit 4acb854

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

lib/internal/main/watch_mode.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,26 @@ const argsWithoutWatchOptions = [];
4343

4444
for (let i = 0; i < process.execArgv.length; i++) {
4545
const arg = process.execArgv[i];
46-
if (StringPrototypeStartsWith(arg, '--watch')) {
47-
i++;
48-
const nextArg = process.execArgv[i];
49-
if (nextArg && nextArg[0] === '-') {
50-
ArrayPrototypePush(argsWithoutWatchOptions, nextArg);
46+
if (StringPrototypeStartsWith(arg, '--watch=')) {
47+
continue;
48+
}
49+
if (arg === '--watch') {
50+
const nextArg = process.execArgv[i + 1];
51+
if (nextArg && nextArg[0] !== '-') {
52+
// If `--watch` doesn't include `=` and the next
53+
// argument is not a flag then it is interpreted as
54+
// the watch argument, so we need to skip that as well
55+
i++;
56+
}
57+
continue;
58+
}
59+
if (StringPrototypeStartsWith(arg, '--watch-path')) {
60+
const lengthOfWatchPathStr = 12;
61+
if (arg[lengthOfWatchPathStr] !== '=') {
62+
// if --watch-path doesn't include `=` it means
63+
// that the next arg is the target path, so we
64+
// need to skip that as well
65+
i++;
5166
}
5267
continue;
5368
}

test/sequential/test-watch-mode.mjs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,4 +791,54 @@ process.on('message', (message) => {
791791
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
792792
]);
793793
});
794+
795+
it('when multiple `--watch` flags are provided should run as if only one was', async () => {
796+
const projectDir = tmpdir.resolve('project-multi-flag');
797+
mkdirSync(projectDir);
798+
799+
const file = createTmpFile(`
800+
console.log(
801+
process.argv.some(arg => arg === '--watch')
802+
? 'Error: unexpected --watch args present'
803+
: 'no --watch args present'
804+
);`, '.js', projectDir);
805+
const args = ['--watch', '--watch', file];
806+
const { stdout, stderr } = await runWriteSucceed({
807+
file, watchedFile: file, watchFlag: null, args, options: { cwd: projectDir }
808+
});
809+
810+
assert.strictEqual(stderr, '');
811+
assert.deepStrictEqual(stdout, [
812+
'no --watch args present',
813+
`Completed running ${inspect(file)}`,
814+
`Restarting ${inspect(file)}`,
815+
'no --watch args present',
816+
`Completed running ${inspect(file)}`,
817+
]);
818+
});
819+
820+
it('`--watch-path` ars without `=` used alongside `--watch` should not make it into the script', async () => {
821+
const projectDir = tmpdir.resolve('project-watch-watch-path-args');
822+
mkdirSync(projectDir);
823+
824+
const file = createTmpFile(`
825+
console.log(
826+
process.argv.slice(2).some(arg => arg.endsWith('.js'))
827+
? 'some cli args end with .js'
828+
: 'no cli arg ends with .js'
829+
);`, '.js', projectDir);
830+
const args = ['--watch', `--watch-path`, file, file];
831+
const { stdout, stderr } = await runWriteSucceed({
832+
file, watchedFile: file, watchFlag: null, args, options: { cwd: projectDir }
833+
});
834+
835+
assert.strictEqual(stderr, '');
836+
assert.deepStrictEqual(stdout, [
837+
'no cli arg ends with .js',
838+
`Completed running ${inspect(file)}`,
839+
`Restarting ${inspect(file)}`,
840+
'no cli arg ends with .js',
841+
`Completed running ${inspect(file)}`,
842+
]);
843+
});
794844
});

0 commit comments

Comments
 (0)