@@ -225,6 +225,7 @@ function createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
225
225
affectingLocations : string [ ] ,
226
226
diagnostics : Diagnostic [ ] ,
227
227
state : ModuleResolutionState ,
228
+ cache : ModuleResolutionCache | NonRelativeModuleNameResolutionCache | undefined ,
228
229
legacyResult ?: string ,
229
230
) : ResolvedModuleWithFailedLookupLocations {
230
231
// If this is from node_modules for non relative name, always respect preserveSymlinks
@@ -246,6 +247,7 @@ function createResolvedModuleWithFailedLookupLocationsHandlingSymlink(
246
247
affectingLocations ,
247
248
diagnostics ,
248
249
state . resultFromCache ,
250
+ cache ,
249
251
legacyResult ,
250
252
) ;
251
253
}
@@ -257,13 +259,24 @@ function createResolvedModuleWithFailedLookupLocations(
257
259
affectingLocations : string [ ] ,
258
260
diagnostics : Diagnostic [ ] ,
259
261
resultFromCache : ResolvedModuleWithFailedLookupLocations | undefined ,
262
+ cache : ModuleResolutionCache | NonRelativeModuleNameResolutionCache | undefined ,
260
263
legacyResult ?: string ,
261
264
) : ResolvedModuleWithFailedLookupLocations {
262
265
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
+ }
267
280
}
268
281
return {
269
282
resolvedModule : resolved && {
@@ -291,6 +304,12 @@ export function updateResolutionField<T>(to: T[] | undefined, value: T[] | undef
291
304
return to ;
292
305
}
293
306
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
+
294
313
/** @internal */
295
314
export interface ModuleResolutionState {
296
315
host : ModuleResolutionHost ;
@@ -612,10 +631,10 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
612
631
affectingLocations : initializeResolutionField ( affectingLocations ) ,
613
632
resolutionDiagnostics : initializeResolutionField ( diagnostics ) ,
614
633
} ;
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 ) ;
617
636
if ( ! isExternalModuleNameRelative ( typeReferenceDirectiveName ) ) {
618
- cache ? .getOrCreateCacheForNonRelativeName ( typeReferenceDirectiveName , resolutionMode , redirectedReference ) . set ( containingDirectory , result ) ;
637
+ cache . getOrCreateCacheForNonRelativeName ( typeReferenceDirectiveName , resolutionMode , redirectedReference ) . set ( containingDirectory , result ) ;
619
638
}
620
639
}
621
640
if ( traceEnabled ) traceResult ( result ) ;
@@ -850,6 +869,8 @@ export interface PerDirectoryResolutionCache<T> {
850
869
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
851
870
*/
852
871
update ( options : CompilerOptions ) : void ;
872
+ /** @internal */ directoryToModuleNameMap : CacheWithRedirects < Path , ModeAwareCache < T > > ;
873
+ /** @internal */ isReadonly ?: boolean ;
853
874
}
854
875
855
876
export interface NonRelativeNameResolutionCache < T > {
@@ -861,11 +882,13 @@ export interface NonRelativeNameResolutionCache<T> {
861
882
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
862
883
*/
863
884
update ( options : CompilerOptions ) : void ;
885
+ /** @internal */ isReadonly ?: boolean ;
864
886
}
865
887
866
888
export interface PerNonRelativeNameCache < T > {
867
889
get ( directory : string ) : T | undefined ;
868
890
set ( directory : string , result : T ) : void ;
891
+ /** @internal */ isReadonly ?: boolean ;
869
892
}
870
893
871
894
export interface ModuleResolutionCache extends PerDirectoryResolutionCache < ResolvedModuleWithFailedLookupLocations > , NonRelativeModuleNameResolutionCache , PackageJsonInfoCache {
@@ -889,6 +912,7 @@ export interface PackageJsonInfoCache {
889
912
/** @internal */ entries ( ) : [ Path , PackageJsonInfo | boolean ] [ ] ;
890
913
/** @internal */ getInternalMap ( ) : Map < Path , PackageJsonInfo | boolean > | undefined ;
891
914
clear ( ) : void ;
915
+ /** @internal */ isReadonly ?: boolean ;
892
916
}
893
917
894
918
export type PerModuleNameCache = PerNonRelativeNameCache < ResolvedModuleWithFailedLookupLocations > ;
@@ -920,6 +944,7 @@ export interface CacheWithRedirects<K, V> {
920
944
getOrCreateMapOfCacheRedirects ( redirectedReference : ResolvedProjectReference | undefined ) : Map < K , V > ;
921
945
update ( newOptions : CompilerOptions ) : void ;
922
946
clear ( ) : void ;
947
+ getOwnMap ( ) : Map < K , V > ;
923
948
}
924
949
925
950
/** @internal */
@@ -936,6 +961,7 @@ export function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | und
936
961
getOrCreateMapOfCacheRedirects,
937
962
update,
938
963
clear,
964
+ getOwnMap : ( ) => ownMap ,
939
965
} ;
940
966
941
967
function getMapOfCacheRedirects ( redirectedReference : ResolvedProjectReference | undefined ) : Map < K , V > | undefined {
@@ -1042,6 +1068,7 @@ function createPerDirectoryResolutionCache<T>(
1042
1068
getOrCreateCacheForDirectory,
1043
1069
clear,
1044
1070
update,
1071
+ directoryToModuleNameMap,
1045
1072
} ;
1046
1073
1047
1074
function clear ( ) {
@@ -1426,10 +1453,12 @@ export function resolveModuleName(moduleName: string, containingFile: string, co
1426
1453
if ( result && result . resolvedModule ) perfLogger ?. logInfoEvent ( `Module "${ moduleName } " resolved to "${ result . resolvedModule . resolvedFileName } "` ) ;
1427
1454
perfLogger ?. logStopResolveModule ( ( result && result . resolvedModule ) ? "" + result . resolvedModule . resolvedFileName : "null" ) ;
1428
1455
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
+ }
1433
1462
}
1434
1463
}
1435
1464
@@ -1850,6 +1879,7 @@ function nodeModuleNameResolverWorker(
1850
1879
affectingLocations ,
1851
1880
diagnostics ,
1852
1881
state ,
1882
+ cache ,
1853
1883
legacyResult ,
1854
1884
) ;
1855
1885
@@ -2386,15 +2416,15 @@ export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures:
2386
2416
trace ( host , Diagnostics . Found_package_json_at_0 , packageJsonPath ) ;
2387
2417
}
2388
2418
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 ) ;
2390
2420
state . affectingLocations ?. push ( packageJsonPath ) ;
2391
2421
return result ;
2392
2422
}
2393
2423
else {
2394
2424
if ( directoryExists && traceEnabled ) {
2395
2425
trace ( host , Diagnostics . File_0_does_not_exist , packageJsonPath ) ;
2396
2426
}
2397
- state . packageJsonInfoCache ? .setPackageJsonInfo ( packageJsonPath , directoryExists ) ;
2427
+ if ( state . packageJsonInfoCache && ! state . packageJsonInfoCache . isReadonly ) state . packageJsonInfoCache . setPackageJsonInfo ( packageJsonPath , directoryExists ) ;
2398
2428
// record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
2399
2429
state . failedLookupLocations ?. push ( packageJsonPath ) ;
2400
2430
}
@@ -3178,6 +3208,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
3178
3208
affectingLocations ,
3179
3209
diagnostics ,
3180
3210
state ,
3211
+ cache ,
3181
3212
) ;
3182
3213
3183
3214
function tryResolve ( extensions : Extensions ) : SearchResult < Resolved > {
@@ -3273,6 +3304,7 @@ export function loadModuleFromGlobalCache(moduleName: string, projectName: strin
3273
3304
affectingLocations ,
3274
3305
diagnostics ,
3275
3306
state . resultFromCache ,
3307
+ /*cache*/ undefined ,
3276
3308
) ;
3277
3309
}
3278
3310
0 commit comments