Skip to content

Commit 07da4e9

Browse files
pulkit-30richardlau
authored andcommitted
watch: fix infinite loop when passing --watch=true flag
PR-URL: #51160 Fixes: #51159 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
1 parent 09519c6 commit 07da4e9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/internal/main/watch_mode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' '));
4141
const args = ArrayPrototypeFilter(process.execArgv, (arg, i, arr) =>
4242
!StringPrototypeStartsWith(arg, '--watch-path') &&
4343
(!arr[i - 1] || !StringPrototypeStartsWith(arr[i - 1], '--watch-path')) &&
44-
arg !== '--watch' && arg !== '--watch-preserve-output');
44+
arg !== '--watch' && !StringPrototypeStartsWith(arg, '--watch=') && arg !== '--watch-preserve-output');
4545
ArrayPrototypePushApply(args, kCommand);
4646

4747
const watcher = new FilesWatcher({ debounce: 200, mode: kShouldFilterModules ? 'filter' : 'all' });

test/sequential/test-watch-mode.mjs

+18-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ function createTmpFile(content = 'console.log("running");', ext = '.js', basenam
3030
}
3131

3232
async function runWriteSucceed({
33-
file, watchedFile, args = [file], completed = 'Completed running', restarts = 2
33+
file, watchedFile, watchFlag = '--watch', args = [file], completed = 'Completed running', restarts = 2, options = {}
3434
}) {
35-
const child = spawn(execPath, ['--watch', '--no-warnings', ...args], { encoding: 'utf8', stdio: 'pipe' });
35+
const child = spawn(execPath, [watchFlag, '--no-warnings', ...args], { encoding: 'utf8', stdio: 'pipe', ...options });
3636
let completes = 0;
3737
let cancelRestarts = () => {};
3838
let stderr = '';
@@ -88,6 +88,22 @@ async function failWriteSucceed({ file, watchedFile }) {
8888
tmpdir.refresh();
8989

9090
describe('watch mode', { concurrency: true, timeout: 60_000 }, () => {
91+
it('should watch changes to a file', async () => {
92+
const file = createTmpFile();
93+
const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file, watchFlag: '--watch=true', options: {
94+
timeout: 10000
95+
} });
96+
97+
assert.strictEqual(stderr, '');
98+
assert.deepStrictEqual(stdout, [
99+
'running',
100+
`Completed running ${inspect(file)}`,
101+
`Restarting ${inspect(file)}`,
102+
'running',
103+
`Completed running ${inspect(file)}`,
104+
]);
105+
});
106+
91107
it('should watch changes to a file - event loop ended', async () => {
92108
const file = createTmpFile();
93109
const { stderr, stdout } = await runWriteSucceed({ file, watchedFile: file });

0 commit comments

Comments
 (0)