Skip to content

Commit b8f6488

Browse files
authored
Build improvements (#48784)
* Use fixed time for vfs so baselining is consistent * Baseline buildinfos * Write new file text in baseline even if the file wasnt read on the shadow * Remove unnecessary debugger statement * Make sure that incremental correctness is checked with correct writeFile so we know buildInfo was written Also baseline these so its easy to verify the changes * More baselines for the tsbuildinfo * If we are writing dts file and have used file text as version, we can update the signature when doing actual emit * Make WriteFileCallback Api ready for future * Assert that there is only single source file when emitting d.ts file * Add test * Renames * More refactoring * If we are updating dts of any of the file and it affects global scope, everything needs update in signature and dts emit Fixes #42769 * Stacktrace optimization for getModified time in anticipation of using it more than fileExists wherever possible * Baseline getModifiedTime, setModifiedTime, fileExits and directoryExits for experiment * Remove unnecessary write file finger print code since its not used at all * Use modified time instead of file existence check * Remove unnecessary getModifiedTime * No need to check for file existence before reading the d.ts file * Do project reference errors before doing input/output file checks * Dont call getModifiedTimes if dts change * Passdown modified time if queried * Use modified time passed through the file watching in tsbuild * Handle force build as separate upto date status * uptodate status worker to read buildinfo and use it to determine upto date ness * No need to update output timestamps if buildinfo will determine uptodateness * Store change file set instead of hasPendingChange to be able to reuse the information * Add test that shows input file is not present * No need to check input time stamp before buildinfo * Keep buildinfos for lifetime of the solution builder and project * Store modified time along with text of buildinfo * Non composite projects dont need to track declaration change time * Pass through buildInfo so we dont have to parse it back * Save dts change time in buildinfo itself * Store dts time for --out in the buildInfo * Store hash of text in the bundle info so it can be verified before manipulating text for fast updates during prepend This helps when text changes during incremental build toggling and we determine we can just manipulate text * Since buildinfo is cached no need to maintain version check state * Store output time stamps for non incremental builds * Revert "Baseline getModifiedTime, setModifiedTime, fileExits and directoryExits for experiment" This reverts commit 7e65cd3. * Change verbose messages for upto date status * Reconcile reusable builder state and builder state so there are not two different types that are almost similar looking * Cleanup impliedFormat * Cleanup * Cleanup noEmit option * BuildInfo options emit as a flag * Factor out types for program written in buildinfo with and without bundle emit * No need to store output file stamps if not in watch mode * Cleanup * Test for single watch per file * Fix emit and error update baselines that were duplicate * More refactoring * Only copy emit state fields when backing up to restore if emit fails * Instead of maintaining delta of changes, maintain old state for those changes * Add test to verify build when input file does not change * If version of the input file does not change, dont mark as out of date * Disable lint warning as build fails without the assert * Report aggregate statistics for solution as well as some solution perf numbers * Options solutionDiagnostics instead so that its not too verbose when printing diagnostics * When tsc --build --clean, only remove tsbuildinfo if its incremental build * Revert "Options solutionDiagnostics instead so that its not too verbose when printing diagnostics" This reverts commit 0cf9e30. * Revert "Report aggregate statistics for solution as well as some solution perf numbers" This reverts commit 405d8e9. * Revert "When tsc --build --clean, only remove tsbuildinfo if its incremental build" This reverts commit e4e6672. * Comments in the code * Feedback
1 parent 7bff2dd commit b8f6488

File tree

320 files changed

+17589
-14811
lines changed

Some content is hidden

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

320 files changed

+17589
-14811
lines changed

src/compiler/builder.ts

+308-242
Large diffs are not rendered by default.

src/compiler/builderPublic.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ namespace ts {
2525
*/
2626
/*@internal*/
2727
storeFilesChangingSignatureDuringEmit?: boolean;
28+
/**
29+
* Gets the current time
30+
*/
31+
/*@internal*/
32+
now?(): Date;
2833
}
2934

3035
/**
@@ -34,9 +39,9 @@ namespace ts {
3439
/*@internal*/
3540
getState(): ReusableBuilderProgramState;
3641
/*@internal*/
37-
backupState(): void;
42+
saveEmitState(): SavedBuildProgramEmitState;
3843
/*@internal*/
39-
restoreState(): void;
44+
restoreEmitState(saved: SavedBuildProgramEmitState): void;
4045
/**
4146
* Returns current program
4247
*/

src/compiler/builderState.ts

+64-122
Large diffs are not rendered by default.

src/compiler/builderStatePublic.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ namespace ts {
1010
name: string;
1111
writeByteOrderMark: boolean;
1212
text: string;
13+
/* @internal */ buildInfo?: BuildInfo
1314
}
1415
}

