@@ -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 ) ;
@@ -754,12 +751,11 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
754
751
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
755
752
shouldRetryResolution : ( t : T ) => boolean ;
756
753
logChanges ?: boolean ;
757
- deferWatchingNonRelativeResolution : boolean ;
758
754
}
759
755
function resolveNamesWithLocalCache < Entry , SourceFile , T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
760
756
entries, containingFile, containingSourceFile, redirectedReference, options,
761
757
perFileCache, reusedNames, ambientModuleNames,
762
- loader, getResolutionWithResolvedFileName, deferWatchingNonRelativeResolution ,
758
+ loader, getResolutionWithResolvedFileName,
763
759
shouldRetryResolution, logChanges,
764
760
} : ResolveNamesWithLocalCacheInput < Entry , SourceFile , T , R > ) : readonly T [ ] {
765
761
const path = resolutionHost . toPath ( containingFile ) ;
@@ -791,7 +787,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
791
787
}
792
788
resolutionsInFile . set ( name , mode , resolution ) ;
793
789
if ( resolution !== existingResolution ) {
794
- watchFailedLookupLocationsOfExternalModuleResolutions ( name , resolution , path , getResolutionWithResolvedFileName , deferWatchingNonRelativeResolution ) ;
790
+ watchResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
795
791
if ( existingResolution ) {
796
792
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolutionWithResolvedFileName ) ;
797
793
}
@@ -887,7 +883,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
887
883
) ,
888
884
getResolutionWithResolvedFileName : getResolvedTypeReferenceDirective ,
889
885
shouldRetryResolution : resolution => resolution . resolvedTypeReferenceDirective === undefined ,
890
- deferWatchingNonRelativeResolution : false ,
891
886
} ) ;
892
887
}
893
888
@@ -919,7 +914,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
919
914
getResolutionWithResolvedFileName : getResolvedModule ,
920
915
shouldRetryResolution : resolution => ! resolution . resolvedModule || ! resolutionExtensionIsTSOrJson ( resolution . resolvedModule . extension ) ,
921
916
logChanges : logChangesWhenResolvingModule ,
922
- deferWatchingNonRelativeResolution : true , // Defer non relative resolution watch because we could be using ambient modules
923
917
} ) ;
924
918
}
925
919
@@ -935,7 +929,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
935
929
const existingResolution = resolution ;
936
930
resolution = ts_resolveLibrary ( libraryName , resolveFrom , options , host , libraryResolutionCache ) ;
937
931
const path = resolutionHost . toPath ( resolveFrom ) ;
938
- watchFailedLookupLocationsOfExternalModuleResolutions ( libraryName , resolution , path , getResolvedModule , /*deferWatchingNonRelativeResolution*/ false ) ;
932
+ watchResolution ( resolution , path , getResolvedModule ) ;
939
933
resolvedLibraries . set ( libFileName , resolution ) ;
940
934
if ( existingResolution ) {
941
935
stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolvedModule ) ;
@@ -1066,21 +1060,15 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1066
1060
return endsWith ( dirPath , "/node_modules/@types" ) ;
1067
1061
}
1068
1062
1069
- function watchFailedLookupLocationsOfExternalModuleResolutions < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1070
- name : string ,
1063
+ function watchResolution < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1071
1064
resolution : T ,
1072
1065
filePath : Path ,
1073
1066
getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1074
- deferWatchingNonRelativeResolution : boolean ,
1075
1067
) {
1076
1068
( resolution . files ??= new Set ( ) ) . add ( filePath ) ;
1069
+ watchFailedLookupLocationOfResolution ( resolution ) ;
1070
+ watchAffectingLocationsOfResolution ( resolution ) ;
1077
1071
if ( resolution . files . size !== 1 ) return ;
1078
- if ( ! deferWatchingNonRelativeResolution || isExternalModuleNameRelative ( name ) ) {
1079
- watchFailedLookupLocationOfResolution ( resolution ) ;
1080
- }
1081
- else {
1082
- nonRelativeExternalModuleResolutions . add ( resolution ) ;
1083
- }
1084
1072
const resolved = getResolutionWithResolvedFileName ( resolution ) ;
1085
1073
if ( resolved && resolved . resolvedFileName ) {
1086
1074
const key = resolutionHost . toPath ( resolved . resolvedFileName ) ;
@@ -1116,33 +1104,38 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1116
1104
function watchFailedLookupLocationOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1117
1105
Debug . assert ( ! ! resolution . files ?. size ) ;
1118
1106
1119
- const { failedLookupLocations, affectingLocations, node10Result } = resolution ;
1120
- if ( ! failedLookupLocations ?. length && ! affectingLocations ?. length && ! node10Result ) return ;
1121
- if ( failedLookupLocations ?. length || node10Result ) resolutionsWithFailedLookups . add ( resolution ) ;
1107
+ const { failedLookupLocations, node10Result, watchedFailed } = resolution ;
1108
+ // There have to be failed lookup locations if there is node10Result so storing failedLookupLocation length is good enough,
1109
+ // node10Result doesnt change later only failed lookup locations get added on
1110
+ if ( watchedFailed === failedLookupLocations ?. length ) return ;
1111
+ if ( ! watchedFailed ) {
1112
+ resolutionsWithFailedLookups . add ( resolution ) ;
1113
+ if ( resolution . watchedAffected ) resolutionsWithOnlyAffectingLocations . delete ( resolution ) ;
1114
+ }
1122
1115
1123
- let setAtRoot = false ;
1124
- if ( failedLookupLocations ) {
1125
- for ( const failedLookupLocation of failedLookupLocations ) {
1126
- setAtRoot = watchFailedLookupLocation ( failedLookupLocation , setAtRoot ) ;
1127
- }
1116
+ let setAtRoot = ! ! resolution . setAtRoot ;
1117
+ for ( let i = watchedFailed || 0 ; i < failedLookupLocations ! . length ; i ++ ) {
1118
+ setAtRoot = watchFailedLookupLocation ( failedLookupLocations ! [ i ] , setAtRoot ) ;
1128
1119
}
1129
- if ( node10Result ) setAtRoot = watchFailedLookupLocation ( node10Result , setAtRoot ) ;
1130
- if ( setAtRoot ) {
1120
+ if ( ! watchedFailed && node10Result ) setAtRoot = watchFailedLookupLocation ( node10Result , setAtRoot ) ;
1121
+ if ( ! resolution . setAtRoot && setAtRoot ) {
1131
1122
// This is always non recursive
1132
1123
setDirectoryWatcher ( rootDir , rootPath , /*nonRecursive*/ true ) ;
1133
1124
}
1134
- watchAffectingLocationsOfResolution ( resolution , ! failedLookupLocations ?. length && ! node10Result ) ;
1125
+ resolution . watchedFailed = failedLookupLocations ?. length ;
1126
+ resolution . setAtRoot = setAtRoot ;
1135
1127
}
1136
1128
1137
- function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations , addToResolutionsWithOnlyAffectingLocations : boolean ) {
1129
+ function watchAffectingLocationsOfResolution ( resolution : ResolutionWithFailedLookupLocations ) {
1138
1130
Debug . assert ( ! ! resolution . files ?. size ) ;
1139
- const { affectingLocations } = resolution ;
1140
- if ( ! affectingLocations ?. length ) return ;
1141
- if ( addToResolutionsWithOnlyAffectingLocations ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1131
+ const { affectingLocations, watchedAffected } = resolution ;
1132
+ if ( affectingLocations ?. length === watchedAffected ) return ;
1133
+ if ( ! watchedAffected && ! resolution . watchedFailed ) resolutionsWithOnlyAffectingLocations . add ( resolution ) ;
1142
1134
// Watch package json
1143
- for ( const affectingLocation of affectingLocations ) {
1144
- createFileWatcherOfAffectingLocation ( affectingLocation , /*forResolution*/ true ) ;
1135
+ for ( let i = watchedAffected || 0 ; i < affectingLocations ! . length ; i ++ ) {
1136
+ createFileWatcherOfAffectingLocation ( affectingLocations ! [ i ] , /*forResolution*/ true ) ;
1145
1137
}
1138
+ resolution . watchedAffected = affectingLocations ?. length ;
1146
1139
}
1147
1140
1148
1141
function createFileWatcherOfAffectingLocation ( affectingLocation : string , forResolution : boolean ) {
@@ -1210,11 +1203,6 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1210
1203
packageJsonMap ?. delete ( resolutionHost . toPath ( path ) ) ;
1211
1204
}
1212
1205
1213
- function watchFailedLookupLocationOfNonRelativeModuleResolutions ( ) {
1214
- nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfResolution ) ;
1215
- nonRelativeExternalModuleResolutions . clear ( ) ;
1216
- }
1217
-
1218
1206
function setDirectoryWatcher ( dir : string , dirPath : Path , nonRecursive ?: boolean ) {
1219
1207
const dirWatcher = directoryWatchesOfFailedLookups . get ( dirPath ) ;
1220
1208
if ( dirWatcher ) {
0 commit comments