From f5d925706a86dd7db0030ad0862f5dfbf5a9a04a Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Thu, 2 May 2024 21:45:40 +0300 Subject: [PATCH] watch: fix arguments parsing PR-URL: https://github.com/nodejs/node/pull/52760 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Raz Luvaton --- lib/internal/main/watch_mode.js | 7 +++-- test/sequential/test-watch-mode.mjs | 45 ++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index c594f64bfca87d..4381cf0d381461 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -6,7 +6,6 @@ const { ArrayPrototypePush, ArrayPrototypePushApply, ArrayPrototypeSlice, - StringPrototypeIncludes, StringPrototypeStartsWith, } = primordials; @@ -44,8 +43,10 @@ const argsWithoutWatchOptions = []; for (let i = 0; i < process.execArgv.length; i++) { const arg = process.execArgv[i]; if (StringPrototypeStartsWith(arg, '--watch')) { - if (!StringPrototypeIncludes(arg, '=')) { - i++; + i++; + const nextArg = process.execArgv[i]; + if (nextArg && StringPrototypeStartsWith(nextArg, '-')) { + ArrayPrototypePush(argsWithoutWatchOptions, nextArg); } continue; } diff --git a/test/sequential/test-watch-mode.mjs b/test/sequential/test-watch-mode.mjs index cc8c56d67ec77e..8bf0bdff7899d2 100644 --- a/test/sequential/test-watch-mode.mjs +++ b/test/sequential/test-watch-mode.mjs @@ -39,7 +39,9 @@ async function runWriteSucceed({ options = {}, shouldFail = false }) { - const child = spawn(execPath, [watchFlag, '--no-warnings', ...args], { encoding: 'utf8', stdio: 'pipe', ...options }); + args.unshift('--no-warnings'); + if (watchFlag !== null) args.unshift(watchFlag); + const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options }); let completes = 0; let cancelRestarts = () => {}; let stderr = ''; @@ -531,4 +533,45 @@ console.log(values.random); `Completed running ${inspect(file)}`, ]); }); + + it('should run when `--watch --inspect`', async () => { + const file = createTmpFile(); + const args = ['--watch', '--inspect', file]; + const { stdout, stderr } = await runWriteSucceed({ file, watchedFile: file, watchFlag: null, args }); + + assert.match(stderr, /listening on ws:\/\//); + assert.deepStrictEqual(stdout, [ + 'running', + `Completed running ${inspect(file)}`, + `Restarting ${inspect(file)}`, + 'running', + `Completed running ${inspect(file)}`, + ]); + }); + + it('should run when `--watch -r ./foo.js`', async () => { + const projectDir = tmpdir.resolve('project7'); + mkdirSync(projectDir); + + const dir = path.join(projectDir, 'watched-dir'); + mkdirSync(dir); + writeFileSync(path.join(projectDir, 'some.js'), "console.log('hello')"); + + const file = createTmpFile("console.log('running');", '.js', projectDir); + const args = ['--watch', '-r', './some.js', file]; + const { stdout, stderr } = await runWriteSucceed({ + file, watchedFile: file, watchFlag: null, args, options: { cwd: projectDir } + }); + + assert.strictEqual(stderr, ''); + assert.deepStrictEqual(stdout, [ + 'hello', + 'running', + `Completed running ${inspect(file)}`, + `Restarting ${inspect(file)}`, + 'hello', + 'running', + `Completed running ${inspect(file)}`, + ]); + }); });