@@ -283,11 +283,15 @@ export function getDirectoryToWatchFailedLookupLocation(
283
283
// Ensure failed look up is normalized path
284
284
failedLookupLocation = isRootedDiskPath ( failedLookupLocation ) ? normalizePath ( failedLookupLocation ) : getNormalizedAbsolutePath ( failedLookupLocation , getCurrentDirectory ( ) ) ;
285
285
const failedLookupComponents = getPathComponents ( failedLookupLocation ) ;
286
+ const perceivedOsRootLength = perceivedOsRootLengthForWatching ( failedLookupPathComponents , failedLookupPathComponents . length ) ;
287
+ if ( failedLookupPathComponents . length <= perceivedOsRootLength + 1 ) return undefined ;
288
+ // If directory path contains node module, get the most parent node_modules directory for watching
289
+ const nodeModulesIndex = failedLookupPathComponents . indexOf ( "node_modules" as Path ) ;
290
+ if ( nodeModulesIndex !== - 1 && nodeModulesIndex + 1 <= perceivedOsRootLength + 1 ) return undefined ; // node_modules not at position where it can be watched
286
291
if ( isInDirectoryPath ( rootPathComponents , failedLookupPathComponents ) ) {
287
- Debug . assert ( failedLookupComponents . length === failedLookupPathComponents . length , `FailedLookup: ${ failedLookupLocation } failedLookupLocationPath: ${ failedLookupLocationPath } ` ) ;
288
292
if ( failedLookupPathComponents . length > rootPathComponents . length + 1 ) {
289
293
// Instead of watching root, watch directory in root to avoid watching excluded directories not needed for module resolution
290
- return getDirectoryOfFailedLookupWatch ( failedLookupComponents , failedLookupPathComponents , rootPathComponents . length + 1 ) ;
294
+ return getDirectoryOfFailedLookupWatch ( failedLookupComponents , failedLookupPathComponents , Math . max ( rootPathComponents . length + 1 , perceivedOsRootLength + 1 ) ) ;
291
295
}
292
296
else {
293
297
// Always watch root directory non recursively
@@ -304,50 +308,35 @@ export function getDirectoryToWatchFailedLookupLocation(
304
308
return getDirectoryToWatchFromFailedLookupLocationDirectory (
305
309
failedLookupComponents ,
306
310
failedLookupPathComponents ,
311
+ perceivedOsRootLength ,
312
+ nodeModulesIndex ,
307
313
rootPathComponents ,
308
314
) ;
309
315
}
310
316
311
- function getDirectoryToWatchFailedLookupLocationNodeModules (
312
- dirPathComponents : Readonly < PathPathComponents > ,
313
- ) {
314
- // If directory path contains node module, get the most parent node_modules directory for watching
315
- const indexOfNodeModules = dirPathComponents . indexOf ( "node_modules" as Path ) ;
316
- if ( indexOfNodeModules !== - 1 ) {
317
- // If the directory is node_modules use it to watch, always watch it recursively
318
- return canWatchDirectoryOrFile ( dirPathComponents , indexOfNodeModules + 1 ) ?
319
- indexOfNodeModules + 1 :
320
- false ;
321
- }
322
- return undefined ;
323
- }
324
-
325
317
function getDirectoryToWatchFromFailedLookupLocationDirectory (
326
318
dirComponents : readonly string [ ] ,
327
319
dirPathComponents : Readonly < PathPathComponents > ,
320
+ perceivedOsRootLength : number ,
321
+ nodeModulesIndex : number ,
328
322
rootPathComponents : Readonly < PathPathComponents > ,
329
323
) : DirectoryOfFailedLookupWatch | undefined {
330
324
// If directory path contains node module, get the most parent node_modules directory for watching
331
- const nodeModulesWatchLength = getDirectoryToWatchFailedLookupLocationNodeModules ( dirPathComponents ) ;
332
- if ( nodeModulesWatchLength !== undefined ) {
325
+ if ( nodeModulesIndex !== - 1 ) {
333
326
// If the directory is node_modules use it to watch, always watch it recursively
334
- return nodeModulesWatchLength ?
335
- getDirectoryOfFailedLookupWatch ( dirComponents , dirPathComponents , nodeModulesWatchLength ) :
336
- undefined ;
327
+ return getDirectoryOfFailedLookupWatch ( dirComponents , dirPathComponents , nodeModulesIndex + 1 ) ;
337
328
}
338
329
// Use some ancestor of the root directory
339
330
let nonRecursive = true ;
340
331
let length : number | undefined ;
341
332
for ( let i = 0 ; i < dirPathComponents . length ; i ++ ) {
342
333
if ( dirPathComponents [ i ] !== rootPathComponents [ i ] ) {
343
334
nonRecursive = false ;
344
- length = i + 1 ;
335
+ length = Math . max ( i + 1 , perceivedOsRootLength + 1 ) ;
345
336
break ;
346
337
}
347
338
}
348
- return canWatchDirectoryOrFile ( dirPathComponents , length ) ?
349
- getDirectoryOfFailedLookupWatch ( dirComponents , dirPathComponents , length , nonRecursive ) :
350
- undefined ;
339
+ return getDirectoryOfFailedLookupWatch ( dirComponents , dirPathComponents , length , nonRecursive ) ;
351
340
}
352
341
353
342
function getDirectoryOfFailedLookupWatch (
@@ -378,7 +367,13 @@ export function getDirectoryToWatchFailedLookupLocationFromTypeRoot(
378
367
return rootPath ;
379
368
}
380
369
typeRoot = isRootedDiskPath ( typeRoot ) ? normalizePath ( typeRoot ) : getNormalizedAbsolutePath ( typeRoot , getCurrentDirectory ( ) ) ;
381
- const toWatch = getDirectoryToWatchFromFailedLookupLocationDirectory ( getPathComponents ( typeRoot ) , typeRootPathComponents , rootPathComponents ) ;
370
+ const toWatch = getDirectoryToWatchFromFailedLookupLocationDirectory (
371
+ getPathComponents ( typeRoot ) ,
372
+ typeRootPathComponents ,
373
+ perceivedOsRootLengthForWatching ( typeRootPathComponents , typeRootPathComponents . length ) ,
374
+ typeRootPathComponents . indexOf ( "node_modules" as Path ) ,
375
+ rootPathComponents ,
376
+ ) ;
382
377
return toWatch && filterCustomPath ( toWatch . dirPath ) ? toWatch . dirPath : undefined ;
383
378
}
384
379
0 commit comments