Skip to content

Commit

Permalink
Store package.json for the directories in buildinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Aug 2, 2022
1 parent 7bdd5fc commit c703705
Show file tree
Hide file tree
Showing 19 changed files with 2,315 additions and 194 deletions.
41 changes: 39 additions & 2 deletions src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace ts {
modules: CacheWithRedirects<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined;
typeRefs: CacheWithRedirects<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined;
moduleNameToDirectoryMap: CacheWithRedirects<ModeAwareCacheKey, ESMap<Path, ResolvedModuleWithFailedLookupLocations>>;
perDirPackageJsonMap: ESMap<Path, string> | undefined;
packageJsonCache: PackageJsonInfoCache | undefined;
};
/**
Expand Down Expand Up @@ -820,13 +821,15 @@ namespace ts {
own: ProgramBuildInfoResolutionCache | undefined;
redirects: readonly ProgramBuildInfoResolutionRedirectsCache[];
};
export type ProgramBuildInfoPackageJsons = (ProgramBuildInfoAbsoluteFileId | [dirId: ProgramBuildInfoFileId, packageJson: ProgramBuildInfoAbsoluteFileId])[];
export interface ProgramBuildInfoCacheResolutions {
resolutions: readonly ProgramBuildInfoResolution[];
names: readonly string[];
hash: readonly ProgramBuildInfoHash[] | undefined;
resolutionEntries: ProgramBuildInfoResolutionEntry[];
modules?: ProgramBuildInfoResolutionCacheWithRedirects;
typeRefs?: ProgramBuildInfoResolutionCacheWithRedirects;
modules: ProgramBuildInfoResolutionCacheWithRedirects | undefined;
typeRefs: ProgramBuildInfoResolutionCacheWithRedirects | undefined;
packageJsons: ProgramBuildInfoPackageJsons | undefined;
}

export interface ProgramMultiFileEmitBuildInfo {
Expand Down Expand Up @@ -1113,6 +1116,7 @@ namespace ts {
const cacheResolutions = getCacheResolutions(state);
const modules = toProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions?.modules);
const typeRefs = toProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions?.typeRefs);
const packageJsons = toProgramBuildInfoPackageJsons(cacheResolutions?.perDirPackageJsonMap);
if (!resolutions) return;
Debug.assertIsDefined(names);
Debug.assertIsDefined(resolutionEntries);
Expand All @@ -1124,12 +1128,27 @@ namespace ts {
resolutionEntries,
modules,
typeRefs,
packageJsons,
},
getProgramBuildInfoFilePathDecoder: memoize(() => getProgramBuildInfoFilePathDecoder(fileNames, buildInfoPath, currentDirectory, getCanonicalFileName))
};
return state.resuableCacheResolutions.cache;
}

function toProgramBuildInfoPackageJsons(cache: ESMap<Path, string> | undefined): ProgramBuildInfoPackageJsons | undefined {
let result: ProgramBuildInfoPackageJsons | undefined;
cache?.forEach((packageJson, dirPath) => {
const packageJsonDirPath = getDirectoryPath(toPath(packageJson, currentDirectory, getCanonicalFileName));
(result ??= []).push(packageJsonDirPath === dirPath ?
toAbsoluteFileId(packageJson) :
[
toFileId(dirPath),
toAbsoluteFileId(packageJson),
]);
});
return result;
}

function toProgramBuildInfoResolutionCacheWithRedirects<T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations>(
cache: CacheWithRedirects<Path, ModeAwareCache<T>> | undefined
): ProgramBuildInfoResolutionCacheWithRedirects | undefined {
Expand Down Expand Up @@ -1251,10 +1270,27 @@ namespace ts {
let modules: CacheWithRedirects<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined;
let typeRefs: CacheWithRedirects<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined;
const moduleNameToDirectoryMap = createCacheWithRedirects<ModeAwareCacheKey, ESMap<Path, ResolvedModuleWithFailedLookupLocations>>(state.compilerOptions);
const dirToPackageJsonMap = new Map<Path, string>();
let perDirPackageJsonMap: ESMap<Path, string> | undefined;
const getCanonicalFileName = createGetCanonicalFileName(state.program!.useCaseSensitiveFileNames());
state.program!.getSourceFiles().forEach(f => {
modules = toPerDirectoryCache(state, getCanonicalFileName, modules, f, f.resolvedModules, moduleNameToDirectoryMap);
typeRefs = toPerDirectoryCache(state, getCanonicalFileName, typeRefs, f, f.resolvedTypeReferenceDirectiveNames);
if (f.packageJsonScope) {
const dirPath = getDirectoryPath(f.resolvedPath);
if (!dirToPackageJsonMap?.has(dirPath)) {
const result = last(f.packageJsonLocations!);
(perDirPackageJsonMap ??= new Map()).set(dirPath, result);
moduleNameToDirectorySet(
dirToPackageJsonMap,
dirPath,
result,
identity,
dir => toPath(dir, state.program!.getCurrentDirectory(), getCanonicalFileName),
ancestorPath => perDirPackageJsonMap?.delete(ancestorPath),
);
}
}
});
const automaticTypeDirectiveNames = state.program!.getAutomaticTypeDirectiveNames();
if (automaticTypeDirectiveNames.length) {
Expand All @@ -1266,6 +1302,7 @@ namespace ts {
modules,
typeRefs,
moduleNameToDirectoryMap,
perDirPackageJsonMap,
packageJsonCache: state.program!.getModuleResolutionCache()?.getPackageJsonInfoCache().clone(),
};
}
Expand Down
14 changes: 12 additions & 2 deletions src/testRunner/unittests/tsbuild/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,15 @@ interface Symbol {
redirects: readonly ReadableProgramBuildInfoResolutionRedirectsCache[];
};
export type ReadableProgramBuildInfoHash = string | [file: string, hash: string];
export type ReadableProgramBuildInfoPackageJsons = (string | [dir: string, packageJson: string])[];
export interface ReadableProgramBuildInfoCacheResolutions {
resolutions: readonly ReadableProgramBuildInfoResolution[];
names: readonly string[];
hash: readonly ReadableProgramBuildInfoHash[] | undefined,
resolutionEntries: ReadableProgramBuildInfoResolutionEntry[];
modules?: ReadableProgramBuildInfoResolutionCacheWithRedirects;
typeRefs?: ReadableProgramBuildInfoResolutionCacheWithRedirects;
modules: ReadableProgramBuildInfoResolutionCacheWithRedirects | undefined;
typeRefs: ReadableProgramBuildInfoResolutionCacheWithRedirects | undefined;
packageJsons: ReadableProgramBuildInfoPackageJsons | undefined;
}

type ReadableProgramMultiFileEmitBuildInfo = Omit<ProgramMultiFileEmitBuildInfo,
Expand Down Expand Up @@ -375,6 +377,7 @@ interface Symbol {
resolutionEntries,
modules: toReadableProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions.modules),
typeRefs: toReadableProgramBuildInfoResolutionCacheWithRedirects(cacheResolutions.typeRefs),
packageJsons: toReadableProgramBuildInfoPackageJsons(cacheResolutions.packageJsons),
hash: cacheResolutions.hash?.map(toReadableProgramBuildInfoHash),
};
}
Expand Down Expand Up @@ -446,6 +449,13 @@ interface Symbol {
}
: undefined;
}

function toReadableProgramBuildInfoPackageJsons(cache: ProgramBuildInfoPackageJsons | undefined): ReadableProgramBuildInfoPackageJsons | undefined {
return cache?.map((dirOrDirAndPackageJsonDir) => isArray(dirOrDirAndPackageJsonDir) ?
[toFileName(dirOrDirAndPackageJsonDir[0]), toFileName(dirOrDirAndPackageJsonDir[1])] :
toFileName(dirOrDirAndPackageJsonDir)
);
}
}

export function toPathWithSystem(sys: System, fileName: string): Path {
Expand Down
3 changes: 3 additions & 0 deletions src/testRunner/unittests/tsc/cacheResolutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ namespace ts.tscWatch.cacheResolutions {
{
subScenario: "Delete package.json",
modifyFs: fs => fs.unlinkSync(`/src/projects/project/package.json`),
discrepancyExplanation: () => [
`Buildinfo is not re-written so it has package.json map from before in incremental and no package.json map in clean build`
]
},
{
subScenario: "Add package json file with type module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ CleanBuild:
]
]
]
],
"packageJsons": [
"./node_modules/pkg0/package.json",
"./node_modules/pkg2/package.json",
"./node_modules/pkg3/package.json"
]
}
},
Expand Down Expand Up @@ -671,6 +676,11 @@ IncrementalBuild:
]
]
]
],
"packageJsons": [
"./node_modules/pkg0/package.json",
"./node_modules/pkg2/package.json",
"./node_modules/pkg3/package.json"
]
}
},
Expand Down
Loading

0 comments on commit c703705

Please sign in to comment.