Skip to content

Commit ae8a6de

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: #58279 Fixes: #57124 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 656bf7d commit ae8a6de

File tree

2 files changed

+117
-5
lines changed

2 files changed

+117
-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
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import * as common from '../common/index.mjs';
2+
import tmpdir from '../common/tmpdir.js';
3+
import assert from 'node:assert';
4+
import path from 'node:path';
5+
import { execPath } from 'node:process';
6+
import { describe, it } from 'node:test';
7+
import { spawn } from 'node:child_process';
8+
import { writeFileSync, mkdirSync } from 'node:fs';
9+
import { inspect } from 'node:util';
10+
import { createInterface } from 'node:readline';
11+
12+
if (common.isIBMi)
13+
common.skip('IBMi does not support `fs.watch()`');
14+
15+
let tmpFiles = 0;
16+
function createTmpFile(content, ext, basename) {
17+
const file = path.join(basename, `${tmpFiles++}${ext}`);
18+
writeFileSync(file, content);
19+
return file;
20+
}
21+
22+
async function runNode({
23+
args,
24+
expectedCompletionLog = 'Completed running',
25+
options = {},
26+
}) {
27+
const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options });
28+
let stderr = '';
29+
const stdout = [];
30+
31+
child.stderr.on('data', (data) => {
32+
stderr += data;
33+
});
34+
35+
try {
36+
// Break the chunks into lines
37+
for await (const data of createInterface({ input: child.stdout })) {
38+
if (!data.startsWith('Waiting for graceful termination') && !data.startsWith('Gracefully restarted')) {
39+
stdout.push(data);
40+
}
41+
if (data.startsWith(expectedCompletionLog)) {
42+
break;
43+
}
44+
}
45+
} finally {
46+
child.kill();
47+
}
48+
return { stdout, stderr, pid: child.pid };
49+
}
50+
51+
tmpdir.refresh();
52+
53+
describe('watch mode - watch flags', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => {
54+
it('when multiple `--watch` flags are provided should run as if only one was', async () => {
55+
const projectDir = tmpdir.resolve('project-multi-flag');
56+
mkdirSync(projectDir);
57+
58+
const file = createTmpFile(`
59+
console.log(
60+
process.argv.some(arg => arg === '--watch')
61+
? 'Error: unexpected --watch args present'
62+
: 'no --watch args present'
63+
);`, '.js', projectDir);
64+
const args = ['--watch', '--watch', file];
65+
const { stdout, stderr } = await runNode({
66+
file, args, options: { cwd: projectDir }
67+
});
68+
69+
assert.strictEqual(stderr, '');
70+
assert.deepStrictEqual(stdout, [
71+
'no --watch args present',
72+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
73+
]);
74+
});
75+
76+
it('`--watch-path` args without `=` used alongside `--watch` should not make it into the script', async () => {
77+
const projectDir = tmpdir.resolve('project-watch-watch-path-args');
78+
mkdirSync(projectDir);
79+
80+
const file = createTmpFile(`
81+
console.log(
82+
process.argv.slice(2).some(arg => arg.endsWith('.js'))
83+
? 'some cli args end with .js'
84+
: 'no cli arg ends with .js'
85+
);`, '.js', projectDir);
86+
const args = ['--watch', `--watch-path`, file, file];
87+
const { stdout, stderr } = await runNode({
88+
file, args, options: { cwd: projectDir }
89+
});
90+
91+
assert.strictEqual(stderr, '');
92+
assert.deepStrictEqual(stdout, [
93+
'no cli arg ends with .js',
94+
`Completed running ${inspect(file)}. Waiting for file changes before restarting...`,
95+
]);
96+
});
97+
});

0 commit comments

Comments
 (0)