@@ -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
+ node10Result ?: string ;
183
182
isInvalidated ?: boolean ;
184
183
// Files that have this resolution using
185
184
files ?: Set < Path > ;
186
- node10Result ?: string ;
185
+ watchedFailed ?: number ;
186
+ watchedAffected ?: number ;
187
+ setAtRoot ?: boolean ;
187
188
globalCacheResolution ?: boolean ;
188
189
}
189
190
@@ -554,7 +555,6 @@ export function createResolutionCache(
554
555
rootDirForResolution : string ,
555
556
) : ResolutionCache {
556
557
let filesWithInvalidatedResolutions : Set < Path > | undefined ;
557
- const nonRelativeExternalModuleResolutions = new Set < ResolutionWithFailedLookupLocations > ( ) ;
558
558
559
559
const resolutionsWithFailedLookups = new Set < ResolutionWithFailedLookupLocations > ( ) ;
560
560
const resolutionsWithOnlyAffectingLocations = new Set < ResolutionWithFailedLookupLocations > ( ) ;
@@ -626,7 +626,7 @@ export function createResolutionCache(
626
626
fileWatchesOfAffectingLocations,
627
627
countResolutionsResolvedWithGlobalCache : ( ) => resolutionsResolvedWithGlobalCache ,
628
628
countResolutionsResolvedWithoutGlobalCache : ( ) => resolutionsResolvedWithoutGlobalCache ,
629
- watchFailedLookupLocationsOfExternalModuleResolutions ,
629
+ watchResolution ,
630
630
getModuleResolutionCache : ( ) => moduleResolutionCache ,
631
631
// perDirectoryResolvedModuleNames and perDirectoryResolvedTypeReferenceDirectives could be non empty if there was exception during program update
632
632
// (between startCachingPerDirectoryResolution and finishCachingPerDirectoryResolution)
@@ -656,7 +656,6 @@ export function createResolutionCache(
656
656
function clear ( ) {
657
657
clearMap ( directoryWatchesOfFailedLookups , closeFileWatcherOf ) ;
658
658
clearMap ( fileWatchesOfAffectingLocations , closeFileWatcherOf ) ;
659
- nonRelativeExternalModuleResolutions . clear ( ) ;
660
659
closeTypeRootsWatch ( ) ;
661
660
resolvedModuleNames . clear ( ) ;
662
661
resolvedTypeReferenceDirectives . clear ( ) ;
@@ -724,7 +723,6 @@ export function createResolutionCache(
724
723
libraryResolutionCache . clearAllExceptPackageJsonInfoCache ( ) ;
725
724
// perDirectoryResolvedModuleNames and perDirectoryResolvedTypeReferenceDirectives could be non empty if there was exception during program update
726
725
// (between startCachingPerDirectoryResolution and finishCachingPerDirectoryResolution)
727
- watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) ;
728
726
}
729
727
730
728
function cleanupLibResolutionWatching ( newProgram : Program | undefined ) {
@@ -745,7 +743,6 @@ export function createResolutionCache(
745
743
resolutionsWithGlobalCachePassAreInvalidated = false ;
746
744
resolutionsWithoutGlobalCachePassAreInvalidated = false ;
747
745
unresolvedResolutionsWithGlobalCachePassAreInvalidated = false ;
748
- watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) ;
749
746
// Update file watches
750
747
if ( newProgram !== oldProgram ) {
751
748
cleanupLibResolutionWatching ( newProgram ) ;
@@ -811,7 +808,6 @@ export function createResolutionCache(
811
808
perFileCache : Map < Path , ModeAwareCache < T > > ;
812
809
loader : ResolutionLoader < Entry , T , SourceFile > ;
813
810
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
814
- deferWatchingNonRelativeResolution : boolean ;
815
811
onNewResolution ?: CallbackOnNewResolution < T > ;
816
812
}
817
813
function resolveNamesWithLocalCache < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
@@ -825,7 +821,6 @@ export function createResolutionCache(
825
821
ambientEntries,
826
822
loader,
827
823
getResolutionWithResolvedFileName,
828
- deferWatchingNonRelativeResolution,
829
824
onNewResolution,
830
825
} : ResolveNamesWithLocalCacheInput < Entry , SourceFile , T , R > ) : readonly T [ ] {
831
826
const path = resolutionHost . toPath ( containingFile ) ;
@@ -862,7 +857,7 @@ export function createResolutionCache(
862
857
}
863
858
resolutionsInFile . set ( name , mode , resolution ) ;
864
859
if ( resolution !== existingResolution ) {
865
- watchFailedLookupLocationsOfExternalModuleResolutions ( name , resolution , path , getResolutionWithResolvedFileName , deferWatchingNonRelativeResolution ) ;
860
+ watchResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
866
861
if ( existingResolution ) {
867
862
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolutionWithResolvedFileName ) ;
868
863
}
@@ -1019,7 +1014,6 @@ export function createResolutionCache(
1019
1014
typeReferenceDirectiveResolutionCache ,
1020
1015
) ,
1021
1016
getResolutionWithResolvedFileName : getResolvedTypeReferenceDirectiveFromResolution ,
1022
- deferWatchingNonRelativeResolution : false ,
1023
1017
} ) ;
1024
1018
}
1025
1019
@@ -1050,7 +1044,6 @@ export function createResolutionCache(
1050
1044
moduleResolutionCache ,
1051
1045
) ,
1052
1046
getResolutionWithResolvedFileName : getResolvedModuleFromResolution ,
1053
- deferWatchingNonRelativeResolution : true , // Defer non relative resolution watch because we could be using ambient modules
1054
1047
onNewResolution,
1055
1048
} ) ;
1056
1049
}
@@ -1067,7 +1060,7 @@ export function createResolutionCache(
1067
1060
const existingResolution = resolution ;
1068
1061
resolution = ts_resolveLibrary ( libraryName , resolveFrom , options , host , libraryResolutionCache ) ;
1069
1062
const path = resolutionHost . toPath ( resolveFrom ) ;
1070
- watchFailedLookupLocationsOfExternalModuleResolutions ( libraryName , resolution , path , getResolvedModuleFromResolution , /*deferWatchingNonRelativeResolution*/ false ) ;
1063
+ watchResolution ( resolution , path , getResolvedModuleFromResolution ) ;
1071
1064
resolvedLibraries . set ( libFileName , resolution ) ;
1072
1065
if ( existingResolution ) {
1073
1066
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolvedModuleFromResolution ) ;
@@ -1116,25 +1109,19 @@ export function createResolutionCache(
1116
1109
return endsWith ( dirPath , "/node_modules/@types" ) ;
1117
1110
}
1118
1111
1119
- function watchFailedLookupLocationsOfExternalModuleResolutions < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1120
- name : string ,
1112
+ function watchResolution < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1121
1113
resolution : T ,
1122
1114
filePath : Path ,
1123
1115
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1124
- deferWatchingNonRelativeResolution : boolean ,
1125
1116
) {
1126
1117
( resolution . files ??= new Set ( ) ) . add ( filePath ) ;
1118
+ watchFailedLookupLocationOfResolution ( resolution ) ;
1119
+ watchAffectingLocationsOfResolution ( resolution ) ;
1127
1120
if ( resolution . files . size !== 1 ) return ;
1128
1121
1129
1122
if ( resolution . globalCacheResolution ) resolutionsResolvedWithGlobalCache ++ ;
1130
1123
else if ( resolution . globalCacheResolution === false ) resolutionsResolvedWithoutGlobalCache ++ ;
1131
1124
1132
- if ( ! deferWatchingNonRelativeResolution || isExternalModuleNameRelative ( name ) ) {
1133
- watchFailedLookupLocationOfResolution ( resolution ) ;
1134
- }
1135
- else {
1136
- nonRelativeExternalModuleResolutions . add ( resolution ) ;
1137
- }
1138
1125
const resolved = getResolutionWithResolvedFileName ( resolution ) ;
1139
1126
if ( resolved && resolved . resolvedFileName ) {
1140
1127
const key = resolutionHost . toPath ( resolved . resolvedFileName ) ;
@@ -1170,33 +1157,38 @@ export function createResolutionCache(
1170
1157
function watchFailedLookupLocationOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1171
1158
Debug . assert ( ! ! resolution . files ?. size ) ;
1172
1159
1173
- const { failedLookupLocations, affectingLocations, node10Result } = resolution ;
1174
- if ( ! failedLookupLocations ?. length && ! affectingLocations ?. length && ! node10Result ) return ;
1175
- if ( failedLookupLocations ?. length || node10Result ) resolutionsWithFailedLookups . add ( resolution ) ;
1160
+ const { failedLookupLocations, node10Result, watchedFailed } = resolution ;
1161
+ // There have to be failed lookup locations if there is node10Result so storing failedLookupLocation length is good enough,
1162
+ // node10Result doesnt change later only failed lookup locations get added on
1163
+ if ( watchedFailed === failedLookupLocations ?. length ) return ;
1164
+ if ( ! watchedFailed ) {
1165
+ resolutionsWithFailedLookups . add ( resolution ) ;
1166
+ if ( resolution . watchedAffected ) resolutionsWithOnlyAffectingLocations . delete ( resolution ) ;
1167
+ }
1176
1168
1177
- let setAtRoot = false ;
1178
- if ( failedLookupLocations ) {
1179
- for ( const failedLookupLocation of failedLookupLocations ) {
1180
- setAtRoot = watchFailedLookupLocation ( failedLookupLocation , setAtRoot ) ;
1181
- }
1169
+ let setAtRoot = ! ! resolution . setAtRoot ;
1170
+ for ( let i = watchedFailed || 0 ; i < failedLookupLocations ! . length ; i ++ ) {
1171
+ setAtRoot = watchFailedLookupLocation ( failedLookupLocations ! [ i ] , setAtRoot ) ;
1182
1172
}
1183
- if ( node10Result ) setAtRoot = watchFailedLookupLocation ( node10Result , setAtRoot ) ;
1184
- if ( setAtRoot ) {
1173
+ if ( ! watchedFailed && node10Result ) setAtRoot = watchFailedLookupLocation ( node10Result , setAtRoot ) ;
1174
+ if ( ! resolution . setAtRoot && setAtRoot ) {
1185
1175
// This is always non recursive
1186
1176
setDirectoryWatcher ( rootDir , rootPath , /*nonRecursive*/ true ) ;
1187
1177
}
1188
- watchAffectingLocationsOfResolution ( resolution , ! failedLookupLocations ?. length && ! node10Result ) ;
1178
+ resolution . watchedFailed = failedLookupLocations ?. length ;
1179
+ resolution . setAtRoot = setAtRoot ;
1189
1180
}
1190
1181
1191
- function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations , addToResolutionsWithOnlyAffectingLocations : boolean ) {
1182
+ function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1192
1183
Debug . assert ( ! ! resolution . files ?. size ) ;
1193
- const { affectingLocations } = resolution ;
1194
- if ( ! affectingLocations ?. length ) return ;
1195
- if ( addToResolutionsWithOnlyAffectingLocations ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1184
+ const { affectingLocations, watchedAffected } = resolution ;
1185
+ if ( affectingLocations ?. length === watchedAffected ) return ;
1186
+ if ( ! watchedAffected && ! resolution . watchedFailed ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1196
1187
// Watch package json
1197
- for ( const affectingLocation of affectingLocations ) {
1198
- createFileWatcherOfAffectingLocation ( affectingLocation , /*forResolution*/ true ) ;
1188
+ for ( let i = watchedAffected || 0 ; i < affectingLocations ! . length ; i ++ ) {
1189
+ createFileWatcherOfAffectingLocation ( affectingLocations ! [ i ] , /*forResolution*/ true ) ;
1199
1190
}
1191
+ resolution . watchedAffected = affectingLocations ?. length ;
1200
1192
}
1201
1193
1202
1194
function createFileWatcherOfAffectingLocation ( affectingLocation : string , forResolution : boolean ) {
@@ -1264,11 +1256,6 @@ export function createResolutionCache(
1264
1256
packageJsonMap ?. delete ( resolutionHost . toPath ( path ) ) ;
1265
1257
}
1266
1258
1267
- function watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) {
1268
- nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfResolution ) ;
1269
- nonRelativeExternalModuleResolutions . clear ( ) ;
1270
- }
1271
-
1272
1259
function setDirectoryWatcher ( dir : string , dirPath : Path , nonRecursive ?: boolean ) {
1273
1260
const dirWatcher = directoryWatchesOfFailedLookups . get ( dirPath ) ;
1274
1261
if ( dirWatcher ) {
0 commit comments