src/compiler/commandLineParser.ts

+116-26
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -5140,11 +5140,11 @@
51405140
"category": "Error",
51415141
"code": 6310
51425142
},
5143-
"Project '{0}' is out of date because oldest output '{1}' is older than newest input '{2}'": {
5143+
"Project '{0}' is out of date because output '{1}' is older than input '{2}'": {
51445144
"category": "Message",
51455145
"code": 6350
51465146
},
5147-
"Project '{0}' is up to date because newest input '{1}' is older than oldest output '{2}'": {
5147+
"Project '{0}' is up to date because newest input '{1}' is older than output '{2}'": {
51485148
"category": "Message",
51495149
"code": 6351
51505150
},
@@ -5322,6 +5322,14 @@
53225322
"category": "Message",
53235323
"code": 6398
53245324
},
5325+
"Project '{0}' is out of date because buildinfo file '{1}' indicates that some of the changes were not emitted": {
5326+
"category": "Message",
5327+
"code": 6399
5328+
},
5329+
"Project '{0}' is up to date but needs to update timestamps of output files that are older than input files": {
5330+
"category": "Message",
5331+
"code": 6400
5332+
},
53255333

53265334
"The expected type comes from property '{0}' which is declared here on type '{1}'": {
53275335
"category": "Message",

src/compiler/emitter.ts

+46-9
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ namespace ts {
366366
return;
367367
}
368368
const version = ts.version; // Extracted into a const so the form is stable between namespace and module
369-
writeFile(host, emitterDiagnostics, buildInfoPath, getBuildInfoText({ bundle, program, version }), /*writeByteOrderMark*/ false);
369+
const buildInfo: BuildInfo = { bundle, program, version };
370+
// Pass buildinfo as additional data to avoid having to reparse
371+
writeFile(host, emitterDiagnostics, buildInfoPath, getBuildInfoText(buildInfo), /*writeByteOrderMark*/ false, /*sourceFiles*/ undefined, { buildInfo });
370372
}
371373

372374
function emitJsFileOrBundle(
@@ -557,14 +559,19 @@ namespace ts {
557559
if (sourceMapFilePath) {
558560
const sourceMap = sourceMapGenerator.toString();
559561
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap, /*writeByteOrderMark*/ false, sourceFiles);
562+
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = BuilderState.computeSignature(sourceMap, maybeBind(host, host.createHash));
560563
}
561564
}
562565
else {
563566
writer.writeLine();
564567
}
565568

566569
// Write the output file
567-
writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos });
570+
const text = writer.getText();
571+
writeFile(host, emitterDiagnostics, jsFilePath, text, !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos });
572+
// We store the hash of the text written in the buildinfo to ensure that text of the referenced d.ts file is same as whats in the buildinfo
573+
// This is needed because incremental can be toggled between two runs and we might use stale file text to do text manipulation in prepend mode
574+
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = BuilderState.computeSignature(text, maybeBind(host, host.createHash));
568575

569576
// Reset state
570577
writer.clear();
@@ -709,6 +716,9 @@ namespace ts {
709716
getCanonicalFileName(fileName: string): string;
710717
useCaseSensitiveFileNames(): boolean;
711718
getNewLine(): string;
719+
createHash?(data: string): string;
720+
now?(): Date;
721+
getBuildInfo?(fileName: string, configFilePath: string | undefined): BuildInfo | undefined;
712722
}
713723

