Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build improvements #48784

Merged
merged 74 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
a3b5207
Use fixed time for vfs so baselining is consistent
sheetalkamat Apr 13, 2022
c20613f
Baseline buildinfos
sheetalkamat Apr 13, 2022
c79af2c
Write new file text in baseline even if the file wasnt read on the sh…
sheetalkamat Apr 14, 2022
40a8c7f
Remove unnecessary debugger statement
sheetalkamat Apr 15, 2022
7377470
Make sure that incremental correctness is checked with correct writeF…
sheetalkamat Apr 15, 2022
c206ff0
More baselines for the tsbuildinfo
sheetalkamat Apr 19, 2022
fc91aff
If we are writing dts file and have used file text as version, we can…
sheetalkamat Apr 13, 2022
396e6c8
Make WriteFileCallback Api ready for future
sheetalkamat Apr 14, 2022
7084221
Assert that there is only single source file when emitting d.ts file
sheetalkamat Apr 15, 2022
5f02cf3
Add test
sheetalkamat Apr 5, 2022
c984298
Renames
sheetalkamat Apr 6, 2022
e4bf58a
More refactoring
sheetalkamat Apr 6, 2022
87a7112
If we are updating dts of any of the file and it affects global scope…
sheetalkamat Apr 6, 2022
16cef4a
Stacktrace optimization for getModified time in anticipation of using…
sheetalkamat Mar 16, 2022
7e65cd3
Baseline getModifiedTime, setModifiedTime, fileExits and directoryExi…
sheetalkamat Apr 5, 2022
0ff8cb3
Remove unnecessary write file finger print code since its not used at…
sheetalkamat Mar 16, 2022
59ad6ef
Use modified time instead of file existence check
sheetalkamat Apr 11, 2022
c8327da
Remove unnecessary getModifiedTime
sheetalkamat Mar 17, 2022
7817fbf
No need to check for file existence before reading the d.ts file
sheetalkamat Mar 17, 2022
cb7aca3
Do project reference errors before doing input/output file checks
sheetalkamat Mar 21, 2022
dd96e33
Dont call getModifiedTimes if dts change
sheetalkamat Mar 28, 2022
89d2d4c
Passdown modified time if queried
sheetalkamat Mar 15, 2022
6e0c916
Use modified time passed through the file watching in tsbuild
sheetalkamat Mar 18, 2022
e6a3ee8
Handle force build as separate upto date status
sheetalkamat Mar 29, 2022
7cb0f40
uptodate status worker to read buildinfo and use it to determine upto…
sheetalkamat Apr 7, 2022
1a8abac
No need to update output timestamps if buildinfo will determine uptod…
sheetalkamat Apr 11, 2022
59f2b5c
Store change file set instead of hasPendingChange to be able to reuse…
sheetalkamat Apr 5, 2022
5c12067
Add test that shows input file is not present
sheetalkamat Apr 7, 2022
7734528
No need to check input time stamp before buildinfo
sheetalkamat Apr 7, 2022
6198fa3
Keep buildinfos for lifetime of the solution builder and project
sheetalkamat Apr 8, 2022
fcf07f8
Store modified time along with text of buildinfo
sheetalkamat Apr 8, 2022
2f2e370
Non composite projects dont need to track declaration change time
sheetalkamat Apr 12, 2022
15fe24e
Pass through buildInfo so we dont have to parse it back
sheetalkamat Apr 14, 2022
dc21283
Save dts change time in buildinfo itself
sheetalkamat Apr 12, 2022
5bccee8
Store dts time for --out in the buildInfo
sheetalkamat Apr 15, 2022
62c687b
Store hash of text in the bundle info so it can be verified before ma…
sheetalkamat Apr 19, 2022
128008a
Since buildinfo is cached no need to maintain version check state
sheetalkamat Apr 19, 2022
4fb6773
Store output time stamps for non incremental builds
sheetalkamat Apr 19, 2022
7e1e9d7
Revert "Baseline getModifiedTime, setModifiedTime, fileExits and dire…
sheetalkamat Apr 5, 2022
cb73874
Change verbose messages for upto date status
sheetalkamat Apr 19, 2022
d8d8609
Merge branch 'main' into timestamps
sheetalkamat Apr 20, 2022
0f7903d
Reconcile reusable builder state and builder state so there are not t…
sheetalkamat Apr 20, 2022
82fb56b
Cleanup impliedFormat
sheetalkamat Apr 20, 2022
7785a87
Cleanup
sheetalkamat Apr 20, 2022
a95d3cc
Cleanup noEmit option
sheetalkamat Apr 20, 2022
437619e
BuildInfo options emit as a flag
sheetalkamat Apr 20, 2022
b568d7e
Factor out types for program written in buildinfo with and without bu…
sheetalkamat Apr 20, 2022
78ba778
No need to store output file stamps if not in watch mode
sheetalkamat Apr 20, 2022
10728b4
Cleanup
sheetalkamat Apr 20, 2022
5492949
Test for single watch per file
sheetalkamat Apr 20, 2022
70069d2
Fix emit and error update baselines that were duplicate
sheetalkamat Apr 21, 2022
21a6806
More refactoring
sheetalkamat Apr 21, 2022
b32d2eb
Only copy emit state fields when backing up to restore if emit fails
sheetalkamat Apr 21, 2022
26e7cb6
Merge branch 'main' into timestamps
sheetalkamat Apr 22, 2022
303824e
Instead of maintaining delta of changes, maintain old state for those…
sheetalkamat Apr 26, 2022
643576c
Merge branch 'main' into timestamps
sheetalkamat Apr 27, 2022
7810c56
Add test to verify build when input file does not change
sheetalkamat Apr 29, 2022
28a9ff3
If version of the input file does not change, dont mark as out of date
sheetalkamat Apr 29, 2022
929ddad
Merge branch 'main' into timestamps
sheetalkamat Apr 29, 2022
5d31847
Disable lint warning as build fails without the assert
sheetalkamat Apr 29, 2022
fa1068e
Merge branch 'main' into timestamps
sheetalkamat May 6, 2022
94e7e43
Merge branch 'main' into timestamps
sheetalkamat May 24, 2022
7593eee
Merge branch 'main' into timestamps
sheetalkamat May 27, 2022
405d8e9
Report aggregate statistics for solution as well as some solution per…
sheetalkamat May 27, 2022
fe69264
Merge branch 'main' into timestamps
sheetalkamat Jun 1, 2022
0cf9e30
Options solutionDiagnostics instead so that its not too verbose when …
sheetalkamat Jun 1, 2022
e4e6672
When tsc --build --clean, only remove tsbuildinfo if its incremental …
sheetalkamat Jun 2, 2022
842de49
Merge branch 'main' into timestamps
sheetalkamat Jun 6, 2022
5c07c77
Revert "Options solutionDiagnostics instead so that its not too verbo…
sheetalkamat Jun 7, 2022
31427f6
Revert "Report aggregate statistics for solution as well as some solu…
sheetalkamat Jun 7, 2022
0f898f2
Merge branch 'main' into timestamps
sheetalkamat Jun 7, 2022
5cd883a
Revert "When tsc --build --clean, only remove tsbuildinfo if its incr…
sheetalkamat Jun 7, 2022
5cd5d57
Comments in the code
sheetalkamat Jun 7, 2022
624c182
Feedback
sheetalkamat Jun 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
550 changes: 308 additions & 242 deletions src/compiler/builder.ts

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/compiler/builderPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ namespace ts {
*/
/*@internal*/
storeFilesChangingSignatureDuringEmit?: boolean;
/**
* Gets the current time
*/
/*@internal*/
now?(): Date;
}

