Skip to content

Commit

Permalink
watch: fix watch path with equals
Browse files Browse the repository at this point in the history
PR-URL: #47369
Fixes: #47296
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
MoLow authored Apr 5, 2023
1 parent f6f3e22 commit 25858e3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
ArrayPrototypeMap,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
StringPrototypeStartsWith,
} = primordials;

const {
Expand Down Expand Up @@ -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' });
Expand 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) => {
Expand Down
40 changes: 39 additions & 1 deletion test/sequential/test-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -148,7 +165,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)}` },
Expand Down

0 comments on commit 25858e3

Please sign in to comment.