@@ -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 ) ;
@@ -851,6 +870,7 @@ export interface PerDirectoryResolutionCache<T> {
851
870
*/
852
871
update ( options : CompilerOptions ) : void ;
853
872
/** @internal */ directoryToModuleNameMap : CacheWithRedirects < Path , ModeAwareCache < T > > ;
873
+ /** @internal */ isReadonly ?: boolean ;
854
874
}
855
875
856
876
export interface NonRelativeNameResolutionCache < T > {
@@ -862,11 +882,13 @@ export interface NonRelativeNameResolutionCache<T> {
862
882
* This updates the redirects map as well if needed so module resolutions are cached if they can across the projects
863
883
*/
864
884
update ( options : CompilerOptions ) : void ;
885
+ /** @internal */ isReadonly ?: boolean ;
865
886
}
866
887
867
888
export interface PerNonRelativeNameCache < T > {
868
889
get ( directory : string ) : T | undefined ;
869
890
set ( directory : string , result : T ) : void ;
891
+ /** @internal */ isReadonly ?: boolean ;
870
892
}
871
893
872
894
export interface ModuleResolutionCache extends PerDirectoryResolutionCache < ResolvedModuleWithFailedLookupLocations > , NonRelativeModuleNameResolutionCache , PackageJsonInfoCache {
@@ -890,6 +912,7 @@ export interface PackageJsonInfoCache {
890
912
/** @internal */ entries ( ) : [ Path , PackageJsonInfo | boolean ] [ ] ;
891
913
/** @internal */ getInternalMap ( ) : Map < Path , PackageJsonInfo | boolean > | undefined ;
892
914
clear ( ) : void ;
915
+ /** @internal */ isReadonly ?: boolean ;
893
916
}
894
917
895
918
export type PerModuleNameCache = PerNonRelativeNameCache < ResolvedModuleWithFailedLookupLocations > ;
@@ -1430,10 +1453,12 @@ export function resolveModuleName(moduleName: string, containingFile: string, co
1430
1453
if ( result && result . resolvedModule ) perfLogger ?. logInfoEvent ( `Module "${ moduleName } " resolved to "${ result . resolvedModule . resolvedFileName } "` ) ;
1431
1454
perfLogger ?. logStopResolveModule ( ( result && result . resolvedModule ) ? "" + result . resolvedModule . resolvedFileName : "null" ) ;
1432
1455
1433
- cache ?. getOrCreateCacheForDirectory ( containingDirectory , redirectedReference ) . set ( moduleName , resolutionMode , result ) ;
1434
- if ( ! isExternalModuleNameRelative ( moduleName ) ) {
1435
- // put result in per-module name cache
1436
- 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
+ }
1437
1462
}
1438
1463
}
1439
1464
@@ -1854,6 +1879,7 @@ function nodeModuleNameResolverWorker(
1854
1879
affectingLocations ,
1855
1880
diagnostics ,
1856
1881
state ,
1882
+ cache ,
1857
1883
legacyResult ,
1858
1884
) ;
1859
1885
@@ -2390,15 +2416,15 @@ export function getPackageJsonInfo(packageDirectory: string, onlyRecordFailures:
2390
2416
trace ( host , Diagnostics . Found_package_json_at_0 , packageJsonPath ) ;
2391
2417
}
2392
2418
const result : PackageJsonInfo = { packageDirectory, contents : { packageJsonContent, versionPaths : undefined , resolvedEntrypoints : undefined } } ;
2393
- state . packageJsonInfoCache ? .setPackageJsonInfo ( packageJsonPath , result ) ;
2419
+ if ( state . packageJsonInfoCache && ! state . packageJsonInfoCache . isReadonly ) state . packageJsonInfoCache . setPackageJsonInfo ( packageJsonPath , result ) ;
2394
2420
state . affectingLocations ?. push ( packageJsonPath ) ;
2395
2421
return result ;
2396
2422
}
2397
2423
else {
2398
2424
if ( directoryExists && traceEnabled ) {
2399
2425
trace ( host , Diagnostics . File_0_does_not_exist , packageJsonPath ) ;
2400
2426
}
2401
- state . packageJsonInfoCache ? .setPackageJsonInfo ( packageJsonPath , directoryExists ) ;
2427
+ if ( state . packageJsonInfoCache && ! state . packageJsonInfoCache . isReadonly ) state . packageJsonInfoCache . setPackageJsonInfo ( packageJsonPath , directoryExists ) ;
2402
2428
// record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
2403
2429
state . failedLookupLocations ?. push ( packageJsonPath ) ;
2404
2430
}
@@ -3182,6 +3208,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
3182
3208
affectingLocations ,
3183
3209
diagnostics ,
3184
3210
state ,
3211
+ cache ,
3185
3212
) ;
3186
3213
3187
3214
function tryResolve ( extensions : Extensions ) : SearchResult < Resolved > {
@@ -3277,6 +3304,7 @@ export function loadModuleFromGlobalCache(moduleName: string, projectName: strin
3277
3304
affectingLocations ,
3278
3305
diagnostics ,
3279
3306
state . resultFromCache ,
3307
+ /*cache*/ undefined ,
3280
3308
) ;
3281
3309
}
3282
3310
0 commit comments