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