@@ -106,12 +106,10 @@ export interface ResolutionCache extends Required<CompilerHostSupportingResoluti
106
106
startRecordingFilesWithChangedResolutions ( ) : void ;
107
107
finishRecordingFilesWithChangedResolutions ( ) : Path [ ] | undefined ;
108
108
109
- watchFailedLookupLocationsOfExternalModuleResolutions < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
110
- name : string ,
109
+ watchResolution < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
111
110
resolution : T ,
112
111
filePath : Path ,
113
112
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
114
- deferWatchingNonRelativeResolution : boolean ,
115
113
) : void ;
116
114
117
115
resolveModuleNameLiterals (
@@ -171,10 +169,13 @@ export interface ResolutionCache extends Required<CompilerHostSupportingResoluti
171
169
export interface ResolutionWithFailedLookupLocations {
172
170
failedLookupLocations ?: string [ ] ;
173
171
affectingLocations ?: string [ ] ;
172
+ node10Result ?: string ;
174
173
isInvalidated ?: boolean ;
175
174
// Files that have this resolution using
176
175
files ?: Set < Path > ;
177
- node10Result ?: string ;
176
+ watchedFailed ?: number ;
177
+ watchedAffected ?: number ;
178
+ setAtRoot ?: boolean ;
178
179
}
179
180
180
181
/** @internal */
@@ -501,7 +502,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
501
502
let filesWithChangedSetOfUnresolvedImports : Path [ ] | undefined ;
502
503
let filesWithInvalidatedResolutions : Set < Path > | undefined ;
503
504
let filesWithInvalidatedNonRelativeUnresolvedImports : ReadonlyMap < Path , readonly string [ ] > | undefined ;
504
- const nonRelativeExternalModuleResolutions = new Set < ResolutionWithFailedLookupLocations > ( ) ;
505
505
506
506
const resolutionsWithFailedLookups = new Set < ResolutionWithFailedLookupLocations > ( ) ;
507
507
const resolutionsWithOnlyAffectingLocations = new Set < ResolutionWithFailedLookupLocations > ( ) ;
@@ -564,7 +564,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
564
564
resolutionsWithOnlyAffectingLocations,
565
565
directoryWatchesOfFailedLookups,
566
566
fileWatchesOfAffectingLocations,
567
- watchFailedLookupLocationsOfExternalModuleResolutions ,
567
+ watchResolution ,
568
568
getModuleResolutionCache : ( ) => moduleResolutionCache ,
569
569
startRecordingFilesWithChangedResolutions,
570
570
finishRecordingFilesWithChangedResolutions,
@@ -603,7 +603,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
603
603
function clear ( ) {
604
604
clearMap ( directoryWatchesOfFailedLookups , closeFileWatcherOf ) ;
605
605
clearMap ( fileWatchesOfAffectingLocations , closeFileWatcherOf ) ;
606
- nonRelativeExternalModuleResolutions . clear ( ) ;
607
606
closeTypeRootsWatch ( ) ;
608
607
resolvedModuleNames . clear ( ) ;
609
608
resolvedTypeReferenceDirectives . clear ( ) ;
@@ -678,7 +677,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
678
677
libraryResolutionCache . clearAllExceptPackageJsonInfoCache ( ) ;
679
678
// perDirectoryResolvedModuleNames and perDirectoryResolvedTypeReferenceDirectives could be non empty if there was exception during program update
680
679
// (between startCachingPerDirectoryResolution and finishCachingPerDirectoryResolution)
681
- watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) ;
682
680
}
683
681
684
682
function cleanupLibResolutionWatching ( newProgram : Program | undefined ) {
@@ -697,7 +695,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
697
695
function finishCachingPerDirectoryResolution ( newProgram : Program | undefined , oldProgram : Program | undefined ) {
698
696
filesWithInvalidatedNonRelativeUnresolvedImports = undefined ;
699
697
allModuleAndTypeResolutionsAreInvalidated = false ;
700
- watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) ;
701
698
// Update file watches
702
699
if ( newProgram !== oldProgram ) {
703
700
cleanupLibResolutionWatching ( newProgram ) ;
@@ -750,12 +747,11 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
750
747
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
751
748
shouldRetryResolution : ( t : T ) => boolean ;
752
749
logChanges ?: boolean ;
753
- deferWatchingNonRelativeResolution : boolean ;
754
750
}
755
751
function resolveNamesWithLocalCache < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
756
752
entries, containingFile, containingSourceFile, redirectedReference, options,
757
753
perFileCache, reusedNames, ambientModuleNames,
758
- loader, getResolutionWithResolvedFileName, deferWatchingNonRelativeResolution ,
754
+ loader, getResolutionWithResolvedFileName,
759
755
shouldRetryResolution, logChanges,
760
756
} : ResolveNamesWithLocalCacheInput < Entry , SourceFile , T , R > ) : readonly T [ ] {
761
757
const path = resolutionHost . toPath ( containingFile ) ;
@@ -786,7 +782,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
786
782
resolutionHost . onDiscoveredSymlink ( ) ;
787
783
}
788
784
resolutionsInFile . set ( name , mode , resolution ) ;
789
- watchFailedLookupLocationsOfExternalModuleResolutions ( name , resolution , path , getResolutionWithResolvedFileName , deferWatchingNonRelativeResolution ) ;
785
+ watchResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
790
786
if ( existingResolution ) {
791
787
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolutionWithResolvedFileName ) ;
792
788
}
@@ -881,7 +877,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
881
877
) ,
882
878
getResolutionWithResolvedFileName : getResolvedTypeReferenceDirective ,
883
879
shouldRetryResolution : resolution => resolution . resolvedTypeReferenceDirective === undefined ,
884
- deferWatchingNonRelativeResolution : false ,
885
880
} ) ;
886
881
}
887
882
@@ -913,7 +908,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
913
908
getResolutionWithResolvedFileName : getResolvedModule ,
914
909
shouldRetryResolution : resolution => ! resolution . resolvedModule || ! resolutionExtensionIsTSOrJson ( resolution . resolvedModule . extension ) ,
915
910
logChanges : logChangesWhenResolvingModule ,
916
- deferWatchingNonRelativeResolution : true , // Defer non relative resolution watch because we could be using ambient modules
917
911
} ) ;
918
912
}
919
913
@@ -929,7 +923,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
929
923
const existingResolution = resolution ;
930
924
resolution = ts_resolveLibrary ( libraryName , resolveFrom , options , host , libraryResolutionCache ) ;
931
925
const path = resolutionHost . toPath ( resolveFrom ) ;
932
- watchFailedLookupLocationsOfExternalModuleResolutions ( libraryName , resolution , path , getResolvedModule , /*deferWatchingNonRelativeResolution*/ false ) ;
926
+ watchResolution ( resolution , path , getResolvedModule ) ;
933
927
resolvedLibraries . set ( libFileName , resolution ) ;
934
928
if ( existingResolution ) {
935
929
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolvedModule ) ;
@@ -1060,21 +1054,15 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1060
1054
return endsWith ( dirPath , "/node_modules/@types" ) ;
1061
1055
}
1062
1056
1063
- function watchFailedLookupLocationsOfExternalModuleResolutions < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1064
- name : string ,
1057
+ function watchResolution < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1065
1058
resolution : T ,
1066
1059
filePath : Path ,
1067
1060
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1068
- deferWatchingNonRelativeResolution : boolean ,
1069
1061
) {
1070
1062
( resolution . files ??= new Set ( ) ) . add ( filePath ) ;
1063
+ watchFailedLookupLocationOfResolution ( resolution ) ;
1064
+ watchAffectingLocationsOfResolution ( resolution ) ;
1071
1065
if ( resolution . files . size !== 1 ) return ;
1072
- if ( ! deferWatchingNonRelativeResolution || isExternalModuleNameRelative ( name ) ) {
1073
- watchFailedLookupLocationOfResolution ( resolution ) ;
1074
- }
1075
- else {
1076
- nonRelativeExternalModuleResolutions . add ( resolution ) ;
1077
- }
1078
1066
const resolved = getResolutionWithResolvedFileName ( resolution ) ;
1079
1067
if ( resolved && resolved . resolvedFileName ) {
1080
1068
const key = resolutionHost . toPath ( resolved . resolvedFileName ) ;
@@ -1110,33 +1098,38 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1110
1098
function watchFailedLookupLocationOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1111
1099
Debug . assert ( ! ! resolution . files ?. size ) ;
1112
1100
1113
- const { failedLookupLocations, affectingLocations, node10Result } = resolution ;
1114
- if ( ! failedLookupLocations ?. length && ! affectingLocations ?. length && ! node10Result ) return ;
1115
- if ( failedLookupLocations ?. length || node10Result ) resolutionsWithFailedLookups . add ( resolution ) ;
1101
+ const { failedLookupLocations, node10Result, watchedFailed } = resolution ;
1102
+ // There have to be failed lookup locations if there is node10Result so storing failedLookupLocation length is good enough,
1103
+ // node10Result doesnt change later only failed lookup locations get added on
1104
+ if ( watchedFailed === failedLookupLocations ?. length ) return ;
1105
+ if ( ! watchedFailed ) {
1106
+ resolutionsWithFailedLookups . add ( resolution ) ;
1107
+ if ( resolution . watchedAffected ) resolutionsWithOnlyAffectingLocations . delete ( resolution ) ;
1108
+ }
1116
1109
1117
- let setAtRoot = false ;
1118
- if ( failedLookupLocations ) {
1119
- for ( const failedLookupLocation of failedLookupLocations ) {
1120
- setAtRoot = watchFailedLookupLocation ( failedLookupLocation , setAtRoot ) ;
1121
- }
1110
+ let setAtRoot = ! ! resolution . setAtRoot ;
1111
+ for ( let i = watchedFailed || 0 ; i < failedLookupLocations ! . length ; i ++ ) {
1112
+ setAtRoot = watchFailedLookupLocation ( failedLookupLocations ! [ i ] , setAtRoot ) ;
1122
1113
}
1123
- if ( node10Result ) setAtRoot = watchFailedLookupLocation ( node10Result , setAtRoot ) ;
1124
- if ( setAtRoot ) {
1114
+ if ( ! watchedFailed && node10Result ) setAtRoot = watchFailedLookupLocation ( node10Result , setAtRoot ) ;
1115
+ if ( ! resolution . setAtRoot && setAtRoot ) {
1125
1116
// This is always non recursive
1126
1117
setDirectoryWatcher ( rootDir , rootPath , /*nonRecursive*/ true ) ;
1127
1118
}
1128
- watchAffectingLocationsOfResolution ( resolution , ! failedLookupLocations ?. length && ! node10Result ) ;
1119
+ resolution . watchedFailed = failedLookupLocations ?. length ;
1120
+ resolution . setAtRoot = setAtRoot ;
1129
1121
}
1130
1122
1131
- function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations , addToResolutionsWithOnlyAffectingLocations : boolean ) {
1123
+ function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1132
1124
Debug . assert ( ! ! resolution . files ?. size ) ;
1133
- const { affectingLocations } = resolution ;
1134
- if ( ! affectingLocations ?. length ) return ;
1135
- if ( addToResolutionsWithOnlyAffectingLocations ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1125
+ const { affectingLocations, watchedAffected } = resolution ;
1126
+ if ( affectingLocations ?. length === watchedAffected ) return ;
1127
+ if ( ! watchedAffected && ! resolution . watchedFailed ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1136
1128
// Watch package json
1137
- for ( const affectingLocation of affectingLocations ) {
1138
- createFileWatcherOfAffectingLocation ( affectingLocation , /*forResolution*/ true ) ;
1129
+ for ( let i = watchedAffected || 0 ; i < affectingLocations ! . length ; i ++ ) {
1130
+ createFileWatcherOfAffectingLocation ( affectingLocations ! [ i ] , /*forResolution*/ true ) ;
1139
1131
}
1132
+ resolution . watchedAffected = affectingLocations ?. length ;
1140
1133
}
1141
1134
1142
1135
function createFileWatcherOfAffectingLocation ( affectingLocation : string , forResolution : boolean ) {
@@ -1204,11 +1197,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1204
1197
packageJsonMap ?. delete ( resolutionHost . toPath ( path ) ) ;
1205
1198
}
1206
1199
1207
- function watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) {
1208
- nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfResolution ) ;
1209
- nonRelativeExternalModuleResolutions . clear ( ) ;
1210
- }
1211
-
1212
1200
function setDirectoryWatcher ( dir : string , dirPath : Path , nonRecursive ?: boolean ) {
1213
1201
const dirWatcher = directoryWatchesOfFailedLookups . get ( dirPath ) ;
1214
1202
if ( dirWatcher ) {
0 commit comments