@@ -55,6 +55,7 @@ import {
55
55
getNormalizedAbsolutePath ,
56
56
getOptionsNameMap ,
57
57
getOwnKeys ,
58
+ getPackageJsonLocationFromScope ,
58
59
getRelativePathFromDirectory ,
59
60
GetResolutionWithResolvedFileName ,
60
61
getResolvedModuleOfResolution ,
@@ -69,7 +70,6 @@ import {
69
70
isJsonSourceFile ,
70
71
isNumber ,
71
72
isString ,
72
- last ,
73
73
map ,
74
74
mapDefined ,
75
75
maybeBind ,
@@ -86,6 +86,7 @@ import {
86
86
outFile ,
87
87
PackageJsonInfo ,
88
88
PackageJsonInfoCache ,
89
+ PackageJsonScope ,
89
90
Path ,
90
91
Program ,
91
92
ProjectReference ,
@@ -194,7 +195,7 @@ export interface ReusableBuilderProgramState extends BuilderState {
194
195
modules : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ;
195
196
typeRefs : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
196
197
moduleNameToDirectoryMap : CacheWithRedirects < ModeAwareCacheKey , Map < Path , ResolvedModuleWithFailedLookupLocations > > ;
197
- dirToPackageJsonMap : Map < Path , string > ;
198
+ dirToPackageJsonScope : Map < Path , PackageJsonScope > ;
198
199
perDirPackageJsonMap : Map < Path , string > | undefined ;
199
200
packageJsonCache : PackageJsonInfoCache | undefined ;
200
201
} ;
@@ -1474,21 +1475,20 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
1474
1475
let typeRefs : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
1475
1476
getCanonicalFileName ??= createGetCanonicalFileName ( state . program ! . useCaseSensitiveFileNames ( ) ) ;
1476
1477
const moduleNameToDirectoryMap = createCacheWithRedirects < ModeAwareCacheKey , Map < Path , ResolvedModuleWithFailedLookupLocations > > ( state . compilerOptions ) ;
1477
- const dirToPackageJsonMap = new Map < Path , string > ( ) ;
1478
+ const dirToPackageJsonScope = new Map < Path , PackageJsonScope > ( ) ;
1478
1479
let perDirPackageJsonMap : Map < Path , string > | undefined ;
1479
1480
state . program ! . getSourceFiles ( ) . forEach ( f => {
1480
1481
modules = toPerDirectoryCache ( state , getCanonicalFileName ! , modules , getResolvedModuleOfResolution , f , f . resolvedModules , moduleNameToDirectoryMap ) ;
1481
1482
typeRefs = toPerDirectoryCache ( state , getCanonicalFileName ! , typeRefs , getResolvedTypeReferenceDirectiveOfResolution , f , f . resolvedTypeReferenceDirectiveNames ) ;
1482
- if ( f . packageJsonScope ) {
1483
+ if ( f . packageJsonScope ?. info ) {
1483
1484
const dirPath = getDirectoryPath ( f . resolvedPath ) ;
1484
- if ( ! dirToPackageJsonMap ?. has ( dirPath ) ) {
1485
- const result = last ( f . packageJsonLocations ! ) ;
1486
- ( perDirPackageJsonMap ??= new Map ( ) ) . set ( dirPath , result ) ;
1485
+ if ( ! dirToPackageJsonScope ?. has ( dirPath ) ) {
1486
+ ( perDirPackageJsonMap ??= new Map ( ) ) . set ( dirPath , getPackageJsonLocationFromScope ( f . packageJsonScope ) ! ) ;
1487
1487
moduleNameToDirectorySet (
1488
- dirToPackageJsonMap ,
1488
+ dirToPackageJsonScope ,
1489
1489
dirPath ,
1490
- result ,
1491
- identity ,
1490
+ f . packageJsonScope ,
1491
+ getPackageJsonLocationFromScope ,
1492
1492
dir => toPath ( dir , state . program ! . getCurrentDirectory ( ) , getCanonicalFileName ! ) ,
1493
1493
ancestorPath => perDirPackageJsonMap ?. delete ( ancestorPath ) ,
1494
1494
) ;
@@ -1505,7 +1505,7 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
1505
1505
modules,
1506
1506
typeRefs,
1507
1507
moduleNameToDirectoryMap,
1508
- dirToPackageJsonMap ,
1508
+ dirToPackageJsonScope ,
1509
1509
perDirPackageJsonMap,
1510
1510
packageJsonCache : state . program ! . getModuleResolutionCache ( ) ?. getPackageJsonInfoCache ( ) . clone ( ) ,
1511
1511
} ;
@@ -2170,6 +2170,7 @@ export function createOldBuildInfoProgram(
2170
2170
if ( ! cacheResolutions && ! resuableCacheResolutions ) return undefined ;
2171
2171
const fileExistsMap = new Map < string , boolean > ( ) ;
2172
2172
const affectingLoationsSameMap = new Map < string , boolean > ( ) ;
2173
+ const packageJsonInfoMap = new Map < string , PackageJsonInfo | false > ( ) ;
2173
2174
2174
2175
type Resolution = ResolvedModuleWithFailedLookupLocations & ResolvedTypeReferenceDirectiveWithFailedLookupLocations ;
2175
2176
type ResolutionEntry = [ name : string , resolutionId : ProgramBuildInfoResolutionId , mode : ResolutionMode ] ;
@@ -2179,6 +2180,7 @@ export function createOldBuildInfoProgram(
2179
2180
const decodedResolvedTypeRefs : DecodedResolvedMap = createCacheWithRedirects ( compilerOptions ) ;
2180
2181
const decodedModuleNameToDirectoryMap : DecodedModuleNameToDirectoryMap = createCacheWithRedirects ( compilerOptions ) ;
2181
2182
let decodedPackageJsonMap : Map < Path , string > | undefined ;
2183
+ let packageJsonScopes : Map < string , PackageJsonScope | false > | undefined ;
2182
2184
let decodedHashes : Map < ProgramBuildInfoAbsoluteFileId , string | undefined > | undefined ;
2183
2185
2184
2186
let resolutions : ( Resolution | false ) [ ] | undefined ;
@@ -2208,7 +2210,7 @@ export function createOldBuildInfoProgram(
2208
2210
/*moduleNameToDirectoryMap*/ undefined ,
2209
2211
/*decodedModuleNameToDirectoryMap*/ undefined ,
2210
2212
) ,
2211
- getPackageJsonPath ,
2213
+ getPackageJsonScope ,
2212
2214
} ;
2213
2215
function fileExists ( fileName : string ) {
2214
2216
let result = fileExistsMap . get ( fileName ) ;
@@ -2235,10 +2237,28 @@ export function createOldBuildInfoProgram(
2235
2237
return result ;
2236
2238
}
2237
2239
2238
- function getPackageJsonPath ( dirPath : Path ) {
2239
- const fromCache = cacheResolutions ?. dirToPackageJsonMap ?. get ( dirPath ) ;
2240
+ function getPackageJsonInfo ( fileName : string ) {
2241
+ let result = packageJsonInfoMap . get ( fileName ) ;
2242
+ if ( result === undefined ) packageJsonInfoMap . set ( fileName , result = host . getPackageJsonInfo ( fileName ) || false ) ;
2243
+ return result || undefined ;
2244
+ }
2245
+
2246
+ function getPackageJsonScope ( dirPath : Path ) : PackageJsonScope | undefined {
2247
+ const fromCache = cacheResolutions ?. dirToPackageJsonScope ?. get ( dirPath ) ;
2240
2248
if ( fromCache ) {
2241
- return fileExists ( fromCache ) ? fromCache : undefined ;
2249
+ const packageJson = getPackageJsonLocationFromScope ( fromCache ) ! ;
2250
+ let result = packageJsonScopes ?. get ( packageJson ) ;
2251
+ if ( result === undefined ) {
2252
+ ( packageJsonScopes ??= new Map ( ) ) . set (
2253
+ packageJson ,
2254
+ result = affectingLocationsSame ( packageJson , fromCache . info ) ?
2255
+ fromCache :
2256
+ fileExists ( packageJson ) ?
2257
+ { info : getPackageJsonInfo ( packageJson ) , affectingLocations : [ packageJson ] } :
2258
+ false
2259
+ ) ;
2260
+ }
2261
+ return result || undefined ;
2242
2262
}
2243
2263
if ( ! resuableCacheResolutions ?. cache . packageJsons ) return ;
2244
2264
if ( ! decodedPackageJsonMap ) {
@@ -2264,8 +2284,22 @@ export function createOldBuildInfoProgram(
2264
2284
) ;
2265
2285
}
2266
2286
}
2267
- const fromDecoded = decodedPackageJsonMap . get ( dirPath ) ;
2268
- return fromDecoded && fileExists ( fromDecoded ) ? fromDecoded : undefined ;
2287
+ return toPackageJsonScope ( decodedPackageJsonMap . get ( dirPath ) ) ;
2288
+ }
2289
+
2290
+ function toPackageJsonScope ( file : string | undefined ) : PackageJsonScope | undefined {
2291
+ if ( ! file ) return undefined ;
2292
+ let result = packageJsonScopes ?. get ( file ) ;
2293
+ if ( result !== undefined ) return result || undefined ;
2294
+ ( packageJsonScopes ??= new Map ( ) ) ;
2295
+ if ( fileExists ( file ) ) {
2296
+ result = {
2297
+ info : getPackageJsonInfo ( file ) ,
2298
+ affectingLocations : [ file ]
2299
+ } ;
2300
+ }
2301
+ packageJsonScopes . set ( file , result || false ) ;
2302
+ return result ;
2269
2303
}
2270
2304
2271
2305
function getResolvedFromCache < T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations > (
0 commit comments