Skip to content

Commit

Permalink
Dont store originalPath as separate, instead store originalPath || re…
Browse files Browse the repository at this point in the history
…solvedFileName and resolve the symlinks on read
  • Loading branch information
sheetalkamat committed Dec 10, 2022
1 parent 2bb56da commit b179c7c
Show file tree
Hide file tree
Showing 26 changed files with 2,053 additions and 1,952 deletions.
43 changes: 21 additions & 22 deletions src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
getEmitDeclarations,
getNormalizedAbsolutePath,
getOptionsNameMap,
getOriginalAndResolvedFileName,
getOriginalOrResolvedModuleFileName,
getOriginalOrResolvedTypeReferenceFileName,
getOwnKeys,
Expand All @@ -67,6 +68,7 @@ import {
isJsonSourceFile,
isNumber,
isString,
isTraceEnabled,
map,
mapDefined,
maybeBind,
Expand Down Expand Up @@ -956,7 +958,6 @@ export type ProgramMultiFileEmitBuildInfoFileInfo = string | ProgramMultiFileEmi
/** @internal */
export interface ProgramBuildInfoResolved {
readonly resolvedFileName: ProgramBuildInfoAbsoluteFileId;
readonly originalPath: ProgramBuildInfoAbsoluteFileId | undefined;
readonly packageId: PackageId | undefined;
}
/** @internal */
Expand Down Expand Up @@ -1455,10 +1456,9 @@ function getBuildInfo(state: BuilderProgramState, host: BuilderProgramHost, bund

function toProgramBuildInfoResolved(resolved: ResolvedModuleFull | ResolvedTypeReferenceDirective | undefined): ProgramBuildInfoAbsoluteFileId | ProgramBuildInfoResolved | undefined {
if (!resolved) return undefined;
const resolvedFileName = toAbsoluteFileId(resolved.resolvedFileName!);
const originalPath = resolved.originalPath ? toAbsoluteFileId(resolved.originalPath) : undefined;
if (!originalPath && !resolved.packageId) return resolvedFileName;
return { resolvedFileName, originalPath, packageId: resolved.packageId };
const resolvedFileName = toAbsoluteFileId(resolved.originalPath || resolved.resolvedFileName!);
if (!resolved.packageId) return resolvedFileName;
return { resolvedFileName, packageId: resolved.packageId };
}
}

Expand Down Expand Up @@ -2168,6 +2168,7 @@ export function createOldBuildInfoProgram(
let resolutions: (Resolution | false)[] | undefined;
let originalPathOrResolvedFileNames: string[] | undefined;
let resolutionEntries: ResolutionEntry[] | undefined;
const traceEnabled = isTraceEnabled(host.getCompilerOptions(), host.compilerHost);
return {
getCompilerOptions: () => compilerOptions,
getResolvedModule: (name, mode, dirPath, redirectedReference) => getResolvedFromCache(
Expand Down Expand Up @@ -2199,7 +2200,7 @@ export function createOldBuildInfoProgram(

function fileExists(fileName: string) {
let result = fileExistsMap.get(fileName);
if (result === undefined) fileExistsMap.set(fileName, result = host.fileExists(fileName));
if (result === undefined) fileExistsMap.set(fileName, result = host.compilerHost.fileExists(fileName));
return result;
}

Expand All @@ -2212,7 +2213,7 @@ export function createOldBuildInfoProgram(
const packageJsonInfo = host.getPackageJsonInfo(fileName);
const currentText = typeof packageJsonInfo === "object" ? packageJsonInfo.contents.packageJsonText : undefined;
if (isString(expected)) {
result = !!currentText && (host.createHash ?? generateDjb2Hash)(currentText) === expected;
result = !!currentText && (host.compilerHost.createHash ?? generateDjb2Hash)(currentText) === expected;
}
else {
result = currentText === expected?.packageJsonText;
Expand Down Expand Up @@ -2303,7 +2304,6 @@ export function createOldBuildInfoProgram(
// If we are using the cache, directly get from there
const fromCache = cache?.getFromCache(name, mode, dirPath, options);
if (fromCache) {
// TODO:: symlinks
const resolvedFileName = getResolvedFileName(fromCache);
return resolvedFileName && fileExists(resolvedFileName) && every(
fromCache.affectingLocations,
Expand Down Expand Up @@ -2336,7 +2336,7 @@ export function createOldBuildInfoProgram(
if (!reusableResolutionsCache.decoded) return undefined;
}
const resolutionId = reusableResolutionsCache.decoded.getFromCache(name, mode, dirPath, options);
return resolutionId ? toResolution(resolutionId) as T : undefined;
return resolutionId ? toResolution(resolutionId, name) as T : undefined;
}

function setBuildInfoResolutionEntries(
Expand Down Expand Up @@ -2382,12 +2382,8 @@ export function createOldBuildInfoProgram(
return resuableCacheResolutions!.cache.names[nameId - 1];
}

function toResolvedFileName(resolved: ProgramBuildInfoAbsoluteFileId | ProgramBuildInfoResolved) {
return isNumber(resolved) ? resolved : resolved.resolvedFileName;
}

function toOriginalOrResolvedFileName(resolved: ProgramBuildInfoAbsoluteFileId | ProgramBuildInfoResolved) {
return isNumber(resolved) ? resolved : resolved.originalPath || resolved.resolvedFileName;
return isNumber(resolved) ? resolved : resolved.resolvedFileName;
}

function toOriginalOrResolvedModuleFileName(resolutionId: ProgramBuildInfoResolutionId): string {
Expand All @@ -2413,13 +2409,13 @@ export function createOldBuildInfoProgram(
return affectingLocationsSame(file, hash) ? file : undefined;
}

function toResolution(resolutionId: ProgramBuildInfoResolutionId): Resolution | undefined {
function toResolution(resolutionId: ProgramBuildInfoResolutionId, name: string): Resolution | undefined {
const existing = resolutions?.[resolutionId - 1];
if (existing !== undefined) return existing || undefined;
resolutions ??= new Array(resuableCacheResolutions!.cache.resolutions.length);
const resolution = resuableCacheResolutions!.cache.resolutions[resolutionId - 1];
const resolvedFileName = resuableCacheResolutions!.getProgramBuildInfoFilePathDecoder().toFileAbsolutePath(
toResolvedFileName(resolution.resolvedModule || resolution.resolvedTypeReferenceDirective!)
toOriginalOrResolvedFileName(resolution.resolvedModule || resolution.resolvedTypeReferenceDirective!)
);
let affectingLocations: string[] | undefined;
if (fileExists(resolvedFileName) && every(resolution.affectingLocations, fileId => {
Expand All @@ -2430,8 +2426,8 @@ export function createOldBuildInfoProgram(
// Type Ref doesnt need extension
const extenstion = resolution.resolvedModule ? extensionFromPath(resolvedFileName) : undefined!;
return resolutions[resolutionId - 1] = {
resolvedModule: toResolved(resolution.resolvedModule, resolvedFileName, extenstion, /*primary*/ undefined),
resolvedTypeReferenceDirective: toResolved(resolution.resolvedTypeReferenceDirective, resolvedFileName, extenstion, !resolution.notPrimary),
resolvedModule: toResolved(resolution.resolvedModule, resolvedFileName, extenstion, name, /*primary*/ undefined),
resolvedTypeReferenceDirective: toResolved(resolution.resolvedTypeReferenceDirective, resolvedFileName, extenstion, /*name*/ undefined, !resolution.notPrimary),
affectingLocations,
resolutionDiagnostics: resolution.resolutionDiagnostics?.length ? convertToDiagnostics(resolution.resolutionDiagnostics, /*newProgram*/ undefined!) as Diagnostic[] : undefined
};
Expand All @@ -2444,18 +2440,21 @@ export function createOldBuildInfoProgram(
resolved: ProgramBuildInfoAbsoluteFileId | ProgramBuildInfoResolved | undefined,
resolvedFileName: string,
extension: Extension,
name: string | undefined,
primary: boolean | undefined,
): (ResolvedModuleFull & ResolvedTypeReferenceDirective) | undefined {
if (!resolved) return undefined;
const originalPath = isNumber(resolved) || !resolved.originalPath ?
undefined :
resuableCacheResolutions!.getProgramBuildInfoFilePathDecoder().toFileAbsolutePath(resolved.originalPath);
let originalPath: string | undefined;
const isExternalLibraryImport = pathContainsNodeModules(resolvedFileName);
if (!host.getCompilerOptions().preserveSymlinks && (!name || (isExternalLibraryImport && !isExternalModuleNameRelative(name)))) {
({ resolvedFileName, originalPath } = getOriginalAndResolvedFileName(resolvedFileName, host.compilerHost, traceEnabled));
}
const packageId = isNumber(resolved) ? undefined : resolved.packageId;
return {
resolvedFileName,
originalPath,
packageId,
isExternalLibraryImport: pathContainsNodeModules(originalPath || resolvedFileName),
isExternalLibraryImport,
extension,
primary,
};
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ function arePathsEqual(path1: string, path2: string, host: ModuleResolutionHost)
return comparePaths(path1, path2, !useCaseSensitiveFileNames) === Comparison.EqualTo;
}

function getOriginalAndResolvedFileName(fileName: string, host: ModuleResolutionHost, traceEnabled: boolean) {
/** @internal */
export function getOriginalAndResolvedFileName(fileName: string, host: ModuleResolutionHost, traceEnabled: boolean) {
const resolvedFileName = realPath(fileName, host, traceEnabled);
const pathsAreEqual = arePathsEqual(fileName, resolvedFileName, host);
return {
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1582,9 +1582,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
if (!oldProgram && typeof oldProgramOrOldBuildInfoProgramConstructor === "function") {
const state = getTemporaryModuleResolutionState(moduleResolutionCache?.getPackageJsonInfoCache(), host, options);
oldBuildInfoProgram = oldProgramOrOldBuildInfoProgramConstructor({
fileExists: fileName => host.fileExists(fileName),
compilerHost: host,
getCompilerOptions: () => options,
createHash: maybeBind(host, host.createHash),
getPackageJsonInfo: fileName => getPackageJsonInfo(getDirectoryPath(fileName), /*onlyRecordFailures*/ false, state),
});
if (oldBuildInfoProgram) {
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6988,9 +6988,8 @@ export interface OldBuildInfoProgram {

/** @internal */
export interface OldBuildInfoProgramHost {
fileExists(fileName: string): boolean;
compilerHost: CompilerHost;
getCompilerOptions(): CompilerOptions;
createHash?(data: string): string;
getPackageJsonInfo(fileName: string): PackageJsonInfo | undefined;
}

Expand Down
4 changes: 1 addition & 3 deletions src/testRunner/unittests/tsc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,9 +538,8 @@ type ReadableProgramBuildInfoFileInfo<T> = Omit<ts.BuilderState.FileInfo, "impli
impliedFormat: string | undefined;
original: T | undefined;
};
type ReadableProgramBuildInfoResolved = string | Omit<ts.ProgramBuildInfoResolved, "resolvedFileName" | "originalPath"> & {
type ReadableProgramBuildInfoResolved = string | Omit<ts.ProgramBuildInfoResolved, "resolvedFileName"> & {
readonly resolvedFileName: string;
readonly originalPath: string | undefined;
};
type ReadableProgramBuildInfoResolution = Omit<ts.ProgramBuildInfoResolution, "resolvedModule" | "resolvedTypeReferenceDirective" | "failedLookupLocations" | "affectingLocations"> & {
readonly resolutionId: ts.ProgramBuildInfoResolutionId;
Expand Down Expand Up @@ -794,7 +793,6 @@ function generateBuildInfoProgramBaseline(sys: ts.System, buildInfoPath: string,
return resolved && (ts.isNumber(resolved) ? toFileName(resolved) : {
...resolved,
resolvedFileName: toFileName(resolved.resolvedFileName),
originalPath: resolved.originalPath ? toFileName(resolved.originalPath) : undefined,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,9 +985,12 @@ Output::
/lib/tsc -b /src/project --explainFiles
Reusing resolution of module 'pkg0' from '/src/project/fileWithImports.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/pkg0.d.ts'.
Reusing resolution of module 'pkg1' from '/src/project/fileWithImports.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/pkg1.d.ts'.
Resolving real path for '/src/project/node_modules/pkg2/index.d.ts', result '/src/project/node_modules/pkg2/index.d.ts'.
Reusing resolution of type reference directive 'pkg2' from '/src/project/fileWithTypeRefs.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/node_modules/pkg2/index.d.ts'.
Resolving real path for '/src/project/node_modules/pkg3/index.d.ts', result '/src/project/node_modules/pkg3/index.d.ts'.
Reusing resolution of type reference directive 'pkg3' from '/src/project/fileWithTypeRefs.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/node_modules/pkg3/index.d.ts'.
Reusing resolution of module 'pkg0' from '/src/project/randomFileForImport.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/pkg0.d.ts'.
Resolving real path for '/src/project/node_modules/@types/pkg4/index.d.ts', result '/src/project/node_modules/@types/pkg4/index.d.ts'.
Reusing resolution of type reference directive 'pkg4' from '/src/project/__inferred type names__.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/node_modules/@types/pkg4/index.d.ts'.
lib/lib.d.ts
Default library for target 'es5'
Expand Down Expand Up @@ -1324,10 +1327,13 @@ Output::
/lib/tsc -b /src/project --explainFiles
Reusing resolution of module 'pkg0' from '/src/project/fileWithImports.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/pkg0.d.ts'.
Reusing resolution of module 'pkg1' from '/src/project/fileWithImports.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/pkg1.d.ts'.
Resolving real path for '/src/project/node_modules/pkg2/index.d.ts', result '/src/project/node_modules/pkg2/index.d.ts'.
Reusing resolution of type reference directive 'pkg2' from '/src/project/fileWithTypeRefs.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/node_modules/pkg2/index.d.ts'.
Resolving real path for '/src/project/node_modules/pkg3/index.d.ts', result '/src/project/node_modules/pkg3/index.d.ts'.
Reusing resolution of type reference directive 'pkg3' from '/src/project/fileWithTypeRefs.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/node_modules/pkg3/index.d.ts'.
Reusing resolution of module 'pkg0' from '/src/project/randomFileForImport.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/pkg0.d.ts'.
Reusing resolution of type reference directive 'pkg2' from '/src/project/randomFileForTypeRef.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/node_modules/pkg2/index.d.ts'.
Resolving real path for '/src/project/node_modules/@types/pkg4/index.d.ts', result '/src/project/node_modules/@types/pkg4/index.d.ts'.
Reusing resolution of type reference directive 'pkg4' from '/src/project/__inferred type names__.ts' found in cache from location '/src/project', it was successfully resolved to '/src/project/node_modules/@types/pkg4/index.d.ts'.
lib/lib.d.ts
Default library for target 'es5'
Expand Down
Loading

0 comments on commit b179c7c

Please sign in to comment.