@@ -125,6 +125,7 @@ export interface ResolutionCache {
125
125
getModuleResolutionCache ( ) : ModuleResolutionCache ;
126
126
127
127
clear ( ) : void ;
128
+ onChangesAffectModuleResolution ( ) : void ;
128
129
}
129
130
130
131
/** @internal */
@@ -412,6 +413,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
412
413
let failedLookupChecks : Set < Path > | undefined ;
413
414
let startsWithPathChecks : Set < Path > | undefined ;
414
415
let isInDirectoryChecks : Set < Path > | undefined ;
416
+ let allResolutionsAreInvalidated = false ;
415
417
416
418
const getCurrentDirectory = memoize ( ( ) => resolutionHost . getCurrentDirectory ! ( ) ) ; // TODO: GH#18217
417
419
const cachedDirectoryStructureHost = resolutionHost . getCachedDirectoryStructureHost ( ) ;
@@ -464,7 +466,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
464
466
isFileWithInvalidatedNonRelativeUnresolvedImports,
465
467
updateTypeRootsWatch,
466
468
closeTypeRootsWatch,
467
- clear
469
+ clear,
470
+ onChangesAffectModuleResolution,
468
471
} ;
469
472
470
473
function getResolvedModule ( resolution : CachedResolvedModuleWithFailedLookupLocations ) {
@@ -490,6 +493,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
490
493
isInDirectoryChecks = undefined ;
491
494
affectingPathChecks = undefined ;
492
495
affectingPathChecksForFile = undefined ;
496
+ allResolutionsAreInvalidated = false ;
493
497
moduleResolutionCache . clear ( ) ;
494
498
typeReferenceDirectiveResolutionCache . clear ( ) ;
495
499
moduleResolutionCache . update ( resolutionHost . getCompilationSettings ( ) ) ;
@@ -498,6 +502,14 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
498
502
hasChangedAutomaticTypeDirectiveNames = false ;
499
503
}
500
504
505
+ function onChangesAffectModuleResolution ( ) {
506
+ allResolutionsAreInvalidated = true ;
507
+ moduleResolutionCache . clearAllExceptPackageJsonInfoCache ( ) ;
508
+ typeReferenceDirectiveResolutionCache . clearAllExceptPackageJsonInfoCache ( ) ;
509
+ moduleResolutionCache . update ( resolutionHost . getCompilationSettings ( ) ) ;
510
+ typeReferenceDirectiveResolutionCache . update ( resolutionHost . getCompilationSettings ( ) ) ;
511
+ }
512
+
501
513
function startRecordingFilesWithChangedResolutions ( ) {
502
514
filesWithChangedSetOfUnresolvedImports = [ ] ;
503
515
}
@@ -524,6 +536,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
524
536
const collected = filesWithInvalidatedResolutions ;
525
537
filesWithInvalidatedResolutions = undefined ;
526
538
return path => customHasInvalidatedResolutions ( path ) ||
539
+ allResolutionsAreInvalidated ||
527
540
! ! collected ?. has ( path ) ||
528
541
isFileWithInvalidatedNonRelativeUnresolvedImports ( path ) ;
529
542
}
@@ -539,6 +552,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
539
552
540
553
function finishCachingPerDirectoryResolution ( newProgram : Program | undefined , oldProgram : Program | undefined ) {
541
554
filesWithInvalidatedNonRelativeUnresolvedImports = undefined ;
555
+ allResolutionsAreInvalidated = false ;
542
556
nonRelativeExternalModuleResolutions . forEach ( watchFailedLookupLocationOfNonRelativeModuleResolutions ) ;
543
557
nonRelativeExternalModuleResolutions . clear ( ) ;
544
558
// Update file watches
@@ -671,9 +685,9 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
671
685
let resolution = resolutionsInFile . get ( name , mode ) ;
672
686
// Resolution is valid if it is present and not invalidated
673
687
if ( ! seenNamesInFile . has ( name , mode ) &&
674
- unmatchedRedirects || ! resolution || resolution . isInvalidated ||
675
- // If the name is unresolved import that was invalidated, recalculate
676
- ( hasInvalidatedNonRelativeUnresolvedImport && ! isExternalModuleNameRelative ( name ) && shouldRetryResolution ( resolution ) ) ) {
688
+ ( allResolutionsAreInvalidated || unmatchedRedirects || ! resolution || resolution . isInvalidated ||
689
+ // If the name is unresolved import that was invalidated, recalculate
690
+ ( hasInvalidatedNonRelativeUnresolvedImport && ! isExternalModuleNameRelative ( name ) && shouldRetryResolution ( resolution ) ) ) ) {
677
691
const existingResolution = resolution ;
678
692
resolution = loader . resolve ( name , mode ) ;
679
693
if ( resolutionHost . onDiscoveredSymlink && resolutionIsSymlink ( resolution ) ) {
@@ -694,7 +708,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
694
708
else {
695
709
const host = resolutionHost . getCompilerHost ?.( ) || resolutionHost ;
696
710
if ( isTraceEnabled ( options , host ) && ! seenNamesInFile . has ( name , mode ) ) {
697
- const resolved = getResolutionWithResolvedFileName ( resolution ) ;
711
+ const resolved = getResolutionWithResolvedFileName ( resolution ! ) ;
698
712
trace (
699
713
host ,
700
714
perFileCache === resolvedModuleNames as unknown ?
@@ -1163,7 +1177,23 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1163
1177
resolutionHost . scheduleInvalidateResolutionsOfFailedLookupLocations ( ) ;
1164
1178
}
1165
1179
1180
+ function invalidatePackageJsonMap ( ) {
1181
+ const packageJsonMap = moduleResolutionCache . getPackageJsonInfoCache ( ) . getInternalMap ( ) ;
1182
+ if ( packageJsonMap && ( failedLookupChecks || startsWithPathChecks || isInDirectoryChecks ) ) {
1183
+ packageJsonMap . forEach ( ( _value , path ) => isInvalidatedFailedLookup ( path ) ? packageJsonMap . delete ( path ) : undefined ) ;
1184
+ }
1185
+ }
1186
+
1166
1187
function invalidateResolutionsOfFailedLookupLocations ( ) {
1188
+ if ( allResolutionsAreInvalidated ) {
1189
+ affectingPathChecksForFile = undefined ;
1190
+ invalidatePackageJsonMap ( ) ;
1191
+ failedLookupChecks = undefined ;
1192
+ startsWithPathChecks = undefined ;
1193
+ isInDirectoryChecks = undefined ;
1194
+ affectingPathChecks = undefined ;
1195
+ return true ;
1196
+ }
1167
1197
let invalidated = false ;
1168
1198
if ( affectingPathChecksForFile ) {
1169
1199
resolutionHost . getCurrentProgram ( ) ?. getSourceFiles ( ) . forEach ( f => {
@@ -1180,10 +1210,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
1180
1210
}
1181
1211
1182
1212
invalidated = invalidateResolutions ( resolutionsWithFailedLookups , canInvalidateFailedLookupResolution ) || invalidated ;
1183
- const packageJsonMap = moduleResolutionCache . getPackageJsonInfoCache ( ) . getInternalMap ( ) ;
1184
- if ( packageJsonMap && ( failedLookupChecks || startsWithPathChecks || isInDirectoryChecks ) ) {
1185
- packageJsonMap . forEach ( ( _value , path ) => isInvalidatedFailedLookup ( path ) ? packageJsonMap . delete ( path ) : undefined ) ;
1186
- }
1213
+ invalidatePackageJsonMap ( ) ;
1187
1214
failedLookupChecks = undefined ;
1188
1215
startsWithPathChecks = undefined ;
1189
1216
isInDirectoryChecks = undefined ;
0 commit comments