Skip to content

Commit d3e0229

Browse files
committed
watch: fix watch path with equals
PR-URL: #47369Fixes: #47296 Reviewed-By: Debadree Chatterjee <debadree333@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent a779261 commit d3e0229

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎lib/internal/main/watch_mode.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ArrayPrototypeMap,
77
ArrayPrototypePushApply,
88
ArrayPrototypeSlice,
9+
StringPrototypeStartsWith,
910
}=primordials;
1011

1112
const{
@@ -36,7 +37,9 @@ const kPreserveOutput = getOptionValue('--watch-preserve-output');
3637
constkCommand=ArrayPrototypeSlice(process.argv,1);
3738
constkCommandStr=inspect(ArrayPrototypeJoin(kCommand,' '));
3839
constargs=ArrayPrototypeFilter(process.execArgv,(arg,i,arr)=>
39-
arg!=='--watch-path'&&arr[i-1]!=='--watch-path'&&arg!=='--watch'&&arg!=='--watch-preserve-output');
40+
!StringPrototypeStartsWith(arg,'--watch-path')&&
41+
(!arr[i-1]||!StringPrototypeStartsWith(arr[i-1],'--watch-path'))&&
42+
arg!=='--watch'&&arg!=='--watch-preserve-output');
4043
ArrayPrototypePushApply(args,kCommand);
4144

4245
constwatcher=newFilesWatcher({throttle: 500,mode: kShouldFilterModules ? 'filter' : 'all'});
@@ -48,7 +51,7 @@ let exited;
4851

4952
functionstart(){
5053
exited=false;
51-
conststdio=kShouldFilterModules ? ['inherit','inherit','inherit','ipc'] : undefined;
54+
conststdio=kShouldFilterModules ? ['inherit','inherit','inherit','ipc'] : 'inherit';
5255
child=spawn(process.execPath,args,{ stdio,env: { ...process.env,WATCH_REPORT_DEPENDENCIES: '1'}});
5356
watcher.watchChildProcessModules(child);
5457
child.once('exit',(code)=>{

‎test/sequential/test-watch-mode.mjs‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ describe('watch mode', { concurrency: false, timeout: 60_000 }, () => {
137137
});
138138
});
139139

140+
it('should watch changes to a file with watch-path',{
141+
skip: !supportsRecursive,
142+
},async()=>{
143+
constfile=createTmpFile();
144+
constwatchedFile=fixtures.path('watch-mode/subdir/file.js');
145+
const{ stderr, stdout }=awaitspawnWithRestarts({
146+
file,
147+
watchedFile,
148+
args: ['--watch-path',fixtures.path('./watch-mode/subdir'),file],
149+
});
150+
assert.strictEqual(stderr,'');
151+
assertRestartedCorrectly({
152+
stdout,
153+
messages: {inner: 'running',completed: `Completed running ${inspect(file)}`,restarted: `Restarting ${inspect(file)}`},
154+
});
155+
});
156+
140157
it('should watch when running an non-existing file - when specified under --watch-path',{
141158
skip: !supportsRecursive
142159
},async()=>{
@@ -148,7 +165,28 @@ describe('watch mode', { concurrency: false, timeout: 60_000 }, () => {
148165
args: ['--watch-path',fixtures.path('./watch-mode/subdir/'),file],
149166
});
150167

151-
assert.strictEqual(stderr,'');
168+
assert.match(stderr,/Error:Cannotfindmodule/);
169+
assert(stderr.match(/Error:Cannotfindmodule/g).length>=2);
170+
171+
assertRestartedCorrectly({
172+
stdout,
173+
messages: {completed: `Failed running ${inspect(file)}`,restarted: `Restarting ${inspect(file)}`},
174+
});
175+
});
176+
177+
it('should watch when running an non-existing file - when specified under --watch-path with equals',{
178+
skip: !supportsRecursive
179+
},async()=>{
180+
constfile=fixtures.path('watch-mode/subdir/non-existing.js');
181+
constwatchedFile=fixtures.path('watch-mode/subdir/file.js');
182+
const{ stderr, stdout }=awaitspawnWithRestarts({
183+
file,
184+
watchedFile,
185+
args: [`--watch-path=${fixtures.path('./watch-mode/subdir/')}`,file],
186+
});
187+
188+
assert.match(stderr,/Error:Cannotfindmodule/);
189+
assert(stderr.match(/Error:Cannotfindmodule/g).length>=2);
152190
assertRestartedCorrectly({
153191
stdout,
154192
messages: {completed: `Failed running ${inspect(file)}`,restarted: `Restarting ${inspect(file)}`},

0 commit comments

Comments
 (0)