@@ -73,7 +73,7 @@ namespace ts {
73
73
modules : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ;
74
74
typeRefs : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
75
75
moduleNameToDirectoryMap : CacheWithRedirects < ModeAwareCacheKey , ESMap < Path , ResolvedModuleWithFailedLookupLocations > > ;
76
- dirToPackageJsonMap : ESMap < Path , string > ;
76
+ dirToPackageJsonScope : ESMap < Path , PackageJsonScope > ;
77
77
perDirPackageJsonMap : ESMap < Path , string > | undefined ;
78
78
packageJsonCache : PackageJsonInfoCache | undefined ;
79
79
} ;
@@ -1272,22 +1272,21 @@ namespace ts {
1272
1272
let modules : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ;
1273
1273
let typeRefs : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
1274
1274
const moduleNameToDirectoryMap = createCacheWithRedirects < ModeAwareCacheKey , ESMap < Path , ResolvedModuleWithFailedLookupLocations > > ( state . compilerOptions ) ;
1275
- const dirToPackageJsonMap = new Map < Path , string > ( ) ;
1275
+ const dirToPackageJsonScope = new Map < Path , PackageJsonScope > ( ) ;
1276
1276
let perDirPackageJsonMap : ESMap < Path , string > | undefined ;
1277
1277
const getCanonicalFileName = createGetCanonicalFileName ( state . program ! . useCaseSensitiveFileNames ( ) ) ;
1278
1278
state . program ! . getSourceFiles ( ) . forEach ( f => {
1279
1279
modules = toPerDirectoryCache ( state , getCanonicalFileName , modules , getResolvedModuleOfResolution , f , f . resolvedModules , moduleNameToDirectoryMap ) ;
1280
1280
typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , getResolvedTypeReferenceDirectiveOfResolution , f , f . resolvedTypeReferenceDirectiveNames ) ;
1281
- if ( f . packageJsonScope ) {
1281
+ if ( f . packageJsonScope ?. info ) {
1282
1282
const dirPath = getDirectoryPath ( f . resolvedPath ) ;
1283
- if ( ! dirToPackageJsonMap ?. has ( dirPath ) ) {
1284
- const result = last ( f . packageJsonLocations ! ) ;
1285
- ( perDirPackageJsonMap ??= new Map ( ) ) . set ( dirPath , result ) ;
1283
+ if ( ! dirToPackageJsonScope ?. has ( dirPath ) ) {
1284
+ ( perDirPackageJsonMap ??= new Map ( ) ) . set ( dirPath , getPackageJsconLocationFromScope ( f . packageJsonScope ) ! ) ;
1286
1285
moduleNameToDirectorySet (
1287
- dirToPackageJsonMap ,
1286
+ dirToPackageJsonScope ,
1288
1287
dirPath ,
1289
- result ,
1290
- identity ,
1288
+ f . packageJsonScope ,
1289
+ getPackageJsconLocationFromScope ,
1291
1290
dir => toPath ( dir , state . program ! . getCurrentDirectory ( ) , getCanonicalFileName ) ,
1292
1291
ancestorPath => perDirPackageJsonMap ?. delete ( ancestorPath ) ,
1293
1292
) ;
@@ -1304,7 +1303,7 @@ namespace ts {
1304
1303
modules,
1305
1304
typeRefs,
1306
1305
moduleNameToDirectoryMap,
1307
- dirToPackageJsonMap ,
1306
+ dirToPackageJsonScope ,
1308
1307
perDirPackageJsonMap,
1309
1308
packageJsonCache : state . program ! . getModuleResolutionCache ( ) ?. getPackageJsonInfoCache ( ) . clone ( ) ,
1310
1309
} ;
@@ -1919,6 +1918,7 @@ namespace ts {
1919
1918
) : OldBuildInfoProgram | undefined {
1920
1919
if ( ! cacheResolutions && ! resuableCacheResolutions ) return undefined ;
1921
1920
const fileExistsMap = new Map < string , boolean > ( ) ;
1921
+ const packageJsonInfoMap = new Map < string , PackageJsonInfo | false > ( ) ;
1922
1922
const affectingLoationsSameMap = new Map < string , boolean > ( ) ;
1923
1923
1924
1924
type Resolution = ResolvedModuleWithFailedLookupLocations & ResolvedTypeReferenceDirectiveWithFailedLookupLocations ;
@@ -1929,6 +1929,7 @@ namespace ts {
1929
1929
const decodedResolvedTypeRefs : DecodedResolvedMap = createCacheWithRedirects ( compilerOptions ) ;
1930
1930
const decodedModuleNameToDirectoryMap : DecodedModuleNameToDirectoryMap = createCacheWithRedirects ( compilerOptions ) ;
1931
1931
let decodedPackageJsonMap : ESMap < Path , string > | undefined ;
1932
+ let packageJsonScopes : ESMap < string , PackageJsonScope | false > | undefined ;
1932
1933
let decodedHashes : ESMap < ProgramBuildInfoAbsoluteFileId , string | undefined > | undefined ;
1933
1934
1934
1935
let resolutions : ( Resolution | false ) [ ] | undefined ;
@@ -1959,7 +1960,7 @@ namespace ts {
1959
1960
/*moduleNameToDirectoryMap*/ undefined ,
1960
1961
/*decodedModuleNameToDirectoryMap*/ undefined ,
1961
1962
) ,
1962
- getPackageJsonPath ,
1963
+ getPackageJsonScope ,
1963
1964
} ;
1964
1965
1965
1966
function fileExists ( fileName : string ) {
@@ -1968,13 +1969,19 @@ namespace ts {
1968
1969
return result ;
1969
1970
}
1970
1971
1972
+ function getPackageJsonInfo ( fileName : string ) {
1973
+ let result = packageJsonInfoMap . get ( fileName ) ;
1974
+ if ( result === undefined ) packageJsonInfoMap . set ( fileName , result = host . getPackageJsonInfo ( fileName ) || false ) ;
1975
+ return result || undefined ;
1976
+ }
1977
+
1971
1978
function affectingLocationsSame (
1972
1979
fileName : string ,
1973
1980
expected : PackageJsonInfo | boolean | string | undefined
1974
1981
) : boolean {
1975
1982
let result = affectingLoationsSameMap . get ( fileName ) ;
1976
1983
if ( result !== undefined ) return result ;
1977
- const packageJsonInfo = host . getPackageJsonInfo ( fileName ) ;
1984
+ const packageJsonInfo = getPackageJsonInfo ( fileName ) ;
1978
1985
const currentText = typeof packageJsonInfo === "object" ? packageJsonInfo . packageJsonText : undefined ;
1979
1986
if ( isString ( expected ) ) {
1980
1987
result = ! ! currentText && ( host . createHash ?? generateDjb2Hash ) ( currentText ) === expected ;
@@ -1987,10 +1994,22 @@ namespace ts {
1987
1994
return result ;
1988
1995
}
1989
1996
1990
- function getPackageJsonPath ( dirPath : Path ) {
1991
- const fromCache = cacheResolutions ?. dirToPackageJsonMap ?. get ( dirPath ) ;
1997
+ function getPackageJsonScope ( dirPath : Path ) : PackageJsonScope | undefined {
1998
+ const fromCache = cacheResolutions ?. dirToPackageJsonScope ?. get ( dirPath ) ;
1992
1999
if ( fromCache ) {
1993
- return fileExists ( fromCache ) ? fromCache : undefined ;
2000
+ const packageJson = getPackageJsconLocationFromScope ( fromCache ) ! ;
2001
+ let result = packageJsonScopes ?. get ( packageJson ) ;
2002
+ if ( result === undefined ) {
2003
+ ( packageJsonScopes ??= new Map ( ) ) . set (
2004
+ packageJson ,
2005
+ result = affectingLocationsSame ( packageJson , fromCache . info ) ?
2006
+ fromCache :
2007
+ fileExists ( packageJson ) ?
2008
+ { info : getPackageJsonInfo ( packageJson ) , affectingLocations : [ packageJson ] } :
2009
+ false
2010
+ ) ;
2011
+ }
2012
+ return result || undefined ;
1994
2013
}
1995
2014
if ( ! resuableCacheResolutions ?. cache . packageJsons ) return ;
1996
2015
if ( ! decodedPackageJsonMap ) {
@@ -2009,8 +2028,22 @@ namespace ts {
2009
2028
) ;
2010
2029
}
2011
2030
}
2012
- const fromDecoded = decodedPackageJsonMap . get ( dirPath ) ;
2013
- return fromDecoded && fileExists ( fromDecoded ) ? fromDecoded : undefined ;
2031
+ return toPackageJsonScope ( decodedPackageJsonMap . get ( dirPath ) ) ;
2032
+ }
2033
+
2034
+ function toPackageJsonScope ( file : string | undefined ) : PackageJsonScope | undefined {
2035
+ if ( ! file ) return undefined ;
2036
+ let result = packageJsonScopes ?. get ( file ) ;
2037
+ if ( result !== undefined ) return result || undefined ;
2038
+ ( packageJsonScopes ??= new Map ( ) ) ;
2039
+ if ( fileExists ( file ) ) {
2040
+ result = {
2041
+ info : getPackageJsonInfo ( file ) ,
2042
+ affectingLocations : [ file ]
2043
+ } ;
2044
+ }
2045
+ packageJsonScopes . set ( file , result || false ) ;
2046
+ return result ;
2014
2047
}
2015
2048
2016
2049
function getResolvedFromCache < T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations > (
0 commit comments