@@ -99,6 +99,8 @@ export interface ResolutionCache {
9999 resolutionsWithOnlyAffectingLocations : Set < ResolutionWithFailedLookupLocations > ;
100100 directoryWatchesOfFailedLookups : Map < string , DirectoryWatchesOfFailedLookup > ;
101101 fileWatchesOfAffectingLocations : Map < string , FileWatcherOfAffectingLocation > ;
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 , syncDirWatcherRemove ?: boolean ) : void ;
143147 removeResolutionsFromProjectReferenceRedirects ( filePath : Path ) : void ;
@@ -170,6 +174,7 @@ export interface ResolutionWithFailedLookupLocations {
170174 // Files that have this resolution using
171175 files ?: Set < Path > ;
172176 node10Result ?: string ;
177+ globalCacheResolution ?: boolean ;
173178}
174179
175180/** @internal */
@@ -490,13 +495,17 @@ function resolveModuleNameUsingGlobalCache(
490495 const host = getModuleResolutionHost ( resolutionHost ) ;
491496 const primaryResult = ts_resolveModuleName ( moduleName , containingFile , compilerOptions , host , moduleResolutionCache , redirectedReference , mode ) ;
492497 // return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts
493- if ( ! resolutionHost . getGlobalCache ) {
498+ if ( ! resolutionHost . getGlobalCache || primaryResult . globalCacheResolution !== undefined ) {
494499 return primaryResult ;
495500 }
496501
497502 // otherwise try to load typings from @types
498503 const globalCache = resolutionHost . getGlobalCache ( ) ;
499- if ( globalCache !== undefined && ! isExternalModuleNameRelative ( moduleName ) && ! ( primaryResult . resolvedModule && extensionIsTS ( primaryResult . resolvedModule . extension ) ) ) {
504+ if ( ! isExternalModuleNameRelative ( moduleName ) && ! ( primaryResult . resolvedModule && extensionIsTS ( primaryResult . resolvedModule . extension ) ) ) {
505+ if ( globalCache === undefined ) {
506+ primaryResult . globalCacheResolution = false ;
507+ return primaryResult ;
508+ }
500509 // create different collection of failed lookup locations for second pass
501510 // if it will fail and we've already found something during the first pass - we don't want to pollute its results
502511 const { resolvedModule, failedLookupLocations, affectingLocations, resolutionDiagnostics } = loadModuleFromGlobalCache (
@@ -507,6 +516,7 @@ function resolveModuleNameUsingGlobalCache(
507516 globalCache ,
508517 moduleResolutionCache ,
509518 ) ;
519+ primaryResult . globalCacheResolution = true ;
510520 if ( resolvedModule ) {
511521 // Modify existing resolution so its saved in the directory cache as well
512522 ( primaryResult . resolvedModule as any ) = resolvedModule ;
@@ -525,7 +535,10 @@ function resolveModuleNameUsingGlobalCache(
525535export type GetResolutionWithResolvedFileName < T extends ResolutionWithFailedLookupLocations = ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName = ResolutionWithResolvedFileName > = ( resolution : T ) => R | undefined ;
526536
527537/** @internal */
528- export function createResolutionCache ( resolutionHost : ResolutionCacheHost , rootDirForResolution : string , logChangesWhenResolvingModule : boolean ) : ResolutionCache {
538+ export function createResolutionCache (
539+ resolutionHost : ResolutionCacheHost ,
540+ rootDirForResolution : string ,
541+ ) : ResolutionCache {
529542 let filesWithChangedSetOfUnresolvedImports : Path [ ] | undefined ;
530543 let filesWithInvalidatedResolutions : Set < Path > | undefined ;
531544 let filesWithInvalidatedNonRelativeUnresolvedImports : ReadonlyMap < Path , readonly string [ ] > | undefined ;
@@ -543,6 +556,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
543556 let startsWithPathChecks : Set < Path > | undefined ;
544557 let isInDirectoryChecks : Set < Path > | undefined ;
545558 let allModuleAndTypeResolutionsAreInvalidated = false ;
559+ let resolutionsWithGlobalCachePassAreInvalidated = false ;
560+ let resolutionsWithoutGlobalCachePassAreInvalidated = false ;
546561
547562 const getCurrentDirectory = memoize ( ( ) => resolutionHost . getCurrentDirectory ! ( ) ) ; // TODO: GH#18217
548563 const cachedDirectoryStructureHost = resolutionHost . getCachedDirectoryStructureHost ( ) ;
@@ -574,6 +589,9 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
574589 moduleResolutionCache . getPackageJsonInfoCache ( ) ,
575590 ) ;
576591
592+ let resolutionsResolvedWithGlobalCache = 0 ;
593+ let resolutionsResolvedWithoutGlobalCache = 0 ;
594+
577595 const directoryWatchesOfFailedLookups = new Map < Path , DirectoryWatchesOfFailedLookup > ( ) ;
578596 const fileWatchesOfAffectingLocations = new Map < string , FileWatcherOfAffectingLocation > ( ) ;
579597 const rootDir = getRootDirectoryOfResolutionCache ( rootDirForResolution , getCurrentDirectory ) ;
@@ -593,6 +611,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
593611 resolutionsWithOnlyAffectingLocations,
594612 directoryWatchesOfFailedLookups,
595613 fileWatchesOfAffectingLocations,
614+ countResolutionsResolvedWithGlobalCache : ( ) => resolutionsResolvedWithGlobalCache ,
615+ countResolutionsResolvedWithoutGlobalCache : ( ) => resolutionsResolvedWithoutGlobalCache ,
596616 watchFailedLookupLocationsOfExternalModuleResolutions,
597617 getModuleResolutionCache : ( ) => moduleResolutionCache ,
598618 startRecordingFilesWithChangedResolutions,
@@ -610,6 +630,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
610630 hasChangedAutomaticTypeDirectiveNames : ( ) => hasChangedAutomaticTypeDirectiveNames ,
611631 invalidateResolutionOfFile,
612632 invalidateResolutionsOfFailedLookupLocations,
633+ invalidateResolutionsWithGlobalCachePass,
634+ invalidateResolutionsWithoutGlobalCachePass,
613635 setFilesWithInvalidatedNonRelativeUnresolvedImports,
614636 createHasInvalidatedResolutions,
615637 isFileWithInvalidatedNonRelativeUnresolvedImports,
@@ -637,12 +659,16 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
637659 resolvedFileToResolution . clear ( ) ;
638660 resolutionsWithFailedLookups . clear ( ) ;
639661 resolutionsWithOnlyAffectingLocations . clear ( ) ;
662+ resolutionsResolvedWithGlobalCache = 0 ;
663+ resolutionsResolvedWithoutGlobalCache = 0 ;
640664 failedLookupChecks = undefined ;
641665 startsWithPathChecks = undefined ;
642666 isInDirectoryChecks = undefined ;
643667 affectingPathChecks = undefined ;
644668 affectingPathChecksForFile = undefined ;
645669 allModuleAndTypeResolutionsAreInvalidated = false ;
670+ resolutionsWithGlobalCachePassAreInvalidated = false ;
671+ resolutionsWithoutGlobalCachePassAreInvalidated = false ;
646672 moduleResolutionCache . clear ( ) ;
647673 typeReferenceDirectiveResolutionCache . clear ( ) ;
648674 moduleResolutionCache . update ( resolutionHost . getCompilationSettings ( ) ) ;
@@ -970,7 +996,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
970996 ) ,
971997 getResolutionWithResolvedFileName : getResolvedModule ,
972998 shouldRetryResolution : resolution => ! resolution . resolvedModule || ! resolutionExtensionIsTSOrJson ( resolution . resolvedModule . extension ) ,
973- logChanges : logChangesWhenResolvingModule ,
999+ logChanges : ! ! resolutionHost . getGlobalCache ,
9741000 deferWatchingNonRelativeResolution : true , // Defer non relative resolution watch because we could be using ambient modules
9751001 } ) ;
9761002 }
@@ -1049,6 +1075,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
10491075 }
10501076 else {
10511077 resolution . refCount = 1 ;
1078+ if ( resolution . globalCacheResolution ) resolutionsResolvedWithGlobalCache ++ ;
1079+ else if ( resolution . globalCacheResolution === false ) resolutionsResolvedWithoutGlobalCache ++ ;
10521080 Debug . assert ( ! resolution . files ?. size ) ; // This resolution shouldnt be referenced by any file yet
10531081 if ( ! deferWatchingNonRelativeResolution || isExternalModuleNameRelative ( name ) ) {
10541082 watchFailedLookupLocationOfResolution ( resolution ) ;
@@ -1241,6 +1269,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
12411269 if ( resolution . refCount ) {
12421270 return ;
12431271 }
1272+ if ( resolution . globalCacheResolution ) resolutionsResolvedWithGlobalCache -- ;
1273+ if ( resolution . globalCacheResolution === false ) resolutionsResolvedWithoutGlobalCache -- ;
12441274 const resolved = getResolutionWithResolvedFileName ( resolution ) ;
12451275 if ( resolved && resolved . resolvedFileName ) {
12461276 const key = resolutionHost . toPath ( resolved . resolvedFileName ) ;
@@ -1421,6 +1451,13 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14211451 }
14221452 }
14231453
1454+ function invalidateResolutionsWithGlobalCachePass ( ) {
1455+ if ( resolutionsResolvedWithGlobalCache ) resolutionsWithGlobalCachePassAreInvalidated = true ;
1456+ }
1457+ function invalidateResolutionsWithoutGlobalCachePass ( ) {
1458+ if ( resolutionsResolvedWithoutGlobalCache ) resolutionsWithoutGlobalCachePassAreInvalidated = true ;
1459+ }
1460+
14241461 function invalidateResolutionsOfFailedLookupLocations ( ) {
14251462 if ( allModuleAndTypeResolutionsAreInvalidated ) {
14261463 affectingPathChecksForFile = undefined ;
@@ -1432,6 +1469,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14321469 startsWithPathChecks = undefined ;
14331470 isInDirectoryChecks = undefined ;
14341471 affectingPathChecks = undefined ;
1472+ resolutionsWithGlobalCachePassAreInvalidated = false ;
1473+ resolutionsWithoutGlobalCachePassAreInvalidated = false ;
14351474 return true ;
14361475 }
14371476 let invalidated = false ;
@@ -1445,7 +1484,10 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14451484 affectingPathChecksForFile = undefined ;
14461485 }
14471486
1448- if ( ! failedLookupChecks && ! startsWithPathChecks && ! isInDirectoryChecks && ! affectingPathChecks ) {
1487+ if (
1488+ ! failedLookupChecks && ! startsWithPathChecks && ! isInDirectoryChecks && ! affectingPathChecks &&
1489+ ! resolutionsWithGlobalCachePassAreInvalidated && ! resolutionsWithoutGlobalCachePassAreInvalidated
1490+ ) {
14491491 return invalidated ;
14501492 }
14511493
@@ -1456,6 +1498,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14561498 isInDirectoryChecks = undefined ;
14571499 invalidated = invalidateResolutions ( resolutionsWithOnlyAffectingLocations , canInvalidatedFailedLookupResolutionWithAffectingLocation ) || invalidated ;
14581500 affectingPathChecks = undefined ;
1501+ resolutionsWithGlobalCachePassAreInvalidated = false ;
1502+ resolutionsWithoutGlobalCachePassAreInvalidated = false ;
14591503 return invalidated ;
14601504 }
14611505
@@ -1475,6 +1519,8 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
14751519 }
14761520
14771521 function canInvalidatedFailedLookupResolutionWithAffectingLocation ( resolution : ResolutionWithFailedLookupLocations ) {
1522+ if ( resolutionsWithGlobalCachePassAreInvalidated && resolution . globalCacheResolution ) return true ;
1523+ if ( resolutionsWithoutGlobalCachePassAreInvalidated && resolution . globalCacheResolution === false ) return true ;
14781524 return ! ! affectingPathChecks && resolution . affectingLocations ?. some ( location => affectingPathChecks ! . has ( location ) ) ;
14791525 }
14801526
0 commit comments