714724
function createSourceFilesFromBundleBuildInfo(bundle: BundleBuildInfo, buildInfoDirectory: string, host: EmitUsingBuildInfoHost): readonly SourceFile[] {
@@ -745,23 +755,40 @@ namespace ts {
745755
getCommandLine: (ref: ProjectReference) => ParsedCommandLine | undefined,
746756
customTransformers?: CustomTransformers
747757
): EmitUsingBuildInfoResult {
758+
const createHash = maybeBind(host, host.createHash);
748759
const { buildInfoPath, jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } = getOutputPathsForBundle(config.options, /*forceDtsPaths*/ false);
749-
const buildInfoText = host.readFile(Debug.checkDefined(buildInfoPath));
750-
if (!buildInfoText) return buildInfoPath!;
760+
let buildInfo: BuildInfo;
761+
if (host.getBuildInfo) {
762+
// If host directly provides buildinfo we can get it directly. This allows host to cache the buildinfo
763+
const hostBuildInfo = host.getBuildInfo(buildInfoPath!, config.options.configFilePath);
764+
if (!hostBuildInfo) return buildInfoPath!;
765+
buildInfo = hostBuildInfo;
766+
}
767+
else {
768+
const buildInfoText = host.readFile(buildInfoPath!);
769+
if (!buildInfoText) return buildInfoPath!;
770+
buildInfo = getBuildInfo(buildInfoText);
771+
}
772+
if (!buildInfo.bundle || !buildInfo.bundle.js || (declarationFilePath && !buildInfo.bundle.dts)) return buildInfoPath!;
773+
751774
const jsFileText = host.readFile(Debug.checkDefined(jsFilePath));
752775
if (!jsFileText) return jsFilePath!;
776+
// If the jsFileText is not same has what it was created with, tsbuildinfo is stale so dont use it
777+
if (BuilderState.computeSignature(jsFileText, createHash) !== buildInfo.bundle.js.hash) return jsFilePath!;
753778
const sourceMapText = sourceMapFilePath && host.readFile(sourceMapFilePath);
754779
// error if no source map or for now if inline sourcemap
755780
if ((sourceMapFilePath && !sourceMapText) || config.options.inlineSourceMap) return sourceMapFilePath || "inline sourcemap decoding";
781+
if (sourceMapFilePath && BuilderState.computeSignature(sourceMapText!, createHash) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;
782+
756783
// read declaration text
757784
const declarationText = declarationFilePath && host.readFile(declarationFilePath);
758785
if (declarationFilePath && !declarationText) return declarationFilePath;
786+
if (declarationFilePath && BuilderState.computeSignature(declarationText!, createHash) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
759787
const declarationMapText = declarationMapPath && host.readFile(declarationMapPath);
760788
// error if no source map or for now if inline sourcemap
761789
if ((declarationMapPath && !declarationMapText) || config.options.inlineSourceMap) return declarationMapPath || "inline sourcemap decoding";
790+
if (declarationMapPath && BuilderState.computeSignature(declarationMapText!, createHash) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;
762791

763-
const buildInfo = getBuildInfo(buildInfoText);
764-
if (!buildInfo.bundle || !buildInfo.bundle.js || (declarationText && !buildInfo.bundle.dts)) return buildInfoPath!;
765792
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath!, host.getCurrentDirectory()));
766793
const ownPrependInput = createInputFiles(
767794
jsFileText,
@@ -779,6 +806,8 @@ namespace ts {
779806
const outputFiles: OutputFile[] = [];
780807
const prependNodes = createPrependNodes(config.projectReferences, getCommandLine, f => host.readFile(f));
781808
const sourceFilesForJsEmit = createSourceFilesFromBundleBuildInfo(buildInfo.bundle, buildInfoDirectory, host);
809+
let changedDtsText: string | undefined;
810+
let changedDtsData: WriteFileCallbackData | undefined;
782811
const emitHost: EmitHost = {
783812
getPrependNodes: memoize(() => [...prependNodes, ownPrependInput]),
784813
getCanonicalFileName: host.getCanonicalFileName,
@@ -794,7 +823,7 @@ namespace ts {
794823
getResolvedProjectReferenceToRedirect: returnUndefined,
795824
getProjectReferenceRedirect: returnUndefined,
796825
isSourceOfProjectReferenceRedirect: returnFalse,
797-
writeFile: (name, text, writeByteOrderMark) => {
826+
writeFile: (name, text, writeByteOrderMark, _onError, _sourceFiles, data) => {
798827
switch (name) {
799828
case jsFilePath:
800829
if (jsFileText === text) return;
@@ -803,19 +832,26 @@ namespace ts {
803832
if (sourceMapText === text) return;
804833
break;
805834
case buildInfoPath:
806-
const newBuildInfo = getBuildInfo(text);
835+
const newBuildInfo = data!.buildInfo!;
807836
newBuildInfo.program = buildInfo.program;
837+
if (newBuildInfo.program && changedDtsText !== undefined && config.options.composite) {
838+
// Update the output signature
839+
(newBuildInfo.program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, changedDtsData, createHash);
840+
newBuildInfo.program.dtsChangeTime = getCurrentTime(host).getTime();
841+
}
808842
// Update sourceFileInfo
809843
const { js, dts, sourceFiles } = buildInfo.bundle!;
810844
newBuildInfo.bundle!.js!.sources = js!.sources;
811845
if (dts) {
812846
newBuildInfo.bundle!.dts!.sources = dts.sources;
813847
}
814848
newBuildInfo.bundle!.sourceFiles = sourceFiles;
815-
outputFiles.push({ name, text: getBuildInfoText(newBuildInfo), writeByteOrderMark });
849+
outputFiles.push({ name, text: getBuildInfoText(newBuildInfo), writeByteOrderMark, buildInfo: newBuildInfo });
816850
return;
817851
case declarationFilePath:
818852
if (declarationText === text) return;
853+
changedDtsText = text;
854+
changedDtsData = data;
819855
break;
820856
case declarationMapPath:
821857
if (declarationMapText === text) return;
@@ -833,6 +869,7 @@ namespace ts {
833869
getSourceFileFromReference: returnUndefined,
834870
redirectTargetsMap: createMultiMap(),
835871
getFileIncludeReasons: notImplemented,
872+
createHash,
836873
};
837874
emitFiles(
838875
notImplementedResolver,

src/compiler/program.ts

+2-45
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,14 @@ namespace ts {
5858
return getPathFromPathComponents(commonPathComponents);
5959
}
6060

61-
interface OutputFingerprint {
62-
hash: string;
63-
byteOrderMark: boolean;
64-
mtime: Date;
65-
}
66-
6761
export function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost {
6862
return createCompilerHostWorker(options, setParentNodes);
6963
}
7064

7165
/*@internal*/
72-
// TODO(shkamat): update this after reworking ts build API
7366
export function createCompilerHostWorker(options: CompilerOptions, setParentNodes?: boolean, system = sys): CompilerHost {
7467
const existingDirectories = new Map<string, boolean>();
7568
const getCanonicalFileName = createGetCanonicalFileName(system.useCaseSensitiveFileNames);
76-
const computeHash = maybeBind(system, system.createHash) || generateDjb2Hash;
7769
function getSourceFile(fileName: string, languageVersionOrOptions: ScriptTarget | CreateSourceFileOptions, onError?: (message: string) => void): SourceFile | undefined {
7870
let text: string | undefined;
7971
try {
@@ -113,7 +105,7 @@ namespace ts {
113105
fileName,
114106
data,
115107
writeByteOrderMark,
116-
(path, data, writeByteOrderMark) => writeFileWorker(path, data, writeByteOrderMark),
108+
(path, data, writeByteOrderMark) => system.writeFile(path, data, writeByteOrderMark),
117109
path => (compilerHost.createDirectory || system.createDirectory)(path),
118110
path => directoryExists(path));
119111

@@ -127,42 +119,6 @@ namespace ts {
127119
}
128120
}
129121

130-
let outputFingerprints: ESMap<string, OutputFingerprint>;
131-
function writeFileWorker(fileName: string, data: string, writeByteOrderMark: boolean) {
132-
if (!isWatchSet(options) || !system.getModifiedTime) {
133-
system.writeFile(fileName, data, writeByteOrderMark);
134-
return;
135-
}
136-
137-
if (!outputFingerprints) {
138-
outputFingerprints = new Map<string, OutputFingerprint>();
139-
}
140-
141-
const hash = computeHash(data);
142-
const mtimeBefore = system.getModifiedTime(fileName);
143-
144-
if (mtimeBefore) {
145-
const fingerprint = outputFingerprints.get(fileName);
146-
// If output has not been changed, and the file has no external modification
147-
if (fingerprint &&
148-
fingerprint.byteOrderMark === writeByteOrderMark &&
149-
fingerprint.hash === hash &&
150-
fingerprint.mtime.getTime() === mtimeBefore.getTime()) {
151-
return;
152-
}
153-
}
154-
155-
system.writeFile(fileName, data, writeByteOrderMark);
156-
157-
const mtimeAfter = system.getModifiedTime(fileName) || missingFileModifiedTime;
158-
159-
outputFingerprints.set(fileName, {
160-
hash,
161-
byteOrderMark: writeByteOrderMark,
162-
mtime: mtimeAfter
163-
});
164-
}
165-
166122
function getDefaultLibLocation(): string {
167123
return getDirectoryPath(normalizePath(system.getExecutingFilePath()));
168124
}
@@ -1973,6 +1929,7 @@ namespace ts {
19731929
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
19741930
redirectTargetsMap,
19751931
getFileIncludeReasons: program.getFileIncludeReasons,
1932+
createHash: maybeBind(host, host.createHash),
19761933
};
19771934
}
19781935

0 commit comments

Comments
 (0)