Skip to content

Commit b32afb5

Browse files
authored
Merge pull request #31985 from microsoft/buildInfoData
Make paths in tsbuildinfo file to be relative to the file
2 parents f1c781f + d36099c commit b32afb5

File tree

112 files changed

+1917
-1764
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1917
-1764
lines changed

src/compiler/builder.ts

Lines changed: 129 additions & 56 deletions
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,8 @@ namespace ts {
969969
return typeAcquisition;
970970
}
971971

972-
function getOptionNameMap(): OptionNameMap {
972+
/* @internal */
973+
export function getOptionNameMap(): OptionNameMap {
973974
return optionNameMapCache || (optionNameMapCache = createOptionNameMap(optionDeclarations));
974975
}
975976

src/compiler/emitter.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,16 @@ namespace ts {
249249
};
250250

251251
function emitSourceFileOrBundle({ jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, buildInfoPath }: EmitFileNames, sourceFileOrBundle: SourceFile | Bundle | undefined) {
252+
let buildInfoDirectory: string | undefined;
252253
if (buildInfoPath && sourceFileOrBundle && isBundle(sourceFileOrBundle)) {
254+
buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath, host.getCurrentDirectory()));
253255
bundleBuildInfo = {
254-
commonSourceDirectory: host.getCommonSourceDirectory(),
255-
sourceFiles: sourceFileOrBundle.sourceFiles.map(file => file.fileName)
256+
commonSourceDirectory: relativeToBuildInfo(host.getCommonSourceDirectory()),
257+
sourceFiles: sourceFileOrBundle.sourceFiles.map(file => relativeToBuildInfo(getNormalizedAbsolutePath(file.fileName, host.getCurrentDirectory())))
256258
};
257259
}
258-
emitJsFileOrBundle(sourceFileOrBundle, jsFilePath, sourceMapFilePath);
259-
emitDeclarationFileOrBundle(sourceFileOrBundle, declarationFilePath, declarationMapPath);
260+
emitJsFileOrBundle(sourceFileOrBundle, jsFilePath, sourceMapFilePath, relativeToBuildInfo);
261+
emitDeclarationFileOrBundle(sourceFileOrBundle, declarationFilePath, declarationMapPath, relativeToBuildInfo);
260262
emitBuildInfo(bundleBuildInfo, buildInfoPath);
261263

262264
if (!emitSkipped && emittedFilesList) {
@@ -278,6 +280,10 @@ namespace ts {
278280
emittedFilesList.push(declarationMapPath);
279281
}
280282
}
283+
284+
function relativeToBuildInfo(path: string) {
285+
return ensurePathIsNonModuleName(getRelativePathFromDirectory(buildInfoDirectory!, path, host.getCanonicalFileName));
286+
}
281287
}
282288

