Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3172629
Add new option to cacheResolution (No actual functionality yet)
sheetalkamat Jun 30, 2022
e1600ab
Add baselining of modules and type refs
sheetalkamat Dec 9, 2022
666eece
Add tests
sheetalkamat Jul 1, 2022
1eeea26
Store resolved module and type reference resolution cache in buildinfo
sheetalkamat Jul 7, 2022
76100d7
Read reusable module cache information from the buildinfo
sheetalkamat Jul 8, 2022
4bc74b9
Add reusing cache stub
sheetalkamat Jul 8, 2022
23cfc79
Buildinfo resolutions actually reused
sheetalkamat Jul 18, 2022
0132dc0
Handle project reference redirects for the module and type reference …
sheetalkamat Jul 22, 2022
d55ab98
Test for module resolutions from different directories
sheetalkamat Jul 22, 2022
ce07d0e
Handle resolutions that would be same in ancestor directory and can b…
sheetalkamat Jul 23, 2022
959206d
Do not store failed lookups with cacheResolution option if it is reso…
sheetalkamat Jul 25, 2022
0446430
Add tests where module resolution caches should reuse the resolutions…
sheetalkamat Jul 25, 2022
207226a
Reusing resolutions in tsserver scenario
sheetalkamat Jul 25, 2022
591960e
Set old program build info as a location to look for from module reso…
sheetalkamat Jul 25, 2022
71ebed9
Modify resolution cache to update on program creation completion
sheetalkamat Nov 29, 2022
4d127fa
Remove files that are not in program from cache of unresolved imports
sheetalkamat Jul 27, 2022
feb3220
Tests for unresolved imports from multiple places
sheetalkamat Jul 27, 2022
84cda33
More tests for cache reuse directory structure
sheetalkamat Jul 27, 2022
2219b5d
Start using cache as perDirectory lookup
sheetalkamat Nov 16, 2022
6261b6c
Add tests where cache resoluition is incorrectly used because of not …
sheetalkamat Jul 27, 2022
e1ed9ea
Store package json hash in buildinfo
sheetalkamat Nov 29, 2022
7de4780
Use the hashes to verify the affecting location before using the reso…
sheetalkamat Jul 28, 2022
5c11252
Add tests for package json edits
sheetalkamat Aug 2, 2022
e21738c
Store package.json for the directories in buildinfo
sheetalkamat Aug 2, 2022
be5ecb2
During cacheResolutions dont watch failed lookups and dont look at th…
sheetalkamat Aug 3, 2022
78e215f
Dont store package json path if its found in same directory
sheetalkamat Aug 4, 2022
1e123ec
Cache packagejson scopes per directory
sheetalkamat Dec 9, 2022
81a1af9
Dont store isExternalLibraryImport in the buildInfo
sheetalkamat Dec 9, 2022
3115572
Store resolvedFileName as fileId instead of structure if thats the on…
sheetalkamat Dec 10, 2022
2bb56da
Always respect preserveSymlinks
sheetalkamat Dec 10, 2022
b179c7c
Dont store originalPath as separate, instead store originalPath || re…
sheetalkamat Dec 10, 2022
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cache packagejson scopes per directory
  • Loading branch information
