Skip to content

Commit fa97054

Browse files
authored
Merge pull request #30438 from Microsoft/tsconfigChangeDetection
Check oldest output time with tsconfig time to see if its out of date
2 parents cfb0ade + e94b7d8 commit fa97054

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/compiler/tsbuild.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,15 @@ namespace ts {
792792
newerInputFileName: newestInputFileName
793793
};
794794
}
795+
else {
796+
// Check tsconfig time
797+
const configStatus = checkConfigFileUpToDateStatus(project.options.configFilePath!, oldestOutputFileTime, oldestOutputFileName);
798+
if (configStatus) return configStatus;
799+
800+
// Check extended config time
801+
const extendedConfigStatus = forEach(project.options.configFile!.extendedSourceFiles || emptyArray, configFile => checkConfigFileUpToDateStatus(configFile, oldestOutputFileTime, oldestOutputFileName));
802+
if (extendedConfigStatus) return extendedConfigStatus;
803+
}
795804

796805
if (!buildInfoChecked.hasKey(project.options.configFilePath as ResolvedConfigFileName)) {
797806
buildInfoChecked.setValue(project.options.configFilePath as ResolvedConfigFileName, true);
@@ -828,6 +837,18 @@ namespace ts {
828837
};
829838
}
830839

840+
function checkConfigFileUpToDateStatus(configFile: string, oldestOutputFileTime: Date, oldestOutputFileName: string): Status.OutOfDateWithSelf | undefined {
841+
// Check tsconfig time
842+
const tsconfigTime = host.getModifiedTime(configFile) || missingFileModifiedTime;
843+
if (oldestOutputFileTime < tsconfigTime) {
844+
return {
845+
type: UpToDateStatusType.OutOfDateWithSelf,
846+
outOfDateOutputFileName: oldestOutputFileName,
847+
newerInputFileName: configFile
848+
};
849+
}
850+
}
851+
831852
function invalidateProject(configFileName: string, reloadLevel?: ConfigFileProgramReloadLevel) {
832853
invalidateResolvedProject(resolveProjectName(configFileName), reloadLevel);
833854
}

src/testRunner/unittests/tsbuild/sample.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,51 @@ namespace ts {
268268
[Diagnostics.Building_project_0, "/src/tests/tsconfig.json"]
269269
);
270270
});
271+
272+
it("rebuilds when tsconfig changes", () => {
273+
const { fs, host, builder } = initializeWithBuild();
274+
replaceText(fs, "/src/tests/tsconfig.json", `"composite": true`, `"composite": true, "target": "es3"`);
275+
builder.buildAllProjects();
276+
host.assertDiagnosticMessages(
277+
getExpectedDiagnosticForProjectsInBuild("src/core/tsconfig.json", "src/logic/tsconfig.json", "src/tests/tsconfig.json"),
278+
[Diagnostics.Project_0_is_up_to_date_because_newest_input_1_is_older_than_oldest_output_2, "src/core/tsconfig.json", "src/core/anotherModule.ts", "src/core/anotherModule.js"],
279+
[Diagnostics.Project_0_is_up_to_date_because_newest_input_1_is_older_than_oldest_output_2, "src/logic/tsconfig.json", "src/logic/index.ts", "src/logic/index.js"],
280+
[Diagnostics.Project_0_is_out_of_date_because_oldest_output_1_is_older_than_newest_input_2, "src/tests/tsconfig.json", "src/tests/index.js", "src/tests/tsconfig.json"],
281+
[Diagnostics.Building_project_0, "/src/tests/tsconfig.json"],
282+
[Diagnostics.Updating_unchanged_output_timestamps_of_project_0, "/src/tests/tsconfig.json"]
283+
);
284+
});
285+
286+
it("rebuilds when extended config file changes", () => {
287+
const fs = projFs.shadow();
288+
fs.writeFileSync("/src/tests/tsconfig.base.json", JSON.stringify({ compilerOptions: { target: "es3" } }));
289+
replaceText(fs, "/src/tests/tsconfig.json", `"references": [`, `"extends": "./tsconfig.base.json", "references": [`);
290+
const host = new fakes.SolutionBuilderHost(fs);
291+
const builder = createSolutionBuilder(host, ["/src/tests"], { verbose: true });
292+
builder.buildAllProjects();
293+
host.assertDiagnosticMessages(
294+
getExpectedDiagnosticForProjectsInBuild("src/core/tsconfig.json", "src/logic/tsconfig.json", "src/tests/tsconfig.json"),
295+
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/core/tsconfig.json", "src/core/anotherModule.js"],
296+
[Diagnostics.Building_project_0, "/src/core/tsconfig.json"],
297+
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/logic/tsconfig.json", "src/logic/index.js"],
298+
[Diagnostics.Building_project_0, "/src/logic/tsconfig.json"],
299+
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/tests/tsconfig.json", "src/tests/index.js"],
300+
[Diagnostics.Building_project_0, "/src/tests/tsconfig.json"]
301+
);
302+
host.clearDiagnostics();
303+
tick();
304+
builder.resetBuildContext();
305+
fs.writeFileSync("/src/tests/tsconfig.base.json", JSON.stringify({ compilerOptions: {} }));
306+
builder.buildAllProjects();
307+
host.assertDiagnosticMessages(
308+
getExpectedDiagnosticForProjectsInBuild("src/core/tsconfig.json", "src/logic/tsconfig.json", "src/tests/tsconfig.json"),
309+
[Diagnostics.Project_0_is_up_to_date_because_newest_input_1_is_older_than_oldest_output_2, "src/core/tsconfig.json", "src/core/anotherModule.ts", "src/core/anotherModule.js"],
310+
[Diagnostics.Project_0_is_up_to_date_because_newest_input_1_is_older_than_oldest_output_2, "src/logic/tsconfig.json", "src/logic/index.ts", "src/logic/index.js"],
311+
[Diagnostics.Project_0_is_out_of_date_because_oldest_output_1_is_older_than_newest_input_2, "src/tests/tsconfig.json", "src/tests/index.js", "src/tests/tsconfig.base.json"],
312+
[Diagnostics.Building_project_0, "/src/tests/tsconfig.json"],
313+
[Diagnostics.Updating_unchanged_output_timestamps_of_project_0, "/src/tests/tsconfig.json"]
314+
);
315+
});
271316
});
272317

273318
describe("downstream-blocked compilations", () => {

0 commit comments

Comments
 (0)