diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index dc8323a7c3b..6ccc90436e6 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -43,26 +43,11 @@ const argsWithoutWatchOptions = []; for (let i = 0; i < process.execArgv.length; i++) { const arg = process.execArgv[i]; - if (StringPrototypeStartsWith(arg, '--watch=')) { - continue; - } - if (arg === '--watch') { - const nextArg = process.execArgv[i + 1]; - if (nextArg && nextArg[0] !== '-') { - // If `--watch` doesn't include `=` and the next - // argument is not a flag then it is interpreted as - // the watch argument, so we need to skip that as well - i++; - } - continue; - } - if (StringPrototypeStartsWith(arg, '--watch-path')) { - const lengthOfWatchPathStr = 12; - if (arg[lengthOfWatchPathStr] !== '=') { - // if --watch-path doesn't include `=` it means - // that the next arg is the target path, so we - // need to skip that as well - i++; + if (StringPrototypeStartsWith(arg, '--watch')) { + i++; + const nextArg = process.execArgv[i]; + if (nextArg && nextArg[0] === '-') { + ArrayPrototypePush(argsWithoutWatchOptions, nextArg); } continue; } diff --git a/test/sequential/test-watch-mode.mjs b/test/sequential/test-watch-mode.mjs index a2edbf1125a..bb8b8953514 100644 --- a/test/sequential/test-watch-mode.mjs +++ b/test/sequential/test-watch-mode.mjs @@ -791,54 +791,4 @@ process.on('message', (message) => { `Completed running ${inspect(file)}. Waiting for file changes before restarting...`, ]); }); - - it('when multiple `--watch` flags are provided should run as if only one was', async () => { - const projectDir = tmpdir.resolve('project-multi-flag'); - mkdirSync(projectDir); - - const file = createTmpFile(` - console.log( - process.argv.some(arg => arg === '--watch') - ? 'Error: unexpected --watch args present' - : 'no --watch args present' - );`, '.js', projectDir); - const args = ['--watch', '--watch', file]; - const { stdout, stderr } = await runWriteSucceed({ - file, watchedFile: file, watchFlag: null, args, options: { cwd: projectDir } - }); - - assert.strictEqual(stderr, ''); - assert.deepStrictEqual(stdout, [ - 'no --watch args present', - `Completed running ${inspect(file)}`, - `Restarting ${inspect(file)}`, - 'no --watch args present', - `Completed running ${inspect(file)}`, - ]); - }); - - it('`--watch-path` ars without `=` used alongside `--watch` should not make it into the script', async () => { - const projectDir = tmpdir.resolve('project-watch-watch-path-args'); - mkdirSync(projectDir); - - const file = createTmpFile(` - console.log( - process.argv.slice(2).some(arg => arg.endsWith('.js')) - ? 'some cli args end with .js' - : 'no cli arg ends with .js' - );`, '.js', projectDir); - const args = ['--watch', `--watch-path`, file, file]; - const { stdout, stderr } = await runWriteSucceed({ - file, watchedFile: file, watchFlag: null, args, options: { cwd: projectDir } - }); - - assert.strictEqual(stderr, ''); - assert.deepStrictEqual(stdout, [ - 'no cli arg ends with .js', - `Completed running ${inspect(file)}`, - `Restarting ${inspect(file)}`, - 'no cli arg ends with .js', - `Completed running ${inspect(file)}`, - ]); - }); });