sheetalkamat committed Dec 9, 2022
commit 1e123ec1a6ed1d79a606c3920c9fe61a79a39862
82 changes: 60 additions & 22 deletions src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
getOriginalOrResolvedModuleFileName,
getOriginalOrResolvedTypeReferenceFileName,
getOwnKeys,
getPackageJsonLocationFromScope,
getRelativePathFromDirectory,
getTsBuildInfoEmitOutputFilePath,
handleNoEmitOptions,
Expand All @@ -66,7 +67,6 @@ import {
isJsonSourceFile,
isNumber,
isString,
last,
map,
mapDefined,
maybeBind,
Expand All @@ -78,8 +78,9 @@ import {
OldBuildInfoProgramConstructor,
OldBuildInfoProgramHost,
outFile,
PackageJsonInfo,
PackageJsonInfoCache,
PackageJsonInfoContents,
PackageJsonScope,
Path,
PerDirectoryAndNonRelativeNameCache,
PerNonRelativeNameCache,
Expand Down Expand Up @@ -187,7 +188,7 @@ export interface ReusableBuilderProgramState extends BuilderState {
modules: PerDirectoryAndNonRelativeNameCache<ResolvedModuleWithFailedLookupLocations> | undefined;
typeRefs: PerDirectoryAndNonRelativeNameCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations> | undefined;
packageJsons: Map<Path, string> | undefined;
nonRelativePackageJsonsCache: PerNonRelativeNameCache<string> | undefined;
packageJsonScopes: PerNonRelativeNameCache<PackageJsonScope> | undefined;
packageJsonCache: PackageJsonInfoCache | undefined;
};
resuableCacheResolutions?: {
Expand Down Expand Up @@ -1472,20 +1473,19 @@ function getCacheResolutions(state: BuilderProgramState) {
let modules: PerDirectoryAndNonRelativeNameCache<ResolvedModuleWithFailedLookupLocations> | undefined;
let typeRefs: PerDirectoryAndNonRelativeNameCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations> | undefined;
let packageJsons: Map<Path, string> | undefined;
let nonRelativePackageJsonsCache: PerNonRelativeNameCache<string> | undefined;
let packageJsonScopes: PerNonRelativeNameCache<PackageJsonScope> | undefined;
for (const f of state.program!.getSourceFiles()) {
modules = toPerDirectoryAndNonRelativeNameCache(state, modules, getOriginalOrResolvedModuleFileName, f.resolvedModules, f);
typeRefs = toPerDirectoryAndNonRelativeNameCache(state, typeRefs, getOriginalOrResolvedTypeReferenceFileName, f.resolvedTypeReferenceDirectiveNames, f);
if (f.packageJsonScope) {
if (f.packageJsonScope?.contents) {
const dirPath = getDirectoryPath(f.resolvedPath);
if (!nonRelativePackageJsonsCache?.getWithPath(dirPath)) {
const result = last(f.packageJsonLocations!);
(packageJsons ??= new Map()).set(dirPath, result);
(nonRelativePackageJsonsCache ??= createPerNonRelativeNameCache(
if (!packageJsonScopes?.getWithPath(dirPath)) {
(packageJsons ??= new Map()).set(dirPath, getPackageJsonLocationFromScope(f.packageJsonScope));
(packageJsonScopes ??= createPerNonRelativeNameCache(
state.program!.getCurrentDirectory(),
state.program!.getCanonicalFileName,
identity,
)).setWithPath(dirPath, result, ancestorPath => packageJsons!.delete(ancestorPath));
getPackageJsonLocationFromScope,
)).setWithPath(dirPath, f.packageJsonScope, ancestorPath => packageJsons!.delete(ancestorPath));
}
}
}
Expand All @@ -1498,7 +1498,7 @@ function getCacheResolutions(state: BuilderProgramState) {
modules,
typeRefs,
packageJsons,
nonRelativePackageJsonsCache,
packageJsonScopes,
packageJsonCache: state.program!.getModuleResolutionCache()?.getPackageJsonInfoCache().clone(),
};
}
Expand Down Expand Up @@ -2158,6 +2158,7 @@ export function createOldBuildInfoProgram(
if (!cacheResolutions && !resuableCacheResolutions) return undefined;
const fileExistsMap = new Map<string, boolean>();
const affectingLoationsSameMap = new Map<string, boolean>();
const packageJsonInfoContentsMap = new Map<string, PackageJsonInfoContents | false>();

type Resolution = ResolvedModuleWithFailedLookupLocations & ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
type ResolutionEntry = [name: string, resolutionId: ProgramBuildInfoResolutionId, mode: ResolutionMode];
Expand All @@ -2168,6 +2169,7 @@ export function createOldBuildInfoProgram(
const reusableResolvedModules = intializeReusableResolutionsCache(resuableCacheResolutions?.cache.modules);
const reusableResolvedTypeRefs = intializeReusableResolutionsCache(resuableCacheResolutions?.cache.typeRefs);
let decodedPackageJsons: PerNonRelativeNameCache<string> | undefined;
let packageJsonScopes: Map<string, PackageJsonScope | false> | undefined;
let decodedHashes: Map<ProgramBuildInfoAbsoluteFileId, string | undefined> | undefined;
let resolutions: (Resolution | false)[] | undefined;
let originalPathOrResolvedFileNames: string[] | undefined;
Expand All @@ -2194,7 +2196,7 @@ export function createOldBuildInfoProgram(
dirPath,
redirectedReference,
),
getPackageJsonPath,
getPackageJsonScope,
};

function intializeReusableResolutionsCache(reusable: ProgramBuildInfoResolutionCacheWithRedirects | undefined): ReusableResolutionsCache | undefined {
Expand All @@ -2209,7 +2211,7 @@ export function createOldBuildInfoProgram(

function affectingLocationsSame(
fileName: string,
expected: PackageJsonInfo | boolean | string | undefined
expected: PackageJsonInfoContents | string | undefined
): boolean {
let result = affectingLoationsSameMap.get(fileName);
if (result !== undefined) return result;
Expand All @@ -2219,17 +2221,34 @@ export function createOldBuildInfoProgram(
result = !!currentText && (host.createHash ?? generateDjb2Hash)(currentText) === expected;
}
else {
const expectedText = typeof expected === "object" ? expected.contents.packageJsonText : undefined;
result = currentText === expectedText;
result = currentText === expected?.packageJsonText;
}
affectingLoationsSameMap.set(fileName, result);
return result;
}

function getPackageJsonPath(dir: string) {
const fromCache = cacheResolutions?.nonRelativePackageJsonsCache?.get(dir);
function getPackageJsonInfoContents(fileName: string) {
let result = packageJsonInfoContentsMap.get(fileName);
if (result === undefined) packageJsonInfoContentsMap.set(fileName, result = host.getPackageJsonInfo(fileName)?.contents || false);
return result || undefined;
}

function getPackageJsonScope(dir: string): PackageJsonScope | undefined{
const fromCache = cacheResolutions?.packageJsonScopes?.get(dir);
if (fromCache) {
return fileExists(fromCache) ? fromCache : undefined;
const packageJson = getPackageJsonLocationFromScope(fromCache)!;
let result = packageJsonScopes?.get(packageJson);
if (result === undefined) {
(packageJsonScopes ??= new Map()).set(
packageJson,
result = affectingLocationsSame(packageJson, fromCache.contents) ?
fromCache :
fileExists(packageJson) ?
{ contents: getPackageJsonInfoContents(packageJson), affectingLocations: [packageJson] } :
false
);
}
return result || undefined;
}
if (!resuableCacheResolutions?.cache.packageJsons) return;
if (!decodedPackageJsons) {
Expand All @@ -2252,8 +2271,27 @@ export function createOldBuildInfoProgram(
decodedPackageJsons.setWithPath(dirPath, packageJson, noop);
}
}
const fromDecoded = decodedPackageJsons.get(dir);
return fromDecoded && fileExists(fromDecoded) ? fromDecoded : undefined;
return toPackageJsonScope(decodedPackageJsons.get(dir));
}

function toPackageJsonScope(file: string | undefined): PackageJsonScope | undefined {
if (!file) return undefined;
let result = packageJsonScopes?.get(file);
if (result !== undefined) return result || undefined;
(packageJsonScopes ??= new Map());
if (fileExists(file)) {
result = {
contents: getPackageJsonInfoContents(file),
affectingLocations: [file]
};
}
packageJsonScopes.set(file, result || false);
return result;
}

function getPackageJsonContentsFromCachedResolutions(fileName: string) {
const info = cacheResolutions!.packageJsonCache?.getPackageJsonInfo(fileName);
return typeof info === "object" ? info.contents : undefined;
}

function getResolvedFromCache<T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations>(
Expand All @@ -2277,7 +2315,7 @@ export function createOldBuildInfoProgram(
fromCache.affectingLocations,
fileName => affectingLocationsSame(
fileName,
cacheResolutions!.packageJsonCache?.getPackageJsonInfo(fileName)
getPackageJsonContentsFromCachedResolutions(fileName)
)
) ? fromCache : undefined;
}
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import {
ClassLikeDeclaration,
ClassStaticBlockDeclaration,
clear,
combinePaths,
compareDiagnostics,
comparePaths,
compareValues,
Expand Down Expand Up @@ -301,6 +300,7 @@ import {
getOriginalNode,
getOrUpdate,
getOwnKeys,
getPackageJsonLocationFromScope,
getParameterSymbolFromJSDoc,
getParseTreeNode,
getPropertyAssignmentAliasLikeExpression,
Expand Down Expand Up @@ -338,8 +338,8 @@ import {
hasAccessorModifier,
hasAmbientModifier,
hasContextSensitiveParameters,
HasDecorators,
hasDecorators,
HasDecorators,
hasDynamicName,
hasEffectiveModifier,
hasEffectiveModifiers,
Expand All @@ -348,8 +348,8 @@ import {
hasExtension,
HasIllegalDecorators,
HasIllegalModifiers,
HasInitializer,
hasInitializer,
HasInitializer,
hasJSDocNodes,
hasJSDocParameterTags,
hasJsonModuleEmitEnabled,
Expand Down Expand Up @@ -4730,19 +4730,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (ext === Extension.Ts || ext === Extension.Js || ext === Extension.Tsx || ext === Extension.Jsx) {
const scope = currentSourceFile.packageJsonScope;
const targetExt = ext === Extension.Ts ? Extension.Mts : ext === Extension.Js ? Extension.Mjs : undefined;
if (scope && !scope.contents.packageJsonContent.type) {
if (scope?.contents && !scope.contents.packageJsonContent.type) {
if (targetExt) {
diagnosticDetails = chainDiagnosticMessages(
/*details*/ undefined,
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_change_its_file_extension_to_0_or_add_the_field_type_Colon_module_to_1,
targetExt,
combinePaths(scope.packageDirectory, "package.json"));
getPackageJsonLocationFromScope(scope));
}
else {
diagnosticDetails = chainDiagnosticMessages(
/*details*/ undefined,
Diagnostics.To_convert_this_file_to_an_ECMAScript_module_add_the_field_type_Colon_module_to_0,
combinePaths(scope.packageDirectory, "package.json"));
getPackageJsonLocationFromScope(scope));
}
}
else {
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5154,6 +5154,14 @@
"category": "Message",
"code": 6261
},
"Directory '{0}' resolves to '{1}' scope according to cache.": {
"category": "Message",
"code": 6263
},
"Directory '{0}' has no containing package.json scope according to cache.": {
"category": "Message",
"code": 6264
},

"Directory '{0}' has no containing package.json scope. Imports will not resolve.": {
"category": "Message",
Expand Down
Loading