283289
function emitBuildInfo(bundle: BundleBuildInfo | undefined, buildInfoPath: string | undefined) {
@@ -291,7 +297,11 @@ namespace ts {
291297
writeFile(host, emitterDiagnostics, buildInfoPath, getBuildInfoText({ bundle, program, version }), /*writeByteOrderMark*/ false);
292298
}
293299

294-
function emitJsFileOrBundle(sourceFileOrBundle: SourceFile | Bundle | undefined, jsFilePath: string | undefined, sourceMapFilePath: string | undefined) {
300+
function emitJsFileOrBundle(
301+
sourceFileOrBundle: SourceFile | Bundle | undefined,
302+
jsFilePath: string | undefined,
303+
sourceMapFilePath: string | undefined,
304+
relativeToBuildInfo: (path: string) => string) {
295305
if (!sourceFileOrBundle || emitOnlyDtsFiles || !jsFilePath) {
296306
return;
297307
}
@@ -314,7 +324,8 @@ namespace ts {
314324
inlineSourceMap: compilerOptions.inlineSourceMap,
315325
inlineSources: compilerOptions.inlineSources,
316326
extendedDiagnostics: compilerOptions.extendedDiagnostics,
317-
writeBundleFileInfo: !!bundleBuildInfo
327+
writeBundleFileInfo: !!bundleBuildInfo,
328+
relativeToBuildInfo
318329
};
319330

320331
// Create a printer to print the nodes
@@ -335,7 +346,11 @@ namespace ts {
335346
if (bundleBuildInfo) bundleBuildInfo.js = printer.bundleFileInfo;
336347
}
337348

338-
function emitDeclarationFileOrBundle(sourceFileOrBundle: SourceFile | Bundle | undefined, declarationFilePath: string | undefined, declarationMapPath: string | undefined) {
349+
function emitDeclarationFileOrBundle(
350+
sourceFileOrBundle: SourceFile | Bundle | undefined,
351+
declarationFilePath: string | undefined,
352+
declarationMapPath: string | undefined,
353+
relativeToBuildInfo: (path: string) => string) {
339354
if (!sourceFileOrBundle || !(declarationFilePath && !isInJSFile(sourceFileOrBundle))) {
340355
return;
341356
}
@@ -366,7 +381,8 @@ namespace ts {
366381
extendedDiagnostics: compilerOptions.extendedDiagnostics,
367382
onlyPrintJsDocStyle: true,
368383
writeBundleFileInfo: !!bundleBuildInfo,
369-
recordInternalSection: !!bundleBuildInfo
384+
recordInternalSection: !!bundleBuildInfo,
385+
relativeToBuildInfo
370386
};
371387

372388
const declarationPrinter = createPrinter(printerOptions, {
@@ -613,10 +629,14 @@ namespace ts {
613629
getNewLine(): string;
614630
}
615631

616-
function createSourceFilesFromBundleBuildInfo(bundle: BundleBuildInfo): ReadonlyArray<SourceFile> {
632+
function createSourceFilesFromBundleBuildInfo(bundle: BundleBuildInfo, buildInfoDirectory: string, host: EmitUsingBuildInfoHost): ReadonlyArray<SourceFile> {
617633
const sourceFiles = bundle.sourceFiles.map(fileName => {
618634
const sourceFile = createNode(SyntaxKind.SourceFile, 0, 0) as SourceFile;
619-
sourceFile.fileName = fileName;
635+
sourceFile.fileName = getRelativePathFromDirectory(
636+
host.getCurrentDirectory(),
637+
getNormalizedAbsolutePath(fileName, buildInfoDirectory),
638+
!host.useCaseSensitiveFileNames()
639+
);
620640
sourceFile.text = "";
621641
sourceFile.statements = createNodeArray();
622642
return sourceFile;
@@ -660,6 +680,7 @@ namespace ts {
660680

661681
const buildInfo = getBuildInfo(buildInfoText);
662682
if (!buildInfo.bundle || !buildInfo.bundle.js || (declarationText && !buildInfo.bundle.dts)) return buildInfoPath!;
683+
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath!, host.getCurrentDirectory()));
663684
const ownPrependInput = createInputFiles(
664685
jsFileText,
665686
declarationText!,
@@ -675,11 +696,11 @@ namespace ts {
675696
);
676697
const outputFiles: OutputFile[] = [];
677698
const prependNodes = createPrependNodes(config.projectReferences, getCommandLine, f => host.readFile(f));
678-
const sourceFilesForJsEmit = createSourceFilesFromBundleBuildInfo(buildInfo.bundle);
699+
const sourceFilesForJsEmit = createSourceFilesFromBundleBuildInfo(buildInfo.bundle, buildInfoDirectory, host);
679700
const emitHost: EmitHost = {
680701
getPrependNodes: memoize(() => [...prependNodes, ownPrependInput]),
681702
getCanonicalFileName: host.getCanonicalFileName,
682-
getCommonSourceDirectory: () => buildInfo.bundle!.commonSourceDirectory,
703+
getCommonSourceDirectory: () => getNormalizedAbsolutePath(buildInfo.bundle!.commonSourceDirectory, buildInfoDirectory),
683704
getCompilerOptions: () => config.options,
684705
getCurrentDirectory: () => host.getCurrentDirectory(),
685706
getNewLine: () => host.getNewLine(),
@@ -775,6 +796,7 @@ namespace ts {
775796
let write = writeBase;
776797
let isOwnFileEmit: boolean;
777798
const bundleFileInfo = printerOptions.writeBundleFileInfo ? { sections: [] } as BundleFileInfo : undefined;
799+
const relativeToBuildInfo = bundleFileInfo ? Debug.assertDefined(printerOptions.relativeToBuildInfo) : undefined;
778800
const recordInternalSection = printerOptions.recordInternalSection;
779801
let sourceFileTextPos = 0;
780802
let sourceFileTextKind: BundleFileTextLikeKind = BundleFileSectionKind.Text;
@@ -943,7 +965,13 @@ namespace ts {
943965
if (prepend.oldFileOfCurrentEmit) bundleFileInfo.sections.push(...newSections);
944966
else {
945967
newSections.forEach(section => Debug.assert(isBundleFileTextLike(section)));
946-
bundleFileInfo.sections.push({ pos, end: writer.getTextPos(), kind: BundleFileSectionKind.Prepend, data: (prepend as UnparsedSource).fileName, texts: newSections as BundleFileTextLike[] });
968+
bundleFileInfo.sections.push({
969+
pos,
970+
end: writer.getTextPos(),
971+
kind: BundleFileSectionKind.Prepend,
972+
data: relativeToBuildInfo!((prepend as UnparsedSource).fileName),
973+
texts: newSections as BundleFileTextLike[]
974+
});
947975
}
948976
}
949977
}

src/compiler/tsbuild.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,11 +1299,11 @@ namespace ts {
12991299
}
13001300
}
13011301

1302-
function getOldProgram<T extends BuilderProgram>({ options, builderPrograms, readFileWithCache }: SolutionBuilderState<T>, proj: ResolvedConfigFilePath, parsed: ParsedCommandLine) {
1302+
function getOldProgram<T extends BuilderProgram>({ options, builderPrograms, compilerHost }: SolutionBuilderState<T>, proj: ResolvedConfigFilePath, parsed: ParsedCommandLine) {
13031303
if (options.force) return undefined;
13041304
const value = builderPrograms.get(proj);
13051305
if (value) return value;
1306-
return readBuilderProgram(parsed.options, readFileWithCache) as any as T;
1306+
return readBuilderProgram(parsed.options, compilerHost) as any as T;
13071307
}
13081308

13091309
function afterProgramCreate<T extends BuilderProgram>({ host, watch, builderPrograms }: SolutionBuilderState<T>, proj: ResolvedConfigFilePath, program: T) {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5730,6 +5730,7 @@ namespace ts {
57305730
/*@internal*/ writeBundleFileInfo?: boolean;
57315731
/*@internal*/ recordInternalSection?: boolean;
57325732
/*@internal*/ stripInternal?: boolean;
5733+
/*@internal*/ relativeToBuildInfo?: (path: string) => string;
57335734
}
57345735

57355736
/* @internal */

src/compiler/watch.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,21 @@ namespace ts {
437437
}
438438

439439
namespace ts {
440-
export function readBuilderProgram(compilerOptions: CompilerOptions, readFile: (path: string) => string | undefined) {
440+
export interface ReadBuildProgramHost {
441+
useCaseSensitiveFileNames(): boolean;
442+
getCurrentDirectory(): string;
443+
readFile(fileName: string): string | undefined;
444+
}
445+
export function readBuilderProgram(compilerOptions: CompilerOptions, host: ReadBuildProgramHost) {
441446
if (compilerOptions.out || compilerOptions.outFile) return undefined;
442447
const buildInfoPath = getOutputPathForBuildInfo(compilerOptions);
443448
if (!buildInfoPath) return undefined;
444-
const content = readFile(buildInfoPath);
449+
const content = host.readFile(buildInfoPath);
445450
if (!content) return undefined;
446451
const buildInfo = getBuildInfo(content);
447452
if (buildInfo.version !== version) return undefined;
448453
if (!buildInfo.program) return undefined;
449-
return createBuildProgramUsingProgramBuildInfo(buildInfo.program);
454+
return createBuildProgramUsingProgramBuildInfo(buildInfo.program, buildInfoPath, host);
450455
}
451456

452457
export function createIncrementalCompilerHost(options: CompilerOptions, system = sys): CompilerHost {
@@ -471,7 +476,7 @@ namespace ts {
471476
}: IncrementalProgramOptions<T>): T {
472477
host = host || createIncrementalCompilerHost(options);
473478
createProgram = createProgram || createEmitAndSemanticDiagnosticsBuilderProgram as any as CreateProgram<T>;
474-
const oldProgram = readBuilderProgram(options, path => host!.readFile(path)) as any as T;
479+
const oldProgram = readBuilderProgram(options, host) as any as T;
475480
return createProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences);
476481
}
477482

@@ -747,7 +752,7 @@ namespace ts {
747752
((typeDirectiveNames, containingFile, redirectedReference) => resolutionCache.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile, redirectedReference));
748753
const userProvidedResolution = !!host.resolveModuleNames || !!host.resolveTypeReferenceDirectives;
749754

750-
builderProgram = readBuilderProgram(compilerOptions, path => compilerHost.readFile(path)) as any as T;
755+
builderProgram = readBuilderProgram(compilerOptions, compilerHost) as any as T;
751756
synchronizeProgram();
752757

753758
// Update the wild card directory watch

0 commit comments

Comments
 (0)