@@ -99,6 +99,8 @@ export interface ResolutionCache {
9999 fileWatchesOfAffectingLocations : Map < string , FileWatcherOfAffectingLocation > ;
100100 packageDirWatchers : Map < Path , PackageDirWatcher > ;
101101 dirPathToSymlinkPackageRefCount : Map < Path , number > ;
102+ countResolutionsResolvedWithGlobalCache ( ) : number ;
103+ countResolutionsResolvedWithoutGlobalCache ( ) : number ;
102104 startRecordingFilesWithChangedResolutions ( ) : void ;
103105 finishRecordingFilesWithChangedResolutions ( ) : Path [ ] | undefined ;
104106
@@ -138,6 +140,8 @@ export interface ResolutionCache {
138140 ) : ResolvedModuleWithFailedLookupLocations ;
139141
140142 invalidateResolutionsOfFailedLookupLocations ( ) : boolean ;
143+ invalidateResolutionsWithGlobalCachePass ( ) : void ;
144+ invalidateResolutionsWithoutGlobalCachePass ( ) : void ;
141145 invalidateResolutionOfFile ( filePath : Path ) : void ;
142146 removeResolutionsOfFile ( filePath : Path ) : void ;
143147 removeResolutionsFromProjectReferenceRedirects ( filePath : Path ) : void ;
@@ -169,6 +173,7 @@ export interface ResolutionWithFailedLookupLocations {
169173 // Files that have this resolution using
170174 files ?: Set < Path > ;
171175 alternateResult ?: string ;
176+ globalCacheResolution ?: ResolvedModuleWithFailedLookupLocations | false ;
172177}
173178
174179/** @internal */
@@ -537,29 +542,33 @@ function resolveModuleNameUsingGlobalCache(
537542 const host = getModuleResolutionHost ( resolutionHost ) ;
538543 const primaryResult = ts_resolveModuleName ( moduleName , containingFile , compilerOptions , host , moduleResolutionCache , redirectedReference , mode ) ;
539544 // return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts
540- if ( ! resolutionHost . getGlobalTypingsCacheLocation ) {
545+ if ( ! resolutionHost . getGlobalTypingsCacheLocation || primaryResult . globalCacheResolution !== undefined ) {
541546 return primaryResult ;
542547 }
543548
544549 // otherwise try to load typings from @types
545550 const globalCache = resolutionHost . getGlobalTypingsCacheLocation ( ) ;
546- if ( globalCache !== undefined && ! isExternalModuleNameRelative ( moduleName ) && ! ( primaryResult . resolvedModule && extensionIsTS ( primaryResult . resolvedModule . extension ) ) ) {
551+ if ( ! isExternalModuleNameRelative ( moduleName ) && ! ( primaryResult . resolvedModule && extensionIsTS ( primaryResult . resolvedModule . extension ) ) ) {
552+ if ( globalCache === undefined ) {
553+ primaryResult . globalCacheResolution = false ;
554+ return primaryResult ;
555+ }
547556 // create different collection of failed lookup locations for second pass
548557 // if it will fail and we've already found something during the first pass - we don't want to pollute its results
549- const { resolvedModule , failedLookupLocations , affectingLocations , resolutionDiagnostics } = loadModuleFromGlobalCache (
558+ primaryResult . globalCacheResolution = loadModuleFromGlobalCache (
550559 Debug . checkDefined ( resolutionHost . globalCacheResolutionModuleName ) ( moduleName ) ,
551560 resolutionHost . projectName ,
552561 compilerOptions ,
553562 host ,
554563 globalCache ,
555564 moduleResolutionCache ,
556565 ) ;
557- if ( resolvedModule ) {
566+ if ( primaryResult . globalCacheResolution . resolvedModule ) {
558567 // Modify existing resolution so its saved in the directory cache as well
559- ( primaryResult . resolvedModule as any ) = resolvedModule ;
560- primaryResult . failedLookupLocations = updateResolutionField ( primaryResult . failedLookupLocations , failedLookupLocations ) ;
561- primaryResult . affectingLocations = updateResolutionField ( primaryResult . affectingLocations , affectingLocations ) ;
562- primaryResult . resolutionDiagnostics = updateResolutionField ( primaryResult . resolutionDiagnostics , resolutionDiagnostics ) ;
568+ ( primaryResult . resolvedModule as any ) = primaryResult . globalCacheResolution . resolvedModule ;
569+ primaryResult . failedLookupLocations = updateResolutionField ( primaryResult . failedLookupLocations , primaryResult . globalCacheResolution . failedLookupLocations ) ;
570+ primaryResult . affectingLocations = updateResolutionField ( primaryResult . affectingLocations , primaryResult . globalCacheResolution . affectingLocations ) ;
571+ primaryResult . resolutionDiagnostics = updateResolutionField ( primaryResult . resolutionDiagnostics , primaryResult . globalCacheResolution . resolutionDiagnostics ) ;
563572 return primaryResult ;
564573 }
565574 }
@@ -572,7 +581,10 @@ function resolveModuleNameUsingGlobalCache(
572581export type GetResolutionWithResolvedFileName < T extends ResolutionWithFailedLookupLocations = ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName = ResolutionWithResolvedFileName > = ( resolution : T ) => R | undefined ;
573582
574583/** @internal */
575- export function createResolutionCache ( resolutionHost : ResolutionCacheHost , rootDirForResolution : string , logChangesWhenResolvingModule : boolean ) : ResolutionCache {
584+ export function createResolutionCache (
585+ resolutionHost : ResolutionCacheHost ,
586+ rootDirForResolution : string ,
587+ ) : ResolutionCache {
576588 let filesWithChangedSetOfUnresolvedImports : Path [ ] | undefined ;
577589 let filesWithInvalidatedResolutions : Set < Path > | undefined ;
578590 let filesWithInvalidatedNonRelativeUnresolvedImports : ReadonlyMap < Path , readonly string [ ] > | undefined ;
@@ -590,6 +602,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
590602 let startsWithPathChecks : Set < Path > | undefined ;
591603 let isInDirectoryChecks : Set < Path > | undefined ;
592604 let allModuleAndTypeResolutionsAreInvalidated = false ;
605+ let resolutionsWithGlobalCachePassAreInvalidated = false ;
606+ let resolutionsWithoutGlobalCachePassAreInvalidated = false ;
593607
594608 const getCurrentDirectory = memoize ( ( ) => resolutionHost . getCurrentDirectory ! ( ) ) ;
595609 const cachedDirectoryStructureHost = resolutionHost . getCachedDirectoryStructureHost ( ) ;
@@ -621,6 +635,9 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
621635 moduleResolutionCache . getPackageJsonInfoCache ( ) ,
622636 ) ;
623637
638+ let resolutionsResolvedWithGlobalCache = 0 ;
639+ let resolutionsResolvedWithoutGlobalCache = 0 ;
640+
624641 const directoryWatchesOfFailedLookups = new Map < Path , DirectoryWatchesOfFailedLookup > ( ) ;
625642 const fileWatchesOfAffectingLocations = new Map < string , FileWatcherOfAffectingLocation > ( ) ;
626643 const rootDir = getRootDirectoryOfResolutionCache ( rootDirForResolution , getCurrentDirectory ) ;
@@ -647,6 +664,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
647664 fileWatchesOfAffectingLocations,
648665 packageDirWatchers,
649666 dirPathToSymlinkPackageRefCount,
667+ countResolutionsResolvedWithGlobalCache : ( ) => resolutionsResolvedWithGlobalCache ,
668+ countResolutionsResolvedWithoutGlobalCache : ( ) => resolutionsResolvedWithoutGlobalCache ,
650669 watchFailedLookupLocationsOfExternalModuleResolutions,
651670 getModuleResolutionCache : ( ) => moduleResolutionCache ,
652671 startRecordingFilesWithChangedResolutions,
@@ -664,6 +683,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
664683 hasChangedAutomaticTypeDirectiveNames : ( ) => hasChangedAutomaticTypeDirectiveNames ,
665684 invalidateResolutionOfFile,
666685 invalidateResolutionsOfFailedLookupLocations,
686+ invalidateResolutionsWithGlobalCachePass,
687+ invalidateResolutionsWithoutGlobalCachePass,
667688 setFilesWithInvalidatedNonRelativeUnresolvedImports,
668689 createHasInvalidatedResolutions,
669690 isFileWithInvalidatedNonRelativeUnresolvedImports,
@@ -686,12 +707,16 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
686707 resolvedFileToResolution . clear ( ) ;
687708 resolutionsWithFailedLookups . clear ( ) ;
688709 resolutionsWithOnlyAffectingLocations . clear ( ) ;
710+ resolutionsResolvedWithGlobalCache = 0 ;
711+ resolutionsResolvedWithoutGlobalCache = 0 ;
689712 failedLookupChecks = undefined ;
690713 startsWithPathChecks = undefined ;
691714 isInDirectoryChecks = undefined ;
692715 affectingPathChecks = undefined ;
693716 affectingPathChecksForFile = undefined ;
694717 allModuleAndTypeResolutionsAreInvalidated = false ;
718+ resolutionsWithGlobalCachePassAreInvalidated = false ;
719+ resolutionsWithoutGlobalCachePassAreInvalidated = false ;
695720 moduleResolutionCache . clear ( ) ;
696721 typeReferenceDirectiveResolutionCache . clear ( ) ;
697722 moduleResolutionCache . update ( resolutionHost . getCompilationSettings ( ) ) ;
@@ -1027,7 +1052,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
10271052 ) ,
10281053 getResolutionWithResolvedFileName : getResolvedModuleFromResolution ,
10291054 shouldRetryResolution : resolution => ! resolution . resolvedModule || ! resolutionExtensionIsTSOrJson ( resolution . resolvedModule . extension ) ,
1030- logChanges : logChangesWhenResolvingModule ,
1055+ logChanges : ! ! resolutionHost . getGlobalTypingsCacheLocation ,
10311056 deferWatchingNonRelativeResolution : true , // Defer non relative resolution watch because we could be using ambient modules
10321057 } ) ;
10331058 }
@@ -1102,6 +1127,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
11021127 ) {
11031128 ( resolution . files ??= new Set ( ) ) . add ( filePath ) ;
11041129 if ( resolution . files . size !== 1 ) return ;
1130+ if ( resolution . globalCacheResolution ) resolutionsResolvedWithGlobalCache ++ ;
1131+ else if ( resolution . globalCacheResolution === false ) resolutionsResolvedWithoutGlobalCache ++ ;
11051132 if ( ! deferWatchingNonRelativeResolution || isExternalModuleNameRelative ( name ) ) {
11061133 watchFailedLookupLocationOfResolution ( resolution ) ;
11071134 }
@@ -1378,6 +1405,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
13781405 Debug . checkDefined ( resolution . files ) . delete ( filePath ) ;
13791406 if ( resolution . files ! . size ) return ;
13801407 resolution . files = undefined ;
1408+ if ( resolution . globalCacheResolution ) resolutionsResolvedWithGlobalCache -- ;
1409+ if ( resolution . globalCacheResolution === false ) resolutionsResolvedWithoutGlobalCache -- ;
13811410 const resolved = getResolutionWithResolvedFileName ( resolution ) ;
13821411 if ( resolved && resolved . resolvedFileName ) {
13831412 const key = resolutionHost . toPath ( resolved . resolvedFileName ) ;
@@ -1558,6 +1587,13 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15581587 }
15591588 }
15601589
1590+ function invalidateResolutionsWithGlobalCachePass ( ) {
1591+ if ( resolutionsResolvedWithGlobalCache ) resolutionsWithGlobalCachePassAreInvalidated = true ;
1592+ }
1593+ function invalidateResolutionsWithoutGlobalCachePass ( ) {
1594+ if ( resolutionsResolvedWithoutGlobalCache ) resolutionsWithoutGlobalCachePassAreInvalidated = true ;
1595+ }
1596+
15611597 function invalidateResolutionsOfFailedLookupLocations ( ) {
15621598 if ( allModuleAndTypeResolutionsAreInvalidated ) {
15631599 affectingPathChecksForFile = undefined ;
@@ -1569,6 +1605,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15691605 startsWithPathChecks = undefined ;
15701606 isInDirectoryChecks = undefined ;
15711607 affectingPathChecks = undefined ;
1608+ resolutionsWithGlobalCachePassAreInvalidated = false ;
1609+ resolutionsWithoutGlobalCachePassAreInvalidated = false ;
15721610 return true ;
15731611 }
15741612 let invalidated = false ;
@@ -1582,7 +1620,10 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15821620 affectingPathChecksForFile = undefined ;
15831621 }
15841622
1585- if ( ! failedLookupChecks && ! startsWithPathChecks && ! isInDirectoryChecks && ! affectingPathChecks ) {
1623+ if (
1624+ ! failedLookupChecks && ! startsWithPathChecks && ! isInDirectoryChecks && ! affectingPathChecks &&
1625+ ! resolutionsWithGlobalCachePassAreInvalidated && ! resolutionsWithoutGlobalCachePassAreInvalidated
1626+ ) {
15861627 return invalidated ;
15871628 }
15881629
@@ -1593,6 +1634,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
15931634 isInDirectoryChecks = undefined ;
15941635 invalidated = invalidateResolutions ( resolutionsWithOnlyAffectingLocations , canInvalidatedFailedLookupResolutionWithAffectingLocation ) || invalidated ;
15951636 affectingPathChecks = undefined ;
1637+ resolutionsWithGlobalCachePassAreInvalidated = false ;
1638+ resolutionsWithoutGlobalCachePassAreInvalidated = false ;
15961639 return invalidated ;
15971640 }
15981641
@@ -1612,6 +1655,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
16121655 }
16131656
16141657 function canInvalidatedFailedLookupResolutionWithAffectingLocation ( resolution : ResolutionWithFailedLookupLocations ) {
1658+ if ( resolutionsWithGlobalCachePassAreInvalidated && resolution . globalCacheResolution ) return true ;
1659+ if ( resolutionsWithoutGlobalCachePassAreInvalidated && resolution . globalCacheResolution === false ) return true ;
16151660 return ! ! affectingPathChecks && resolution . affectingLocations ?. some ( location => affectingPathChecks ! . has ( location ) ) ;
16161661 }
16171662
0 commit comments