Skip to content

Commit 7cb4ec1

Browse files
committed
Store package.json for the directories in buildinfo
1 parent b46f4d0 commit 7cb4ec1

19 files changed

+2315
-194
lines changed

src/compiler/builder.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ namespace ts {
7373
modules: CacheWithRedirects<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined;
7474
typeRefs: CacheWithRedirects<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined;
7575
moduleNameToDirectoryMap: CacheWithRedirects<ModeAwareCacheKey, ESMap<Path, ResolvedModuleWithFailedLookupLocations>>;
76+
perDirPackageJsonMap: ESMap<Path, string> | undefined;
7677
packageJsonCache: PackageJsonInfoCache | undefined;
7778
};
7879
/**
@@ -820,13 +821,15 @@ namespace ts {
820821
own: ProgramBuildInfoResolutionCache | undefined;
821822
redirects: readonly ProgramBuildInfoResolutionRedirectsCache[];
822823
};
824+
export type ProgramBuildInfoPackageJsons = (ProgramBuildInfoAbsoluteFileId | [dirId: ProgramBuildInfoFileId, packageJson: ProgramBuildInfoAbsoluteFileId])[];
823825
export interface ProgramBuildInfoCacheResolutions {
824826
resolutions: readonly ProgramBuildInfoResolution[];
825827
names: readonly string[];
826828
hash: readonly ProgramBuildInfoHash[] | undefined;
827829
resolutionEntries: ProgramBuildInfoResolutionEntry[];
828-
modules?: ProgramBuildInfoResolutionCacheWithRedirects;
829-
typeRefs?: ProgramBuildInfoResolutionCacheWithRedirects;
830+
modules: ProgramBuildInfoResolutionCacheWithRedirects | undefined;
831+
typeRefs: ProgramBuildInfoResolutionCacheWithRedirects | undefined;
832+
packageJsons: ProgramBuildInfoPackageJsons | undefined;
830833
}
831834

832835
export interface ProgramMultiFileEmitBuildInfo {
@@ -1113,6 +1116,7 @@ namespace ts {
11131116
const cacheResolutions = getCacheResolutions(state);
11141117
const modules = toProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions?.modules);
11151118
const typeRefs = toProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions?.typeRefs);
1119+
const packageJsons = toProgramBuildInfoPackageJsons(cacheResolutions?.perDirPackageJsonMap);
11161120
if (!resolutions) return;
11171121
Debug.assertIsDefined(names);
11181122
Debug.assertIsDefined(resolutionEntries);
@@ -1124,12 +1128,27 @@ namespace ts {
11241128
resolutionEntries,
11251129
modules,
11261130
typeRefs,
1131+
packageJsons,
11271132
},
11281133
getProgramBuildInfoFilePathDecoder: memoize(() => getProgramBuildInfoFilePathDecoder(fileNames, buildInfoPath, currentDirectory, getCanonicalFileName))
11291134
};
11301135
return state.resuableCacheResolutions.cache;
11311136
}
11321137

1138+
function toProgramBuildInfoPackageJsons(cache: ESMap<Path, string> | undefined): ProgramBuildInfoPackageJsons | undefined {
1139+
let result: ProgramBuildInfoPackageJsons | undefined;
1140+
cache?.forEach((packageJson, dirPath) => {
1141+
const packageJsonDirPath = getDirectoryPath(toPath(packageJson, currentDirectory, getCanonicalFileName));
1142+
(result ??= []).push(packageJsonDirPath === dirPath ?
1143+
toAbsoluteFileId(packageJson) :
1144+
[
1145+
toFileId(dirPath),
1146+
toAbsoluteFileId(packageJson),
1147+
]);
1148+
});
1149+
return result;
1150+
}
1151+
11331152
function toProgramBuildInfoResolutionCacheWithRedirects<T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations>(
11341153
cache: CacheWithRedirects<Path, ModeAwareCache<T>> | undefined
11351154
): ProgramBuildInfoResolutionCacheWithRedirects | undefined {
@@ -1251,10 +1270,27 @@ namespace ts {
12511270
let modules: CacheWithRedirects<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined;
12521271
let typeRefs: CacheWithRedirects<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined;
12531272
const moduleNameToDirectoryMap = createCacheWithRedirects<ModeAwareCacheKey, ESMap<Path, ResolvedModuleWithFailedLookupLocations>>(state.compilerOptions);
1273+
const dirToPackageJsonMap = new Map<Path, string>();
1274+
let perDirPackageJsonMap: ESMap<Path, string> | undefined;
12541275
const getCanonicalFileName = createGetCanonicalFileName(state.program!.useCaseSensitiveFileNames());
12551276
state.program!.getSourceFiles().forEach(f => {
12561277
modules = toPerDirectoryCache(state, getCanonicalFileName, modules, f, f.resolvedModules, moduleNameToDirectoryMap);
12571278
typeRefs = toPerDirectoryCache(state, getCanonicalFileName, typeRefs, f, f.resolvedTypeReferenceDirectiveNames);
1279+
if (f.packageJsonScope) {
1280+
const dirPath = getDirectoryPath(f.resolvedPath);
1281+
if (!dirToPackageJsonMap?.has(dirPath)) {
1282+
const result = last(f.packageJsonLocations!);
1283+
(perDirPackageJsonMap ??= new Map()).set(dirPath, result);
1284+
moduleNameToDirectorySet(
1285+
dirToPackageJsonMap,
1286+
dirPath,
1287+
result,
1288+
identity,
1289+
dir => toPath(dir, state.program!.getCurrentDirectory(), getCanonicalFileName),
1290+
ancestorPath => perDirPackageJsonMap?.delete(ancestorPath),
1291+
);
1292+
}
1293+
}
12581294
});
12591295
const automaticTypeDirectiveNames = state.program!.getAutomaticTypeDirectiveNames();
12601296
if (automaticTypeDirectiveNames.length) {
@@ -1266,6 +1302,7 @@ namespace ts {
12661302
modules,
12671303
typeRefs,
12681304
moduleNameToDirectoryMap,
1305+
perDirPackageJsonMap,
12691306
packageJsonCache: state.program!.getModuleResolutionCache()?.getPackageJsonInfoCache().clone(),
12701307
};
12711308
}

src/testRunner/unittests/tsbuild/helpers.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,15 @@ interface Symbol {
229229
redirects: readonly ReadableProgramBuildInfoResolutionRedirectsCache[];
230230
};
231231
export type ReadableProgramBuildInfoHash = string | [file: string, hash: string];
232+
export type ReadableProgramBuildInfoPackageJsons = (string | [dir: string, packageJson: string])[];
232233
export interface ReadableProgramBuildInfoCacheResolutions {
233234
resolutions: readonly ReadableProgramBuildInfoResolution[];
234235
names: readonly string[];
235236
hash: readonly ReadableProgramBuildInfoHash[] | undefined,
236237
resolutionEntries: ReadableProgramBuildInfoResolutionEntry[];
237-
modules?: ReadableProgramBuildInfoResolutionCacheWithRedirects;
238-
typeRefs?: ReadableProgramBuildInfoResolutionCacheWithRedirects;
238+
modules: ReadableProgramBuildInfoResolutionCacheWithRedirects | undefined;
239+
typeRefs: ReadableProgramBuildInfoResolutionCacheWithRedirects | undefined;
240+
packageJsons: ReadableProgramBuildInfoPackageJsons | undefined;
239241
}
240242

241243
type ReadableProgramMultiFileEmitBuildInfo = Omit<ProgramMultiFileEmitBuildInfo,
@@ -375,6 +377,7 @@ interface Symbol {
375377
resolutionEntries,
376378
modules: toReadableProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions.modules),
377379
typeRefs: toReadableProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions.typeRefs),
380+
packageJsons: toReadableProgramBuildInfoPackageJsons(cacheResolutions.packageJsons),
378381
hash: cacheResolutions.hash?.map(toReadableProgramBuildInfoHash),
379382
};
380383
}
@@ -446,6 +449,13 @@ interface Symbol {
446449
}
447450
: undefined;
448451
}
452+
453+
function toReadableProgramBuildInfoPackageJsons(cache: ProgramBuildInfoPackageJsons | undefined): ReadableProgramBuildInfoPackageJsons | undefined {
454+
return cache?.map((dirOrDirAndPackageJsonDir) => isArray(dirOrDirAndPackageJsonDir) ?
455+
[toFileName(dirOrDirAndPackageJsonDir[0]), toFileName(dirOrDirAndPackageJsonDir[1])] :
456+
toFileName(dirOrDirAndPackageJsonDir)
457+
);
458+
}
449459
}
450460

451461
export function toPathWithSystem(sys: System, fileName: string): Path {

src/testRunner/unittests/tsc/cacheResolutions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ namespace ts.tscWatch.cacheResolutions {
253253
{
254254
subScenario: "Delete package.json",
255255
modifyFs: fs => fs.unlinkSync(`/src/projects/project/package.json`),
256+
discrepancyExplanation: () => [
257+
`Buildinfo is not re-written so it has package.json map from before in incremental and no package.json map in clean build`
258+
]
256259
},
257260
{
258261
subScenario: "Add package json file with type module",

tests/baselines/reference/tsbuild/cacheResolutions/caching-resolutions-in-multi-file-scenario-discrepancies.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ CleanBuild:
345345
]
346346
]
347347
]
348+
],
349+
"packageJsons": [
350+
"./node_modules/pkg0/package.json",
351+
"./node_modules/pkg2/package.json",
352+
"./node_modules/pkg3/package.json"
348353
]
349354
}
350355
},
@@ -671,6 +676,11 @@ IncrementalBuild:
671676
]
672677
]
673678
]
679+
],
680+
"packageJsons": [
681+
"./node_modules/pkg0/package.json",
682+
"./node_modules/pkg2/package.json",
683+
"./node_modules/pkg3/package.json"
674684
]
675685
}
676686
},

0 commit comments

Comments
 (0)