@@ -225,6 +225,7 @@ function createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
225225 affectingLocations : string [ ] ,
226226 diagnostics : Diagnostic [ ] ,
227227 state : ModuleResolutionState ,
228+ cache : ModuleResolutionCache | NonRelativeModuleNameResolutionCache | undefined ,
228229 legacyResult ?: string ,
229230) : ResolvedModuleWithFailedLookupLocations {
230231 // If this is from node_modules for non relative name, always respect preserveSymlinks
@@ -246,6 +247,7 @@ function createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
246247 affectingLocations ,
247248 diagnostics ,
248249 state . resultFromCache ,
250+ cache ,
249251 legacyResult ,
250252 ) ;
251253}
@@ -257,13 +259,24 @@ function createResolvedModuleWithFailedLookupLocations(
257259 affectingLocations : string [ ] ,
258260 diagnostics : Diagnostic [ ] ,
259261 resultFromCache : ResolvedModuleWithFailedLookupLocations | undefined ,
262+ cache : ModuleResolutionCache | NonRelativeModuleNameResolutionCache | undefined ,
260263 legacyResult ?: string ,
261264) : ResolvedModuleWithFailedLookupLocations {
262265 if ( resultFromCache ) {
263- resultFromCache . failedLookupLocations = updateResolutionField ( resultFromCache . failedLookupLocations , failedLookupLocations ) ;
264- resultFromCache . affectingLocations = updateResolutionField ( resultFromCache . affectingLocations , affectingLocations ) ;
265- resultFromCache . resolutionDiagnostics = updateResolutionField ( resultFromCache . resolutionDiagnostics , diagnostics ) ;
266- return resultFromCache ;
266+ if ( ! cache ?. isReadonly ) {
267+ resultFromCache . failedLookupLocations = updateResolutionField ( resultFromCache . failedLookupLocations , failedLookupLocations ) ;
268+ resultFromCache . affectingLocations = updateResolutionField ( resultFromCache . affectingLocations , affectingLocations ) ;
269+ resultFromCache . resolutionDiagnostics = updateResolutionField ( resultFromCache . resolutionDiagnostics , diagnostics ) ;
270+ return resultFromCache ;
271+ }
272+ else {
273+ return {
274+ ...resultFromCache ,
275+ failedLookupLocations : initializeResolutionFieldForReadonlyCache ( resultFromCache . failedLookupLocations , failedLookupLocations ) ,
276+ affectingLocations : initializeResolutionFieldForReadonlyCache ( resultFromCache . affectingLocations , affectingLocations ) ,
277+ resolutionDiagnostics : initializeResolutionFieldForReadonlyCache ( resultFromCache . resolutionDiagnostics , diagnostics ) ,
278+ } ;
279+ }
267280 }
268281 return {
269282 resolvedModule : resolved && {
@@ -291,6 +304,12 @@ export function updateResolutionField<T>(to: T[] | undefined, value: T[] | undef
291304 return to ;
292305}
293306
307+ function initializeResolutionFieldForReadonlyCache < T > ( fromCache : T [ ] | undefined , value : T [ ] ) : T [ ] | undefined {
308+ if ( ! fromCache ?. length ) return initializeResolutionField ( value ) ;
309+ if ( ! value . length ) return fromCache . slice ( ) ;
310+ return [ ...fromCache , ...value ] ;
311+ }
312+
294313/** @internal */
295314export interface ModuleResolutionState {
296315 host : ModuleResolutionHost ;
@@ -612,10 +631,10 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
612631 affectingLocations : initializeResolutionField ( affectingLocations ) ,
613632 resolutionDiagnostics : initializeResolutionField ( diagnostics ) ,
614633 } ;
615- if ( containingDirectory ) {
616- cache ? .getOrCreateCacheForDirectory ( containingDirectory , redirectedReference ) . set ( typeReferenceDirectiveName , /*mode*/ resolutionMode , result ) ;
634+ if ( containingDirectory && cache && ! cache . isReadonly ) {
635+ cache . getOrCreateCacheForDirectory ( containingDirectory , redirectedReference ) . set ( typeReferenceDirectiveName , /*mode*/ resolutionMode , result ) ;
617636 if ( ! isExternalModuleNameRelative ( typeReferenceDirectiveName ) ) {
618- cache ? .getOrCreateCacheForNonRelativeName ( typeReferenceDirectiveName , resolutionMode , redirectedReference ) . set ( containingDirectory , result ) ;
637+ cache . getOrCreateCacheForNonRelativeName ( typeReferenceDirectiveName , resolutionMode , redirectedReference ) . set ( containingDirectory , result ) ;
619638 }
620639 }
621640 if ( traceEnabled ) traceResult ( result ) ;
@@ -850,6 +869,8 @@ export interface PerDirectoryResolutionCache<T> {
850869 * This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
851870 */
852871 update ( options : CompilerOptions ) : void ;
872+ /** @internal */ directoryToModuleNameMap : CacheWithRedirects < Path , ModeAwareCache < T > > ;
873+ /** @internal */ isReadonly ?: boolean ;
853874}
854875
855876export interface NonRelativeNameResolutionCache < T > {
@@ -861,11 +882,13 @@ export interface NonRelativeNameResolutionCache<T> {
861882 * This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
862883 */
863884 update ( options : CompilerOptions ) : void ;
885+ /** @internal */ isReadonly ?: boolean ;
864886}
865887
866888export interface PerNonRelativeNameCache < T > {
867889 get ( directory : string ) : T | undefined ;
868890 set ( directory : string , result : T ) : void ;
891+ /** @internal */ isReadonly ?: boolean ;
869892}
870893
871894export interface ModuleResolutionCache extends PerDirectoryResolutionCache < ResolvedModuleWithFailedLookupLocations > , NonRelativeModuleNameResolutionCache , PackageJsonInfoCache {
@@ -889,6 +912,7 @@ export interface PackageJsonInfoCache {
889912 /** @internal */ entries ( ) : [ Path , PackageJsonInfo | boolean ] [ ] ;
890913 /** @internal */ getInternalMap ( ) : Map < Path , PackageJsonInfo | boolean > | undefined ;
891914 clear ( ) : void ;
915+ /** @internal */ isReadonly ?: boolean ;
892916}
893917
894918export type PerModuleNameCache = PerNonRelativeNameCache < ResolvedModuleWithFailedLookupLocations > ;
@@ -920,6 +944,7 @@ export interface CacheWithRedirects<K, V> {
920944 getOrCreateMapOfCacheRedirects ( redirectedReference : ResolvedProjectReference | undefined ) : Map < K , V > ;
921945 update ( newOptions : CompilerOptions ) : void ;
922946 clear ( ) : void ;
947+ getOwnMap ( ) : Map < K , V > ;
923948}
924949
925950/** @internal */
@@ -936,6 +961,7 @@ export function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | und
936961 getOrCreateMapOfCacheRedirects,
937962 update,
938963 clear,
964+ getOwnMap : ( ) => ownMap ,
939965 } ;
940966
941967 function getMapOfCacheRedirects ( redirectedReference : ResolvedProjectReference | undefined ) : Map < K , V > | undefined {
@@ -1042,6 +1068,7 @@ function createPerDirectoryResolutionCache<T>(
10421068 getOrCreateCacheForDirectory,
10431069 clear,
10441070 update,
1071+ directoryToModuleNameMap,
10451072 } ;
10461073
10471074 function clear ( ) {
@@ -1426,10 +1453,12 @@ export function resolveModuleName(moduleName: string, containingFile: string, co
14261453 if ( result && result . resolvedModule ) perfLogger ?. logInfoEvent ( `Module "${ moduleName } " resolved to "${ result . resolvedModule . resolvedFileName } "` ) ;
14271454 perfLogger ?. logStopResolveModule ( ( result && result . resolvedModule ) ? "" + result . resolvedModule . resolvedFileName : "null" ) ;
14281455
1429- cache ?. getOrCreateCacheForDirectory ( containingDirectory , redirectedReference ) . set ( moduleName , resolutionMode , result ) ;
1430- if ( ! isExternalModuleNameRelative ( moduleName ) ) {
1431- // put result in per-module name cache
1432- cache ?. getOrCreateCacheForNonRelativeName ( moduleName , resolutionMode , redirectedReference ) . set ( containingDirectory , result ) ;
1456+ if ( cache && ! cache . isReadonly ) {
1457+ cache . getOrCreateCacheForDirectory ( containingDirectory , redirectedReference ) . set ( moduleName , resolutionMode , result ) ;
1458+ if ( ! isExternalModuleNameRelative ( moduleName ) ) {
1459+ // put result in per-module name cache
1460+ cache . getOrCreateCacheForNonRelativeName ( moduleName , resolutionMode , redirectedReference ) . set ( containingDirectory , result ) ;
1461+ }
14331462 }
14341463 }
14351464
@@ -1850,6 +1879,7 @@ function nodeModuleNameResolverWorker(
18501879 affectingLocations ,
18511880 diagnostics ,
18521881 state ,
1882+ cache ,
18531883 legacyResult ,
18541884 ) ;
18551885
@@ -2386,15 +2416,15 @@ export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures:
23862416 trace ( host , Diagnostics . Found_package_json_at_0 , packageJsonPath ) ;
23872417 }
23882418 const result : PackageJsonInfo = { packageDirectory, contents : { packageJsonContent, versionPaths : undefined , resolvedEntrypoints : undefined } } ;
2389- state . packageJsonInfoCache ? .setPackageJsonInfo ( packageJsonPath , result ) ;
2419+ if ( state . packageJsonInfoCache && ! state . packageJsonInfoCache . isReadonly ) state . packageJsonInfoCache . setPackageJsonInfo ( packageJsonPath , result ) ;
23902420 state . affectingLocations ?. push ( packageJsonPath ) ;
23912421 return result ;
23922422 }
23932423 else {
23942424 if ( directoryExists && traceEnabled ) {
23952425 trace ( host , Diagnostics . File_0_does_not_exist , packageJsonPath ) ;
23962426 }
2397- state . packageJsonInfoCache ? .setPackageJsonInfo ( packageJsonPath , directoryExists ) ;
2427+ if ( state . packageJsonInfoCache && ! state . packageJsonInfoCache . isReadonly ) state . packageJsonInfoCache . setPackageJsonInfo ( packageJsonPath , directoryExists ) ;
23982428 // record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
23992429 state . failedLookupLocations ?. push ( packageJsonPath ) ;
24002430 }
@@ -3178,6 +3208,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
31783208 affectingLocations ,
31793209 diagnostics ,
31803210 state ,
3211+ cache ,
31813212 ) ;
31823213
31833214 function tryResolve ( extensions : Extensions ) : SearchResult < Resolved > {
@@ -3273,6 +3304,7 @@ export function loadModuleFromGlobalCache(moduleName: string, projectName: strin
32733304 affectingLocations ,
32743305 diagnostics ,
32753306 state . resultFromCache ,
3307+ /*cache*/ undefined ,
32763308 ) ;
32773309}
32783310
0 commit comments