Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watch: fix watch path with equals #47369

Merged
merged 2 commits into from Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/internal/main/watch_mode.js
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
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