From 1c4d92c9b546cebc866f1d38778394e7225cf617 Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Sun, 2 Apr 2023 09:30:47 +0300 Subject: [PATCH 1/2] watch: fix watch path with equals --- lib/internal/main/watch_mode.js | 7 +++++-- test/sequential/test-watch-mode.mjs | 23 ++++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index 9ef731c06f50b6..5d0d29cc2ff9da 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -6,6 +6,7 @@ const { ArrayPrototypeMap, ArrayPrototypePushApply, ArrayPrototypeSlice, + StringPrototypeStartsWith, } = primordials; const { @@ -38,7 +39,9 @@ const kPreserveOutput = getOptionValue('--watch-preserve-output'); const kCommand = ArrayPrototypeSlice(process.argv, 1); const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' ')); const args = ArrayPrototypeFilter(process.execArgv, (arg, i, arr) => - arg !== '--watch-path' && arr[i - 1] !== '--watch-path' && arg !== '--watch' && arg !== '--watch-preserve-output'); + !StringPrototypeStartsWith(arg, '--watch-path') && + (!arr[i - 1] || !StringPrototypeStartsWith(arr[i - 1], '--watch-path')) && + arg !== '--watch' && arg !== '--watch-preserve-output'); ArrayPrototypePushApply(args, kCommand); const watcher = new FilesWatcher({ throttle: 500, mode: kShouldFilterModules ? 'filter' : 'all' }); @@ -50,7 +53,7 @@ let exited; function start() { exited = false; - const stdio = kShouldFilterModules ? ['inherit', 'inherit', 'inherit', 'ipc'] : undefined; + const stdio = kShouldFilterModules ? ['inherit', 'inherit', 'inherit', 'ipc'] : 'inherit'; child = spawn(process.execPath, args, { stdio, env: { ...process.env, WATCH_REPORT_DEPENDENCIES: '1' } }); watcher.watchChildProcessModules(child); child.once('exit', (code) => { diff --git a/test/sequential/test-watch-mode.mjs b/test/sequential/test-watch-mode.mjs index fbfd887571e6af..5c9c5aa8446f0d 100644 --- a/test/sequential/test-watch-mode.mjs +++ b/test/sequential/test-watch-mode.mjs @@ -148,7 +148,28 @@ describe('watch mode', { concurrency: false, timeout: 60_000 }, () => { args: ['--watch-path', fixtures.path('./watch-mode/subdir/'), file], }); - assert.strictEqual(stderr, ''); + assert.match(stderr, /Error: Cannot find module/); + assert(stderr.match(/Error: Cannot find module/g).length >= 2); + + assertRestartedCorrectly({ + stdout, + messages: { completed: `Failed running ${inspect(file)}`, restarted: `Restarting ${inspect(file)}` }, + }); + }); + + it('should watch when running an non-existing file - when specified under --watch-path with equals', { + skip: !supportsRecursive + }, async () => { + const file = fixtures.path('watch-mode/subdir/non-existing.js'); + const watchedFile = fixtures.path('watch-mode/subdir/file.js'); + const { stderr, stdout } = await spawnWithRestarts({ + file, + watchedFile, + args: [`--watch-path=${fixtures.path('./watch-mode/subdir/')}`, file], + }); + + assert.match(stderr, /Error: Cannot find module/); + assert(stderr.match(/Error: Cannot find module/g).length >= 2); assertRestartedCorrectly({ stdout, messages: { completed: `Failed running ${inspect(file)}`, restarted: `Restarting ${inspect(file)}` }, From cec5b36b51ef85f4be8c3f068b8a135ae2c76dba Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Sun, 2 Apr 2023 22:42:33 +0530 Subject: [PATCH 2/2] fixup! add a test for --watch-path --- test/sequential/test-watch-mode.mjs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/sequential/test-watch-mode.mjs b/test/sequential/test-watch-mode.mjs index 5c9c5aa8446f0d..e0b23107f703f6 100644 --- a/test/sequential/test-watch-mode.mjs +++ b/test/sequential/test-watch-mode.mjs @@ -137,6 +137,23 @@ describe('watch mode', { concurrency: false, timeout: 60_000 }, () => { }); }); + it('should watch changes to a file with watch-path', { + skip: !supportsRecursive, + }, async () => { + const file = createTmpFile(); + const watchedFile = fixtures.path('watch-mode/subdir/file.js'); + const { stderr, stdout } = await spawnWithRestarts({ + file, + watchedFile, + args: ['--watch-path', fixtures.path('./watch-mode/subdir'), file], + }); + assert.strictEqual(stderr, ''); + assertRestartedCorrectly({ + stdout, + messages: { inner: 'running', completed: `Completed running ${inspect(file)}`, restarted: `Restarting ${inspect(file)}` }, + }); + }); + it('should watch when running an non-existing file - when specified under --watch-path', { skip: !supportsRecursive }, async () => {