@@ -320,6 +320,38 @@ function withFallback<T>(getter: () => T, fallback: T): T {
320
320
}
321
321
}
322
322
323
+ function dirContains ( dir : string , file : string ) : boolean {
324
+ let relative = path . relative ( dir , file )
325
+ return ! ! relative && ! relative . startsWith ( '..' ) && ! path . isAbsolute ( relative )
326
+ }
327
+
328
+ function changeAffectsFile ( change : string , files : string [ ] ) : boolean {
329
+ for ( let file of files ) {
330
+ console . log ( { change, file, contains : dirContains ( change , file ) } )
331
+ if ( change === file || dirContains ( change , file ) ) {
332
+ return true
333
+ }
334
+ }
335
+ return false
336
+ }
337
+
338
+ // We need to add parent directories to the watcher:
339
+ // https://github.com/microsoft/vscode/issues/60813
340
+ function getWatchPatternsForFile ( file : string ) : string [ ] {
341
+ let tmp : string
342
+ let dir = path . dirname ( file )
343
+ let patterns : string [ ] = [ file , dir ]
344
+ while ( true ) {
345
+ dir = path . dirname ( ( tmp = dir ) )
346
+ if ( tmp === dir ) {
347
+ break
348
+ } else {
349
+ patterns . push ( dir )
350
+ }
351
+ }
352
+ return patterns
353
+ }
354
+
323
355
async function createProjectService (
324
356
projectConfig : ProjectConfig ,
325
357
connection : Connection ,
@@ -358,7 +390,14 @@ async function createProjectService(
358
390
}
359
391
360
392
if ( projectConfig . configPath ) {
361
- watchPatterns ( [ projectConfig . configPath , ...getModuleDependencies ( projectConfig . configPath ) ] )
393
+ let deps = [ ]
394
+ try {
395
+ deps = getModuleDependencies ( projectConfig . configPath )
396
+ } catch { }
397
+ watchPatterns ( [
398
+ ...getWatchPatternsForFile ( projectConfig . configPath ) ,
399
+ ...deps . flatMap ( ( dep ) => getWatchPatternsForFile ( dep ) ) ,
400
+ ] )
362
401
}
363
402
364
403
function log ( ...args : string [ ] ) : void {
@@ -374,10 +413,8 @@ async function createProjectService(
374
413
for ( let change of changes ) {
375
414
let file = normalizePath ( change . file )
376
415
377
- let isConfigFile = projectConfig . configPath
378
- ? file === projectConfig . configPath
379
- : minimatch ( file , `**/${ CONFIG_GLOB } ` , { dot : true } )
380
- let isDependency = state . dependencies && state . dependencies . includes ( change . file )
416
+ let isConfigFile = changeAffectsFile ( file , [ projectConfig . configPath ] )
417
+ let isDependency = changeAffectsFile ( change . file , state . dependencies ?? [ ] )
381
418
let isPackageFile = minimatch ( file , `**/${ PACKAGE_LOCK_GLOB } ` , { dot : true } )
382
419
383
420
if ( ! isConfigFile && ! isDependency && ! isPackageFile ) continue
@@ -432,14 +469,14 @@ async function createProjectService(
432
469
433
470
function resetState ( ) : void {
434
471
// clearAllDiagnostics(state)
435
- refreshDiagnostics ( )
436
472
Object . keys ( state ) . forEach ( ( key ) => {
437
473
// Keep `dependencies` to ensure that they are still watched
438
474
if ( key !== 'editor' && key !== 'dependencies' ) {
439
475
delete state [ key ]
440
476
}
441
477
} )
442
478
state . enabled = false
479
+ refreshDiagnostics ( )
443
480
updateCapabilities ( )
444
481
}
445
482
@@ -478,7 +515,7 @@ async function createProjectService(
478
515
throw new SilentError ( 'No config file found.' )
479
516
}
480
517
481
- watchPatterns ( [ configPath ] )
518
+ watchPatterns ( getWatchPatternsForFile ( configPath ) )
482
519
483
520
const pnpPath = findUp . sync (
484
521
( dir ) => {
@@ -967,7 +1004,7 @@ async function createProjectService(
967
1004
// }
968
1005
state . dependencies = getModuleDependencies ( state . configPath )
969
1006
// chokidarWatcher?.add(state.dependencies)
970
- watchPatterns ( state . dependencies ?? [ ] )
1007
+ watchPatterns ( ( state . dependencies ?? [ ] ) . flatMap ( ( dep ) => getWatchPatternsForFile ( dep ) ) )
971
1008
972
1009
state . configId = getConfigId ( state . configPath , state . dependencies )
973
1010
@@ -1686,8 +1723,8 @@ class TW {
1686
1723
for ( let [ key ] of this . projects ) {
1687
1724
let projectConfig = JSON . parse ( key ) as ProjectConfig
1688
1725
if (
1689
- normalizedFilename === projectConfig . configPath &&
1690
- change . type === FileChangeType . Deleted
1726
+ change . type === FileChangeType . Deleted &&
1727
+ changeAffectsFile ( normalizedFilename , [ projectConfig . configPath ] )
1691
1728
) {
1692
1729
needsRestart = true
1693
1730
break changeLoop
0 commit comments