@@ -690,7 +690,6 @@ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: M
690
690
}
691
691
692
692
export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > , NonRelativeNameResolutionCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > , PackageJsonInfoCache {
693
- /** @internal */ clearAllExceptPackageJsonInfoCache ( ) : void ;
694
693
}
695
694
696
695
export interface ModeAwareCache < T > {
@@ -744,11 +743,11 @@ export interface PerNonRelativeNameCache<T> {
744
743
set ( directory : string , result : T ) : void ;
745
744
/** @internal */ getWithPath ( directory : Path ) : T | undefined ;
746
745
/** @internal */ setWithPath ( directory : Path , result : T , ancestoryWorker : ( directory : Path ) => void ) : void ;
746
+ /** @internal */ clear ( ) : void ;
747
747
}
748
748
749
749
export interface ModuleResolutionCache extends PerDirectoryResolutionCache < ResolvedModuleWithFailedLookupLocations > , NonRelativeModuleNameResolutionCache , PackageJsonInfoCache {
750
750
getPackageJsonInfoCache ( ) : PackageJsonInfoCache ;
751
- /** @internal */ clearAllExceptPackageJsonInfoCache ( ) : void ;
752
751
}
753
752
754
753
/**
@@ -760,12 +759,32 @@ export interface NonRelativeModuleNameResolutionCache extends NonRelativeNameRes
760
759
getOrCreateCacheForModuleName ( nonRelativeModuleName : string , mode : ResolutionMode , redirectedReference ?: ResolvedProjectReference ) : PerModuleNameCache ;
761
760
}
762
761
762
+ /** @internal */
763
+ export interface PackageJsonScope {
764
+ info : PackageJsonInfo | undefined ;
765
+ failedLookupLocations ?: string [ ] ;
766
+ affectingLocations ?: string [ ] ;
767
+ }
768
+ /** @internal */
769
+ export function getPackageJsonLocationFromScope ( scope : PackageJsonScope ) {
770
+ return lastOrUndefined ( scope . affectingLocations ) ;
771
+ }
772
+
773
+ /** @internal */
774
+ export interface OldPackageJsonInfoCache {
775
+ getPackageJsonScope ( dir : string ) : PackageJsonScope | undefined ;
776
+ }
777
+
763
778
export interface PackageJsonInfoCache {
764
779
/** @internal */ getPackageJsonInfo ( packageJsonPath : string ) : PackageJsonInfo | boolean | undefined ;
765
780
/** @internal */ setPackageJsonInfo ( packageJsonPath : string , info : PackageJsonInfo | boolean ) : void ;
766
781
/** @internal */ entries ( ) : [ Path , PackageJsonInfo | boolean ] [ ] ;
767
782
/** @internal */ getInternalMap ( ) : Map < Path , PackageJsonInfo | boolean > | undefined ;
783
+ /** @internal */ setInternalMap ( value : Map < Path , PackageJsonInfo | boolean > | undefined ) : void ;
768
784
/** @internal */ clone ( ) : PackageJsonInfoCache ;
785
+ /** @internal */ getPackageJsonScope ( dir : string ) : PackageJsonScope | undefined ;
786
+ /** @internal */ setPackageJsonScope ( dir : string , scope : PackageJsonScope ) : void ;
787
+ /** @internal */ setOldPackageJsonScopeCache ( cache : OldPackageJsonInfoCache | undefined ) : void ;
769
788
clear ( ) : void ;
770
789
}
771
790
@@ -886,26 +905,44 @@ function createCacheWithRedirects<K, V>(ownOptions: CompilerOptions | undefined)
886
905
}
887
906
888
907
function createPackageJsonInfoCache ( currentDirectory : string , getCanonicalFileName : ( s : string ) => string , cache ?: Map < Path , PackageJsonInfo | boolean > ) : PackageJsonInfoCache {
889
- return { getPackageJsonInfo, setPackageJsonInfo, clear, entries, getInternalMap, clone } ;
908
+ const packageJsonScopes = createPerNonRelativeNameCache ( currentDirectory , getCanonicalFileName , getPackageJsonLocationFromScope ) ;
909
+ let oldPackageJsonInfoCache : OldPackageJsonInfoCache | undefined ;
910
+ return {
911
+ getPackageJsonInfo,
912
+ setPackageJsonInfo,
913
+ clear,
914
+ entries,
915
+ getInternalMap : ( ) => cache ,
916
+ setInternalMap : value => cache = value ,
917
+ clone,
918
+ getPackageJsonScope,
919
+ setPackageJsonScope,
920
+ setOldPackageJsonScopeCache : cache => oldPackageJsonInfoCache = cache ,
921
+ } ;
890
922
function getPackageJsonInfo ( packageJsonPath : string ) {
891
923
return cache ?. get ( toPath ( packageJsonPath , currentDirectory , getCanonicalFileName ) ) ;
892
924
}
893
925
function setPackageJsonInfo ( packageJsonPath : string , info : PackageJsonInfo | boolean ) {
894
926
( cache ||= new Map ( ) ) . set ( toPath ( packageJsonPath , currentDirectory , getCanonicalFileName ) , info ) ;
895
927
}
896
928
function clear ( ) {
929
+ oldPackageJsonInfoCache = undefined ;
897
930
cache = undefined ;
931
+ packageJsonScopes . clear ( ) ;
898
932
}
899
933
function entries ( ) {
900
934
const iter = cache ?. entries ( ) ;
901
935
return iter ? arrayFrom ( iter ) : [ ] ;
902
936
}
903
- function getInternalMap ( ) {
904
- return cache ;
905
- }
906
937
function clone ( ) {
907
938
return createPackageJsonInfoCache ( currentDirectory , getCanonicalFileName , cache && new Map ( cache ) ) ;
908
939
}
940
+ function getPackageJsonScope ( dir : string ) {
941
+ return packageJsonScopes . get ( dir ) || oldPackageJsonInfoCache ?. getPackageJsonScope ( dir ) ;
942
+ }
943
+ function setPackageJsonScope ( dir : string , scope : PackageJsonScope ) {
944
+ packageJsonScopes . set ( dir , scope ) ;
945
+ }
909
946
}
910
947
911
948
function getOrCreateCache < K , V > ( cacheWithRedirects : CacheWithRedirects < K , V > , redirectedReference : ResolvedProjectReference | undefined , key : K , create : ( ) => V ) : V {
@@ -1041,7 +1078,11 @@ export function createPerNonRelativeNameCache<T>(
1041
1078
) : PerNonRelativeNameCache < T > {
1042
1079
const directoryPathMap = new Map < Path , T > ( ) ;
1043
1080
1044
- return { get, set, getWithPath, setWithPath } ;
1081
+ return { get, set, getWithPath, setWithPath, clear } ;
1082
+
1083
+ function clear ( ) {
1084
+ directoryPathMap . clear ( ) ;
1085
+ }
1045
1086
1046
1087
function get ( directory : string ) : T | undefined {
1047
1088
return getWithPath ( toPath ( directory , currentDirectory , getCanonicalFileName ) ) ;
@@ -1242,7 +1283,6 @@ export function toPerDirectoryAndNonRelativeNameCache<T, U, V>(
1242
1283
1243
1284
interface ModuleOrTypeReferenceResolutionCache < T > extends PerDirectoryResolutionCache < T > , NonRelativeNameResolutionCache < T > , PackageJsonInfoCache {
1244
1285
getPackageJsonInfoCache ( ) : PackageJsonInfoCache ;
1245
- clearAllExceptPackageJsonInfoCache ( ) : void ;
1246
1286
}
1247
1287
function createModuleOrTypeReferenceResolutionCache < T > (
1248
1288
currentDirectory : string ,
@@ -1261,18 +1301,13 @@ function createModuleOrTypeReferenceResolutionCache<T>(
1261
1301
clear,
1262
1302
update,
1263
1303
getPackageJsonInfoCache : ( ) => packageJsonInfoCache ! ,
1264
- clearAllExceptPackageJsonInfoCache,
1265
1304
setOldResolutionCache,
1266
1305
} ;
1267
1306
1268
1307
function clear ( ) {
1269
- clearAllExceptPackageJsonInfoCache ( ) ;
1270
- packageJsonInfoCache ! . clear ( ) ;
1271
- }
1272
-
1273
- function clearAllExceptPackageJsonInfoCache ( ) {
1274
1308
perDirectory . clear ( ) ;
1275
1309
perNonRelativeName . clear ( ) ;
1310
+ packageJsonInfoCache ! . clear ( ) ;
1276
1311
}
1277
1312
1278
1313
function update ( options : CompilerOptions ) {
@@ -2189,15 +2224,51 @@ export interface PackageJsonInfoContents {
2189
2224
/** false: resolved to nothing. undefined: not yet resolved */
2190
2225
resolvedEntrypoints : string [ ] | false | undefined ;
2191
2226
}
2227
+ /** @internal */
2228
+ export function getPackageScope (
2229
+ dir : string ,
2230
+ packageJsonInfoCache : PackageJsonInfoCache | undefined ,
2231
+ host : ModuleResolutionHost ,
2232
+ options : CompilerOptions ,
2233
+ ) : PackageJsonScope {
2234
+ const state = getTemporaryModuleResolutionState ( packageJsonInfoCache , host , options ) ;
2235
+ const failedLookupLocations : string [ ] = [ ] ;
2236
+ const affectingLocations : string [ ] = [ ] ;
2237
+ state . failedLookupLocations = failedLookupLocations ;
2238
+ state . affectingLocations = affectingLocations ;
2239
+ const result : PackageJsonScope = forEachAncestorDirectory ( dir , directory => {
2240
+ const fromCache = state . packageJsonInfoCache ?. getPackageJsonScope ( directory ) ;
2241
+ if ( fromCache ) {
2242
+ const { host, traceEnabled } = state ;
2243
+ if ( traceEnabled ) {
2244
+ trace (
2245
+ host ,
2246
+ fromCache . info ?
2247
+ Diagnostics . Directory_0_resolves_to_1_scope_according_to_cache :
2248
+ Diagnostics . Directory_0_has_no_containing_package_json_scope_according_to_cache ,
2249
+ directory ,
2250
+ getPackageJsonLocationFromScope ( fromCache )
2251
+ ) ;
2252
+ }
2253
+ return fromCache ;
2254
+ }
2255
+
2256
+ const info = getPackageJsonInfo ( directory , /*onlyRecordFailures*/ false , state ) ;
2257
+ if ( info ) return { info } ;
2258
+ } ) || { info : undefined } ;
2259
+ result . failedLookupLocations = updateResolutionField ( result . failedLookupLocations , ! options . cacheResolutions || ! result . info ? failedLookupLocations : undefined ) ;
2260
+ result . affectingLocations = updateResolutionField ( result . affectingLocations , affectingLocations ) ;
2261
+ packageJsonInfoCache ?. setPackageJsonScope ( dir , result ) ;
2262
+ return result ;
2263
+ }
2192
2264
2193
2265
/**
2194
2266
* A function for locating the package.json scope for a given path
2195
2267
*
2196
2268
* @internal
2197
2269
*/
2198
- export function getPackageScopeForPath ( fileName : string , state : ModuleResolutionState ) : PackageJsonInfo | undefined {
2199
- const parts = getPathComponents ( fileName ) ;
2200
- parts . pop ( ) ;
2270
+ export function getPackageScopeForPath ( dir : string , state : ModuleResolutionState ) : PackageJsonInfo | undefined {
2271
+ const parts = getPathComponents ( dir ) ;
2201
2272
while ( parts . length > 0 ) {
2202
2273
const pkg = getPackageJsonInfo ( getPathFromPathComponents ( parts ) , /*onlyRecordFailures*/ false , state ) ;
2203
2274
if ( pkg ) {
@@ -2362,7 +2433,7 @@ function noKeyStartsWithDot(obj: MapLike<unknown>) {
2362
2433
}
2363
2434
2364
2435
function loadModuleFromSelfNameReference ( extensions : Extensions , moduleName : string , directory : string , state : ModuleResolutionState , cache : ModuleResolutionCache | undefined , redirectedReference : ResolvedProjectReference | undefined ) : SearchResult < Resolved > {
2365
- const directoryPath = getNormalizedAbsolutePath ( combinePaths ( directory , "dummy" ) , state . host . getCurrentDirectory ?.( ) ) ;
2436
+ const directoryPath = getNormalizedAbsolutePath ( directory , state . host . getCurrentDirectory ?.( ) ) ;
2366
2437
const scope = getPackageScopeForPath ( directoryPath , state ) ;
2367
2438
if ( ! scope || ! scope . contents . packageJsonContent . exports ) {
2368
2439
return undefined ;
@@ -2423,7 +2494,7 @@ function loadModuleFromImports(extensions: Extensions, moduleName: string, direc
2423
2494
}
2424
2495
return toSearchResult ( /*value*/ undefined ) ;
2425
2496
}
2426
- const directoryPath = getNormalizedAbsolutePath ( combinePaths ( directory , "dummy" ) , state . host . getCurrentDirectory ?.( ) ) ;
2497
+ const directoryPath = getNormalizedAbsolutePath ( directory , state . host . getCurrentDirectory ?.( ) ) ;
2427
2498
const scope = getPackageScopeForPath ( directoryPath , state ) ;
2428
2499
if ( ! scope ) {
2429
2500
if ( state . traceEnabled ) {
0 commit comments