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