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