@@ -114,12 +114,10 @@ export interface ResolutionCache extends Required<CompilerHostSupportingResoluti
114
114
countResolutionsResolvedWithGlobalCache ( ) : number ;
115
115
countResolutionsResolvedWithoutGlobalCache ( ) : number ;
116
116
117
- watchFailedLookupLocationsOfExternalModuleResolutions < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
118
- name : string ,
117
+ watchResolution < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
119
118
resolution : T ,
120
119
filePath : Path ,
121
120
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
122
- deferWatchingNonRelativeResolution : boolean ,
123
121
) : void ;
124
122
125
123
resolveModuleNameLiterals (
@@ -180,10 +178,13 @@ export interface ResolutionCache extends Required<CompilerHostSupportingResoluti
180
178
export interface ResolutionWithFailedLookupLocations {
181
179
failedLookupLocations ?: string [ ] ;
182
180
affectingLocations ?: string [ ] ;
181
+ alternateResult ?: string ;
183
182
isInvalidated ?: boolean ;
184
183
// Files that have this resolution using
185
184
files ?: Set < Path > ;
186
- alternateResult ?: string ;
185
+ watchedFailed ?: number ;
186
+ watchedAffected ?: number ;
187
+ setAtRoot ?: boolean ;
187
188
globalCacheResolution ?: boolean ;
188
189
}
189
190
@@ -602,7 +603,6 @@ export function createResolutionCache(
602
603
rootDirForResolution : string ,
603
604
) : ResolutionCache {
604
605
let filesWithInvalidatedResolutions : Set < Path > | undefined ;
605
- const nonRelativeExternalModuleResolutions = new Set < ResolutionWithFailedLookupLocations > ( ) ;
606
606
607
607
const resolutionsWithFailedLookups = new Set < ResolutionWithFailedLookupLocations > ( ) ;
608
608
const resolutionsWithOnlyAffectingLocations = new Set < ResolutionWithFailedLookupLocations > ( ) ;
@@ -680,7 +680,7 @@ export function createResolutionCache(
680
680
dirPathToSymlinkPackageRefCount,
681
681
countResolutionsResolvedWithGlobalCache : ( ) => resolutionsResolvedWithGlobalCache ,
682
682
countResolutionsResolvedWithoutGlobalCache : ( ) => resolutionsResolvedWithoutGlobalCache ,
683
- watchFailedLookupLocationsOfExternalModuleResolutions ,
683
+ watchResolution ,
684
684
getModuleResolutionCache : ( ) => moduleResolutionCache ,
685
685
// perDirectoryResolvedModuleNames and perDirectoryResolvedTypeReferenceDirectives could be non empty if there was exception during program update
686
686
// (between startCachingPerDirectoryResolution and finishCachingPerDirectoryResolution)
@@ -713,7 +713,6 @@ export function createResolutionCache(
713
713
isSymlinkCache . clear ( ) ;
714
714
packageDirWatchers . clear ( ) ;
715
715
dirPathToSymlinkPackageRefCount . clear ( ) ;
716
- nonRelativeExternalModuleResolutions . clear ( ) ;
717
716
closeTypeRootsWatch ( ) ;
718
717
resolvedModuleNames . clear ( ) ;
719
718
resolvedTypeReferenceDirectives . clear ( ) ;
@@ -779,9 +778,6 @@ export function createResolutionCache(
779
778
moduleResolutionCache . clearAllExceptPackageJsonInfoCache ( ) ;
780
779
typeReferenceDirectiveResolutionCache . clearAllExceptPackageJsonInfoCache ( ) ;
781
780
libraryResolutionCache . clearAllExceptPackageJsonInfoCache ( ) ;
782
- // perDirectoryResolvedModuleNames and perDirectoryResolvedTypeReferenceDirectives could be non empty if there was exception during program update
783
- // (between startCachingPerDirectoryResolution and finishCachingPerDirectoryResolution)
784
- watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) ;
785
781
isSymlinkCache . clear ( ) ;
786
782
}
787
783
@@ -803,7 +799,6 @@ export function createResolutionCache(
803
799
resolutionsWithGlobalCachePassAreInvalidated = false ;
804
800
resolutionsWithoutGlobalCachePassAreInvalidated = false ;
805
801
unresolvedResolutionsWithGlobalCachePassAreInvalidated = false ;
806
- watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) ;
807
802
// Update file watches
808
803
if ( newProgram !== oldProgram ) {
809
804
cleanupLibResolutionWatching ( newProgram ) ;
@@ -878,7 +873,6 @@ export function createResolutionCache(
878
873
perFileCache : Map < Path , ModeAwareCache < T > > ;
879
874
loader : ResolutionLoader < Entry , T , SourceFile > ;
880
875
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
881
- deferWatchingNonRelativeResolution : boolean ;
882
876
onNewResolution ?: CallbackOnNewResolution < T > ;
883
877
}
884
878
function resolveNamesWithLocalCache < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
@@ -892,7 +886,6 @@ export function createResolutionCache(
892
886
ambientEntries,
893
887
loader,
894
888
getResolutionWithResolvedFileName,
895
- deferWatchingNonRelativeResolution,
896
889
onNewResolution,
897
890
} : ResolveNamesWithLocalCacheInput < Entry , SourceFile , T , R > ) : readonly T [ ] {
898
891
const path = resolutionHost . toPath ( containingFile ) ;
@@ -929,7 +922,7 @@ export function createResolutionCache(
929
922
}
930
923
resolutionsInFile . set ( name , mode , resolution ) ;
931
924
if ( resolution !== existingResolution ) {
932
- watchFailedLookupLocationsOfExternalModuleResolutions ( name , resolution , path , getResolutionWithResolvedFileName , deferWatchingNonRelativeResolution ) ;
925
+ watchResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
933
926
if ( existingResolution ) {
934
927
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolutionWithResolvedFileName ) ;
935
928
}
@@ -1100,7 +1093,6 @@ export function createResolutionCache(
1100
1093
typeReferenceDirectiveResolutionCache ,
1101
1094
) ,
1102
1095
getResolutionWithResolvedFileName : getResolvedTypeReferenceDirectiveFromResolution ,
1103
- deferWatchingNonRelativeResolution : false ,
1104
1096
} ) ;
1105
1097
}
1106
1098
@@ -1131,7 +1123,6 @@ export function createResolutionCache(
1131
1123
moduleResolutionCache ,
1132
1124
) ,
1133
1125
getResolutionWithResolvedFileName : getResolvedModuleFromResolution ,
1134
- deferWatchingNonRelativeResolution : true , // Defer non relative resolution watch because we could be using ambient modules
1135
1126
onNewResolution,
1136
1127
} ) ;
1137
1128
}
@@ -1148,7 +1139,7 @@ export function createResolutionCache(
1148
1139
const existingResolution = resolution ;
1149
1140
resolution = ts_resolveLibrary ( libraryName , resolveFrom , options , host , libraryResolutionCache ) ;
1150
1141
const path = resolutionHost . toPath ( resolveFrom ) ;
1151
- watchFailedLookupLocationsOfExternalModuleResolutions ( libraryName , resolution , path , getResolvedModuleFromResolution , /*deferWatchingNonRelativeResolution*/ false ) ;
1142
+ watchResolution ( resolution , path , getResolvedModuleFromResolution ) ;
1152
1143
resolvedLibraries . set ( libFileName , resolution ) ;
1153
1144
if ( existingResolution ) {
1154
1145
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolvedModuleFromResolution ) ;
@@ -1197,23 +1188,17 @@ export function createResolutionCache(
1197
1188
return endsWith ( dirPath , "/node_modules/@types" ) ;
1198
1189
}
1199
1190
1200
- function watchFailedLookupLocationsOfExternalModuleResolutions < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1201
- name : string ,
1191
+ function watchResolution < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1202
1192
resolution : T ,
1203
1193
filePath : Path ,
1204
1194
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1205
- deferWatchingNonRelativeResolution : boolean ,
1206
1195
) {
1207
1196
( resolution . files ??= new Set ( ) ) . add ( filePath ) ;
1197
+ watchFailedLookupLocationOfResolution ( resolution ) ;
1198
+ watchAffectingLocationsOfResolution ( resolution ) ;
1208
1199
if ( resolution . files . size !== 1 ) return ;
1209
1200
if ( resolution . globalCacheResolution ) resolutionsResolvedWithGlobalCache ++ ;
1210
1201
else if ( resolution . globalCacheResolution === false ) resolutionsResolvedWithoutGlobalCache ++ ;
1211
- if ( ! deferWatchingNonRelativeResolution || isExternalModuleNameRelative ( name ) ) {
1212
- watchFailedLookupLocationOfResolution ( resolution ) ;
1213
- }
1214
- else {
1215
- nonRelativeExternalModuleResolutions . add ( resolution ) ;
1216
- }
1217
1202
const resolved = getResolutionWithResolvedFileName ( resolution ) ;
1218
1203
if ( resolved && resolved . resolvedFileName ) {
1219
1204
const key = resolutionHost . toPath ( resolved . resolvedFileName ) ;
@@ -1251,33 +1236,38 @@ export function createResolutionCache(
1251
1236
function watchFailedLookupLocationOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1252
1237
Debug . assert ( ! ! resolution . files ?. size ) ;
1253
1238
1254
- const { failedLookupLocations, affectingLocations, alternateResult } = resolution ;
1255
- if ( ! failedLookupLocations ?. length && ! affectingLocations ?. length && ! alternateResult ) return ;
1256
- if ( failedLookupLocations ?. length || alternateResult ) resolutionsWithFailedLookups . add ( resolution ) ;
1239
+ const { failedLookupLocations, alternateResult, watchedFailed } = resolution ;
1240
+ // There have to be failed lookup locations if there is alternateResult so storing failedLookupLocation length is good enough,
1241
+ // alternateResult doesnt change later only failed lookup locations get added on
1242
+ if ( watchedFailed === failedLookupLocations ?. length ) return ;
1243
+ if ( ! watchedFailed ) {
1244
+ resolutionsWithFailedLookups . add ( resolution ) ;
1245
+ if ( resolution . watchedAffected ) resolutionsWithOnlyAffectingLocations . delete ( resolution ) ;
1246
+ }
1257
1247
1258
- let setAtRoot = false ;
1259
- if ( failedLookupLocations ) {
1260
- for ( const failedLookupLocation of failedLookupLocations ) {
1261
- setAtRoot = watchFailedLookupLocation ( failedLookupLocation , setAtRoot ) ;
1262
- }
1248
+ let setAtRoot = ! ! resolution . setAtRoot ;
1249
+ for ( let i = watchedFailed || 0 ; i < failedLookupLocations ! . length ; i ++ ) {
1250
+ setAtRoot = watchFailedLookupLocation ( failedLookupLocations ! [ i ] , setAtRoot ) ;
1263
1251
}
1264
- if ( alternateResult ) setAtRoot = watchFailedLookupLocation ( alternateResult , setAtRoot ) ;
1265
- if ( setAtRoot ) {
1252
+ if ( ! watchedFailed && alternateResult ) setAtRoot = watchFailedLookupLocation ( alternateResult , setAtRoot ) ;
1253
+ if ( ! resolution . setAtRoot && setAtRoot ) {
1266
1254
// This is always non recursive
1267
1255
setDirectoryWatcher ( rootDir , rootPath , /*packageDir*/ undefined , /*packageDirPath*/ undefined , /*nonRecursive*/ true ) ;
1268
1256
}
1269
- watchAffectingLocationsOfResolution ( resolution , ! failedLookupLocations ?. length && ! alternateResult ) ;
1257
+ resolution . watchedFailed = failedLookupLocations ?. length ;
1258
+ resolution . setAtRoot = setAtRoot ;
1270
1259
}
1271
1260
1272
- function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations , addToResolutionsWithOnlyAffectingLocations : boolean ) {
1261
+ function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1273
1262
Debug . assert ( ! ! resolution . files ?. size ) ;
1274
- const { affectingLocations } = resolution ;
1275
- if ( ! affectingLocations ?. length ) return ;
1276
- if ( addToResolutionsWithOnlyAffectingLocations ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1263
+ const { affectingLocations, watchedAffected } = resolution ;
1264
+ if ( affectingLocations ?. length === watchedAffected ) return ;
1265
+ if ( ! watchedAffected && ! resolution . watchedFailed ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1277
1266
// Watch package json
1278
- for ( const affectingLocation of affectingLocations ) {
1279
- createFileWatcherOfAffectingLocation ( affectingLocation , /*forResolution*/ true ) ;
1267
+ for ( let i = watchedAffected || 0 ; i < affectingLocations ! . length ; i ++ ) {
1268
+ createFileWatcherOfAffectingLocation ( affectingLocations ! [ i ] , /*forResolution*/ true ) ;
1280
1269
}
1270
+ resolution . watchedAffected = affectingLocations ?. length ;
1281
1271
}
1282
1272
1283
1273
function createFileWatcherOfAffectingLocation ( affectingLocation : string , forResolution : boolean ) {
@@ -1345,11 +1335,6 @@ export function createResolutionCache(
1345
1335
packageJsonMap ?. delete ( resolutionHost . toPath ( path ) ) ;
1346
1336
}
1347
1337
1348
- function watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) {
1349
- nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfResolution ) ;
1350
- nonRelativeExternalModuleResolutions . clear ( ) ;
1351
- }
1352
-
1353
1338
function createDirectoryWatcherForPackageDir (
1354
1339
dir : string ,
1355
1340
dirPath : Path ,
0 commit comments