Skip to content

Commit bbc906d

Browse files
committed
Don't use needsUpdate for quick tasks
needsUpdate may be wrong when the branch changes; these ones are now so fast thanks to being pure JS that we can just always run their contents and be sure that the outputs are right.
1 parent aec2761 commit bbc906d

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

Herebyfile.mjs

+29-31
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { task } from "hereby";
66
import _glob from "glob";
77
import util from "util";
88
import chalk from "chalk";
9-
import { exec, readJson, needsUpdate, getDiffTool, getDirSize, memoize } from "./scripts/build/utils.mjs";
9+
import { exec, readJson, getDiffTool, getDirSize, memoize, needsUpdate } from "./scripts/build/utils.mjs";
1010
import { runConsoleTests, refBaseline, localBaseline, refRwcBaseline, localRwcBaseline } from "./scripts/build/tests.mjs";
1111
import { buildProject as realBuildProject, cleanProject, watchProject } from "./scripts/build/projects.mjs";
1212
import { localizationDirectories } from "./scripts/build/localization.mjs";
@@ -81,22 +81,18 @@ export const generateLibs = task({
8181
description: "Builds the library targets",
8282
run: async () => {
8383
await fs.promises.mkdir("./built/local", { recursive: true });
84-
const allSources = libs().flatMap((lib) => lib.sources);
85-
const allTargets = libs().flatMap((lib) => lib.target);
86-
if (needsUpdate([copyrightFilename, ...allSources], allTargets)) {
87-
for (const lib of libs()) {
88-
let output = await copyright();
89-
90-
for (const source of lib.sources) {
91-
const contents = await fs.promises.readFile(source, "utf-8");
92-
// TODO(jakebailey): "\n\n" is for compatibility with our current tests; our test baselines
93-
// are sensitive to the positions of things in the lib files. Eventually remove this,
94-
// or remove lib.d.ts line numbers from our baselines.
95-
output += "\n\n" + contents.replace(/\r\n/g, "\n");
96-
}
97-
98-
await fs.promises.writeFile(lib.target, output);
84+
for (const lib of libs()) {
85+
let output = await copyright();
86+
87+
for (const source of lib.sources) {
88+
const contents = await fs.promises.readFile(source, "utf-8");
89+
// TODO(jakebailey): "\n\n" is for compatibility with our current tests; our test baselines
90+
// are sensitive to the positions of things in the lib files. Eventually remove this,
91+
// or remove lib.d.ts line numbers from our baselines.
92+
output += "\n\n" + contents.replace(/\r\n/g, "\n");
9993
}
94+
95+
await fs.promises.writeFile(lib.target, output);
10096
}
10197
},
10298
});
@@ -110,9 +106,7 @@ export const generateDiagnostics = task({
110106
name: "generate-diagnostics",
111107
description: "Generates a diagnostic file in TypeScript based on an input JSON file",
112108
run: async () => {
113-
if (needsUpdate(diagnosticMessagesJson, [diagnosticMessagesGeneratedJson, diagnosticInformationMapTs])) {
114-
await exec(process.execPath, ["scripts/processDiagnosticMessages.mjs", diagnosticMessagesJson]);
115-
}
109+
await exec(process.execPath, ["scripts/processDiagnosticMessages.mjs", diagnosticMessagesJson]);
116110
}
117111
});
118112

@@ -410,7 +404,11 @@ export const dtsServices = task({
410404
name: "dts-services",
411405
description: "Bundles typescript.d.ts",
412406
dependencies: [buildServices],
413-
run: () => runDtsBundler("./built/local/typescript/typescript.d.ts", "./built/local/typescript.d.ts"),
407+
run: async () => {
408+
if (needsUpdate("./built/local/typescript/tsconfig.tsbuildinfo", ["./built/local/typescript.d.ts", "./built/local/typescript.internal.d.ts"])) {
409+
runDtsBundler("./built/local/typescript/typescript.d.ts", "./built/local/typescript.d.ts");
410+
}
411+
},
414412
});
415413

416414

@@ -464,7 +462,11 @@ export const dtsLssl = task({
464462
name: "dts-lssl",
465463
description: "Bundles tsserverlibrary.d.ts",
466464
dependencies: [buildLssl],
467-
run: () => runDtsBundler("./built/local/tsserverlibrary/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.d.ts")
465+
run: async () => {
466+
if (needsUpdate("./built/local/tsserverlibrary/tsconfig.tsbuildinfo", ["./built/local/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.internal.d.ts"])) {
467+
await runDtsBundler("./built/local/tsserverlibrary/tsserverlibrary.d.ts", "./built/local/tsserverlibrary.d.ts");
468+
}
469+
}
468470
});
469471

470472
export const dts = task({
@@ -560,11 +562,9 @@ export const generateTypesMap = task({
560562
run: async () => {
561563
const source = "src/server/typesMap.json";
562564
const target = "built/local/typesMap.json";
563-
if (needsUpdate(source, target)) {
564-
const contents = await fs.promises.readFile(source, "utf-8");
565-
JSON.parse(contents);
566-
await fs.promises.writeFile(target, contents);
567-
}
565+
const contents = await fs.promises.readFile(source, "utf-8");
566+
JSON.parse(contents); // Validates that the JSON parses.
567+
await fs.promises.writeFile(target, contents);
568568
}
569569
});
570570

@@ -577,11 +577,9 @@ const copyBuiltLocalDiagnosticMessages = task({
577577
name: "copy-built-local-diagnostic-messages",
578578
dependencies: [generateDiagnostics],
579579
run: async () => {
580-
if (needsUpdate(diagnosticMessagesGeneratedJson, builtLocalDiagnosticMessagesGeneratedJson)) {
581-
const contents = await fs.promises.readFile(diagnosticMessagesGeneratedJson, "utf-8");
582-
JSON.parse(contents);
583-
await fs.promises.writeFile(builtLocalDiagnosticMessagesGeneratedJson, contents);
584-
}
580+
const contents = await fs.promises.readFile(diagnosticMessagesGeneratedJson, "utf-8");
581+
JSON.parse(contents); // Validates that the JSON parses.
582+
await fs.promises.writeFile(builtLocalDiagnosticMessagesGeneratedJson, contents);
585583
}
586584
});
587585

scripts/processDiagnosticMessages.mjs

+17-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void 0;
1313

1414
/** @typedef {Map<string, DiagnosticDetails>} InputDiagnosticMessageTable */
1515

16-
function main() {
16+
async function main() {
1717
if (process.argv.length < 3) {
1818
console.log("Usage:");
1919
console.log("\tnode processDiagnosticMessages.mjs <diagnostic-json-input-file>");
@@ -24,15 +24,24 @@ function main() {
2424
* @param {string} fileName
2525
* @param {string} contents
2626
*/
27-
function writeFile(fileName, contents) {
28-
fs.writeFile(path.join(path.dirname(inputFilePath), fileName), contents, { encoding: "utf-8" }, err => {
29-
if (err) throw err;
30-
});
27+
async function writeFile(fileName, contents) {
28+
const filePath = path.join(path.dirname(inputFilePath), fileName);
29+
try {
30+
const existingContents = await fs.promises.readFile(filePath, "utf-8");
31+
if (existingContents === contents) {
32+
return;
33+
}
34+
}
35+
catch {
36+
// Just write the file.
37+
}
38+
39+
await fs.promises.writeFile(filePath, contents, { encoding: "utf-8" });
3140
}
3241

3342
const inputFilePath = process.argv[2].replace(/\\/g, "/");
3443
console.log(`Reading diagnostics from ${inputFilePath}`);
35-
const inputStr = fs.readFileSync(inputFilePath, { encoding: "utf-8" });
44+
const inputStr = await fs.promises.readFile(inputFilePath, { encoding: "utf-8" });
3645

3746
/** @type {{ [key: string]: DiagnosticDetails }} */
3847
const diagnosticMessagesJson = JSON.parse(inputStr);
@@ -47,10 +56,10 @@ function main() {
4756

4857
const infoFileOutput = buildInfoFileOutput(diagnosticMessages, inputFilePath);
4958
checkForUniqueCodes(diagnosticMessages);
50-
writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
59+
await writeFile("diagnosticInformationMap.generated.ts", infoFileOutput);
5160

5261
const messageOutput = buildDiagnosticMessageOutput(diagnosticMessages);
53-
writeFile("diagnosticMessages.generated.json", messageOutput);
62+
await writeFile("diagnosticMessages.generated.json", messageOutput);
5463
}
5564

5665
/**

0 commit comments

Comments
 (0)