@@ -283,11 +283,15 @@ export function getDirectoryToWatchFailedLookupLocation(
283283 // Ensure failed look up is normalized path
284284 failedLookupLocation = isRootedDiskPath ( failedLookupLocation ) ? normalizePath ( failedLookupLocation ) : getNormalizedAbsolutePath ( failedLookupLocation , getCurrentDirectory ( ) ) ;
285285 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
286291 if ( isInDirectoryPath ( rootPathComponents , failedLookupPathComponents ) ) {
287- Debug . assert ( failedLookupComponents . length === failedLookupPathComponents . length , `FailedLookup: ${ failedLookupLocation } failedLookupLocationPath: ${ failedLookupLocationPath } ` ) ;
288292 if ( failedLookupPathComponents . length > rootPathComponents . length + 1 ) {
289293 // 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 ) ) ;
291295 }
292296 else {
293297 // Always watch root directory non recursively
@@ -304,50 +308,35 @@ export function getDirectoryToWatchFailedLookupLocation(
304308 return getDirectoryToWatchFromFailedLookupLocationDirectory (
305309 failedLookupComponents ,
306310 failedLookupPathComponents ,
311+ perceivedOsRootLength ,
312+ nodeModulesIndex ,
307313 rootPathComponents ,
308314 ) ;
309315}
310316
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-
325317function getDirectoryToWatchFromFailedLookupLocationDirectory (
326318 dirComponents : readonly string [ ] ,
327319 dirPathComponents : Readonly < PathPathComponents > ,
320+ perceivedOsRootLength : number ,
321+ nodeModulesIndex : number ,
328322 rootPathComponents : Readonly < PathPathComponents > ,
329323) : DirectoryOfFailedLookupWatch | undefined {
330324 // 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 ) {
333326 // 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 ) ;
337328 }
338329 // Use some ancestor of the root directory
339330 let nonRecursive = true ;
340331 let length : number | undefined ;
341332 for ( let i = 0 ; i < dirPathComponents . length ; i ++ ) {
342333 if ( dirPathComponents [ i ] !== rootPathComponents [ i ] ) {
343334 nonRecursive = false ;
344- length = i + 1 ;
335+ length = Math . max ( i + 1 , perceivedOsRootLength + 1 ) ;
345336 break ;
346337 }
347338 }
348- return canWatchDirectoryOrFile ( dirPathComponents , length ) ?
349- getDirectoryOfFailedLookupWatch ( dirComponents , dirPathComponents , length , nonRecursive ) :
350- undefined ;
339+ return getDirectoryOfFailedLookupWatch ( dirComponents , dirPathComponents , length , nonRecursive ) ;
351340}
352341
353342function getDirectoryOfFailedLookupWatch (
@@ -378,7 +367,13 @@ export function getDirectoryToWatchFailedLookupLocationFromTypeRoot(
378367 return rootPath ;
379368 }
380369 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+ ) ;
382377 return toWatch && filterCustomPath ( toWatch . dirPath ) ? toWatch . dirPath : undefined ;
383378}
384379
0 commit comments