Skip to content

Commit 04205ca

Browse files
authored
Do not calculate signatures if old state is not used (#43314)
* Extra tests in preparation for lazy signature making sure the original intent of test is maintained * Whenver we cant use state delay signature calculation and use source file version as signature * Incremental correctness checks * Retain old behavior in compile on save by disabling use of file version as signature in when state is not reused
1 parent dcc27eb commit 04205ca

File tree

227 files changed

+41564
-1378
lines changed

Some content is hidden

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

227 files changed

+41564
-1378
lines changed

src/compiler/builder.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ namespace ts {
169169
/**
170170
* Create the state so that we can iterate on changedFiles/affected files
171171
*/
172-
function createBuilderProgramState(newProgram: Program, getCanonicalFileName: GetCanonicalFileName, oldState?: Readonly<ReusableBuilderProgramState>): BuilderProgramState {
173-
const state = BuilderState.create(newProgram, getCanonicalFileName, oldState) as BuilderProgramState;
172+
function createBuilderProgramState(newProgram: Program, getCanonicalFileName: GetCanonicalFileName, oldState: Readonly<ReusableBuilderProgramState> | undefined, disableUseFileVersionAsSignature: boolean | undefined): BuilderProgramState {
173+
const state = BuilderState.create(newProgram, getCanonicalFileName, oldState, disableUseFileVersionAsSignature) as BuilderProgramState;
174174
state.program = newProgram;
175175
const compilerOptions = newProgram.getCompilerOptions();
176176
state.compilerOptions = compilerOptions;
@@ -947,7 +947,7 @@ namespace ts {
947947
* Computing hash to for signature verification
948948
*/
949949
const computeHash = maybeBind(host, host.createHash);
950-
let state = createBuilderProgramState(newProgram, getCanonicalFileName, oldState);
950+
let state = createBuilderProgramState(newProgram, getCanonicalFileName, oldState, host.disableUseFileVersionAsSignature);
951951
let backupState: BuilderProgramState | undefined;
952952
newProgram.getProgramBuildInfo = () => getProgramBuildInfo(state, getCanonicalFileName);
953953

src/compiler/builderPublic.ts

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ namespace ts {
1515
* this callback if present would be used to write files
1616
*/
1717
writeFile?: WriteFileCallback;
18+
/**
19+
* disable using source file version as signature for testing
20+
*/
21+
/*@internal*/
22+
disableUseFileVersionAsSignature?: boolean;
1823
}
1924

2025
/**

src/compiler/builderState.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ namespace ts {
4545
* Otherwise undefined
4646
*/
4747
readonly exportedModulesMap: ESMap<Path, BuilderState.ReferencedSet> | undefined;
48+
49+
/**
50+
* true if file version is used as signature
51+
* This helps in delaying the calculation of the d.ts hash as version for the file till reasonable time
52+
*/
53+
useFileVersionAsSignature: boolean;
4854
/**
4955
* Map of files that have already called update signature.
5056
* That means hence forth these files are assumed to have
@@ -202,7 +208,7 @@ namespace ts {
202208
/**
203209
* Creates the state of file references and signature for the new program from oldState if it is safe
204210
*/
205-
export function create(newProgram: Program, getCanonicalFileName: GetCanonicalFileName, oldState?: Readonly<ReusableBuilderState>): BuilderState {
211+
export function create(newProgram: Program, getCanonicalFileName: GetCanonicalFileName, oldState?: Readonly<ReusableBuilderState>, disableUseFileVersionAsSignature?: boolean): BuilderState {
206212
const fileInfos = new Map<Path, FileInfo>();
207213
const referencedMap = newProgram.getCompilerOptions().module !== ModuleKind.None ? new Map<Path, ReferencedSet>() : undefined;
208214
const exportedModulesMap = referencedMap ? new Map<Path, ReferencedSet>() : undefined;
@@ -236,7 +242,8 @@ namespace ts {
236242
fileInfos,
237243
referencedMap,
238244
exportedModulesMap,
239-
hasCalledUpdateShapeSignature
245+
hasCalledUpdateShapeSignature,
246+
useFileVersionAsSignature: !disableUseFileVersionAsSignature && !useOldState
240247
};
241248
}
242249

@@ -258,6 +265,7 @@ namespace ts {
258265
referencedMap: state.referencedMap && new Map(state.referencedMap),
259266
exportedModulesMap: state.exportedModulesMap && new Map(state.exportedModulesMap),
260267
hasCalledUpdateShapeSignature: new Set(state.hasCalledUpdateShapeSignature),
268+
useFileVersionAsSignature: state.useFileVersionAsSignature,
261269
};
262270
}
263271

@@ -317,7 +325,7 @@ namespace ts {
317325

318326
const prevSignature = info.signature;
319327
let latestSignature: string | undefined;
320-
if (!sourceFile.isDeclarationFile) {
328+
if (!sourceFile.isDeclarationFile && !state.useFileVersionAsSignature) {
321329
const emitOutput = getFileEmitOutput(
322330
programOfThisState,
323331
sourceFile,

src/compiler/sys.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ namespace ts {
12071207
/*@internal*/ bufferFrom?(input: string, encoding?: string): Buffer;
12081208
// For testing
12091209
/*@internal*/ now?(): Date;
1210+
/*@internal*/ disableUseFileVersionAsSignature?: boolean;
12101211
/*@internal*/ require?(baseDir: string, moduleName: string): RequireResult;
12111212
/*@internal*/ defaultWatchFileKind?(): WatchFileKind | undefined;
12121213
}

src/compiler/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6436,6 +6436,9 @@ namespace ts {
64366436
// TODO: later handle this in better way in builder host instead once the api for tsbuild finalizes and doesn't use compilerHost as base
64376437
/*@internal*/createDirectory?(directory: string): void;
64386438
/*@internal*/getSymlinkCache?(): SymlinkCache;
6439+
6440+
// For testing:
6441+
/*@internal*/ disableUseFileVersionAsSignature?: boolean;
64396442
}
64406443

64416444
/** true if --out otherwise source file name */

src/compiler/watch.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ namespace ts {
476476
getEnvironmentVariable: maybeBind(host, host.getEnvironmentVariable) || (() => ""),
477477
createHash: maybeBind(host, host.createHash),
478478
readDirectory: maybeBind(host, host.readDirectory),
479+
disableUseFileVersionAsSignature: host.disableUseFileVersionAsSignature,
479480
};
480481

481482
function writeFile(fileName: string, text: string, writeByteOrderMark: boolean, onError: (message: string) => void) {
@@ -538,7 +539,8 @@ namespace ts {
538539
createDirectory: path => system.createDirectory(path),
539540
writeFile: (path, data, writeByteOrderMark) => system.writeFile(path, data, writeByteOrderMark),
540541
createHash: maybeBind(system, system.createHash),
541-
createProgram: createProgram || createEmitAndSemanticDiagnosticsBuilderProgram as any as CreateProgram<T>
542+
createProgram: createProgram || createEmitAndSemanticDiagnosticsBuilderProgram as any as CreateProgram<T>,
543+
disableUseFileVersionAsSignature: system.disableUseFileVersionAsSignature,
542544
};
543545
}
544546

src/compiler/watchPublic.ts

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace ts {
1919
export function createIncrementalCompilerHost(options: CompilerOptions, system = sys): CompilerHost {
2020
const host = createCompilerHostWorker(options, /*setParentNodes*/ undefined, system);
2121
host.createHash = maybeBind(system, system.createHash);
22+
host.disableUseFileVersionAsSignature = system.disableUseFileVersionAsSignature;
2223
setGetSourceFileAsHashVersioned(host, system);
2324
changeCompilerHostLikeToUseCache(host, fileName => toPath(fileName, host.getCurrentDirectory(), host.getCanonicalFileName));
2425
return host;
@@ -111,6 +112,8 @@ namespace ts {
111112
// TODO: GH#18217 Optional methods are frequently asserted
112113
createDirectory?(path: string): void;
113114
writeFile?(path: string, data: string, writeByteOrderMark?: boolean): void;
115+
// For testing
116+
disableUseFileVersionAsSignature?: boolean;
114117
}
115118

116119
export interface WatchCompilerHost<T extends BuilderProgram> extends ProgramHost<T>, WatchHost {

src/harness/virtualFileSystemWithWatch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ interface Array<T> { length: number; [n: number]: T; }`
127127
return { close: () => map.remove(path, callback) };
128128
}
129129

130-
function getDiffInKeys<T>(map: ESMap<string, T>, expectedKeys: readonly string[]) {
130+
export function getDiffInKeys<T>(map: ESMap<string, T>, expectedKeys: readonly string[]) {
131131
if (map.size === expectedKeys.length) {
132132
return "";
133133
}

src/server/project.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ namespace ts.server {
640640
return [];
641641
}
642642
updateProjectIfDirty(this);
643-
this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState);
643+
this.builderState = BuilderState.create(this.program!, this.projectService.toCanonicalFileName, this.builderState, /*disableUseFileVersionAsSignature*/ true);
644644
return mapDefined(
645645
BuilderState.getFilesAffectedBy(
646646
this.builderState,

0 commit comments

Comments
 (0)