/**
Expand All @@ -34,9 +39,9 @@ namespace ts {
/*@internal*/
getState(): ReusableBuilderProgramState;
/*@internal*/
backupState(): void;
saveEmitState(): SavedBuildProgramEmitState;
/*@internal*/
restoreState(): void;
restoreEmitState(saved: SavedBuildProgramEmitState): void;
/**
* Returns current program
*/
Expand Down
186 changes: 64 additions & 122 deletions src/compiler/builderState.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/compiler/builderStatePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ namespace ts {
name: string;
writeByteOrderMark: boolean;
text: string;
/* @internal */ buildInfo?: BuildInfo
}
}
142 changes: 116 additions & 26 deletions src/compiler/commandLineParser.ts

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5140,11 +5140,11 @@
"category": "Error",
"code": 6310
},
"Project '{0}' is out of date because oldest output '{1}' is older than newest input '{2}'": {
"Project '{0}' is out of date because output '{1}' is older than input '{2}'": {
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
"category": "Message",
"code": 6350
},
"Project '{0}' is up to date because newest input '{1}' is older than oldest output '{2}'": {
"Project '{0}' is up to date because newest input '{1}' is older than output '{2}'": {
"category": "Message",
"code": 6351
},
Expand Down Expand Up @@ -5322,6 +5322,14 @@
"category": "Message",
"code": 6398
},
"Project '{0}' is out of date because buildinfo file '{1}' indicates that some of the changes were not emitted": {
"category": "Message",
"code": 6399
},
"Project '{0}' is up to date but needs to update timestamps of output files that are older than input files": {
"category": "Message",
"code": 6400
},

"The expected type comes from property '{0}' which is declared here on type '{1}'": {
"category": "Message",
Expand Down
55 changes: 46 additions & 9 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ namespace ts {
return;
}
const version = ts.version; // Extracted into a const so the form is stable between namespace and module
writeFile(host, emitterDiagnostics, buildInfoPath, getBuildInfoText({ bundle, program, version }), /*writeByteOrderMark*/ false);
const buildInfo: BuildInfo = { bundle, program, version };
// Pass buildinfo as additional data to avoid having to reparse
writeFile(host, emitterDiagnostics, buildInfoPath, getBuildInfoText(buildInfo), /*writeByteOrderMark*/ false, /*sourceFiles*/ undefined, { buildInfo });
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
}

function emitJsFileOrBundle(
Expand Down Expand Up @@ -557,14 +559,19 @@ namespace ts {
if (sourceMapFilePath) {
const sourceMap = sourceMapGenerator.toString();
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap, /*writeByteOrderMark*/ false, sourceFiles);
if (printer.bundleFileInfo) printer.bundleFileInfo.mapHash = BuilderState.computeSignature(sourceMap, maybeBind(host, host.createHash));
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
}
}
else {
writer.writeLine();
}

// Write the output file
writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos });
const text = writer.getText();
writeFile(host, emitterDiagnostics, jsFilePath, text, !!compilerOptions.emitBOM, sourceFiles, { sourceMapUrlPos });
// 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
// 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
if (printer.bundleFileInfo) printer.bundleFileInfo.hash = BuilderState.computeSignature(text, maybeBind(host, host.createHash));

// Reset state
writer.clear();
Expand Down Expand Up @@ -709,6 +716,9 @@ namespace ts {
getCanonicalFileName(fileName: string): string;
useCaseSensitiveFileNames(): boolean;
getNewLine(): string;
createHash?(data: string): string;
now?(): Date;
getBuildInfo?(fileName: string, configFilePath: string | undefined): BuildInfo | undefined;
}

function createSourceFilesFromBundleBuildInfo(bundle: BundleBuildInfo, buildInfoDirectory: string, host: EmitUsingBuildInfoHost): readonly SourceFile[] {
Expand Down Expand Up @@ -745,23 +755,40 @@ namespace ts {
getCommandLine: (ref: ProjectReference) => ParsedCommandLine | undefined,
customTransformers?: CustomTransformers
): EmitUsingBuildInfoResult {
const createHash = maybeBind(host, host.createHash);
const { buildInfoPath, jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath } = getOutputPathsForBundle(config.options, /*forceDtsPaths*/ false);
const buildInfoText = host.readFile(Debug.checkDefined(buildInfoPath));
if (!buildInfoText) return buildInfoPath!;
let buildInfo: BuildInfo;
if (host.getBuildInfo) {
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
// If host directly provides buildinfo we can get it directly. This allows host to cache the buildinfo
const hostBuildInfo = host.getBuildInfo(buildInfoPath!, config.options.configFilePath);
if (!hostBuildInfo) return buildInfoPath!;
buildInfo = hostBuildInfo;
}
else {
const buildInfoText = host.readFile(buildInfoPath!);
if (!buildInfoText) return buildInfoPath!;
buildInfo = getBuildInfo(buildInfoText);
}
if (!buildInfo.bundle || !buildInfo.bundle.js || (declarationFilePath && !buildInfo.bundle.dts)) return buildInfoPath!;

const jsFileText = host.readFile(Debug.checkDefined(jsFilePath));
if (!jsFileText) return jsFilePath!;
// If the jsFileText is not same has what it was created with, tsbuildinfo is stale so dont use it
if (BuilderState.computeSignature(jsFileText, createHash) !== buildInfo.bundle.js.hash) return jsFilePath!;
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
const sourceMapText = sourceMapFilePath && host.readFile(sourceMapFilePath);
// error if no source map or for now if inline sourcemap
if ((sourceMapFilePath && !sourceMapText) || config.options.inlineSourceMap) return sourceMapFilePath || "inline sourcemap decoding";
if (sourceMapFilePath && BuilderState.computeSignature(sourceMapText!, createHash) !== buildInfo.bundle.js.mapHash) return sourceMapFilePath;

// read declaration text
const declarationText = declarationFilePath && host.readFile(declarationFilePath);
if (declarationFilePath && !declarationText) return declarationFilePath;
if (declarationFilePath && BuilderState.computeSignature(declarationText!, createHash) !== buildInfo.bundle.dts!.hash) return declarationFilePath;
const declarationMapText = declarationMapPath && host.readFile(declarationMapPath);
// error if no source map or for now if inline sourcemap
if ((declarationMapPath && !declarationMapText) || config.options.inlineSourceMap) return declarationMapPath || "inline sourcemap decoding";
if (declarationMapPath && BuilderState.computeSignature(declarationMapText!, createHash) !== buildInfo.bundle.dts!.mapHash) return declarationMapPath;

const buildInfo = getBuildInfo(buildInfoText);
if (!buildInfo.bundle || !buildInfo.bundle.js || (declarationText && !buildInfo.bundle.dts)) return buildInfoPath!;
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath!, host.getCurrentDirectory()));
const ownPrependInput = createInputFiles(
jsFileText,
Expand All @@ -779,6 +806,8 @@ namespace ts {
const outputFiles: OutputFile[] = [];
const prependNodes = createPrependNodes(config.projectReferences, getCommandLine, f => host.readFile(f));
const sourceFilesForJsEmit = createSourceFilesFromBundleBuildInfo(buildInfo.bundle, buildInfoDirectory, host);
let changedDtsText: string | undefined;
let changedDtsData: WriteFileCallbackData | undefined;
const emitHost: EmitHost = {
getPrependNodes: memoize(() => [...prependNodes, ownPrependInput]),
getCanonicalFileName: host.getCanonicalFileName,
Expand All @@ -794,7 +823,7 @@ namespace ts {
getResolvedProjectReferenceToRedirect: returnUndefined,
getProjectReferenceRedirect: returnUndefined,
isSourceOfProjectReferenceRedirect: returnFalse,
writeFile: (name, text, writeByteOrderMark) => {
writeFile: (name, text, writeByteOrderMark, _onError, _sourceFiles, data) => {
switch (name) {
case jsFilePath:
if (jsFileText === text) return;
Expand All @@ -803,19 +832,26 @@ namespace ts {
if (sourceMapText === text) return;
break;
case buildInfoPath:
const newBuildInfo = getBuildInfo(text);
const newBuildInfo = data!.buildInfo!;
newBuildInfo.program = buildInfo.program;
if (newBuildInfo.program && changedDtsText !== undefined && config.options.composite) {
// Update the output signature
(newBuildInfo.program as ProgramBundleEmitBuildInfo).outSignature = computeSignature(changedDtsText, changedDtsData, createHash);
newBuildInfo.program.dtsChangeTime = getCurrentTime(host).getTime();
}
// Update sourceFileInfo
const { js, dts, sourceFiles } = buildInfo.bundle!;
newBuildInfo.bundle!.js!.sources = js!.sources;
if (dts) {
newBuildInfo.bundle!.dts!.sources = dts.sources;
}
newBuildInfo.bundle!.sourceFiles = sourceFiles;
outputFiles.push({ name, text: getBuildInfoText(newBuildInfo), writeByteOrderMark });
outputFiles.push({ name, text: getBuildInfoText(newBuildInfo), writeByteOrderMark, buildInfo: newBuildInfo });
return;
case declarationFilePath:
if (declarationText === text) return;
changedDtsText = text;
changedDtsData = data;
break;
case declarationMapPath:
if (declarationMapText === text) return;
Expand All @@ -833,6 +869,7 @@ namespace ts {
getSourceFileFromReference: returnUndefined,
redirectTargetsMap: createMultiMap(),
getFileIncludeReasons: notImplemented,
createHash,
};
emitFiles(
notImplementedResolver,
Expand Down
47 changes: 2 additions & 45 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,14 @@ namespace ts {
return getPathFromPathComponents(commonPathComponents);
}

interface OutputFingerprint {
hash: string;
byteOrderMark: boolean;
mtime: Date;
}

export function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost {
return createCompilerHostWorker(options, setParentNodes);
}

/*@internal*/
// TODO(shkamat): update this after reworking ts build API
export function createCompilerHostWorker(options: CompilerOptions, setParentNodes?: boolean, system = sys): CompilerHost {
const existingDirectories = new Map<string, boolean>();
const getCanonicalFileName = createGetCanonicalFileName(system.useCaseSensitiveFileNames);
const computeHash = maybeBind(system, system.createHash) || generateDjb2Hash;
function getSourceFile(fileName: string, languageVersionOrOptions: ScriptTarget | CreateSourceFileOptions, onError?: (message: string) => void): SourceFile | undefined {
let text: string | undefined;
try {
Expand Down Expand Up @@ -113,7 +105,7 @@ namespace ts {
fileName,
data,
writeByteOrderMark,
(path, data, writeByteOrderMark) => writeFileWorker(path, data, writeByteOrderMark),
(path, data, writeByteOrderMark) => system.writeFile(path, data, writeByteOrderMark),
path => (compilerHost.createDirectory || system.createDirectory)(path),
path => directoryExists(path));

Expand All @@ -127,42 +119,6 @@ namespace ts {
}
}

let outputFingerprints: ESMap<string, OutputFingerprint>;
sheetalkamat marked this conversation as resolved.
Show resolved Hide resolved
function writeFileWorker(fileName: string, data: string, writeByteOrderMark: boolean) {
if (!isWatchSet(options) || !system.getModifiedTime) {
system.writeFile(fileName, data, writeByteOrderMark);
return;
}

if (!outputFingerprints) {
outputFingerprints = new Map<string, OutputFingerprint>();
}

const hash = computeHash(data);
const mtimeBefore = system.getModifiedTime(fileName);

if (mtimeBefore) {
const fingerprint = outputFingerprints.get(fileName);
// If output has not been changed, and the file has no external modification
if (fingerprint &&
fingerprint.byteOrderMark === writeByteOrderMark &&
fingerprint.hash === hash &&
fingerprint.mtime.getTime() === mtimeBefore.getTime()) {
return;
}
}

system.writeFile(fileName, data, writeByteOrderMark);

const mtimeAfter = system.getModifiedTime(fileName) || missingFileModifiedTime;

outputFingerprints.set(fileName, {
hash,
byteOrderMark: writeByteOrderMark,
mtime: mtimeAfter
});
}

function getDefaultLibLocation(): string {
return getDirectoryPath(normalizePath(system.getExecutingFilePath()));
}
Expand Down Expand Up @@ -1973,6 +1929,7 @@ namespace ts {
getSourceFileFromReference: (file, ref) => program.getSourceFileFromReference(file, ref),
redirectTargetsMap,
getFileIncludeReasons: program.getFileIncludeReasons,
createHash: maybeBind(host, host.createHash),
};
}

Expand Down
Loading