Skip to content

Commit 7cc1ad4

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 01839d2 commit 7cc1ad4

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

Herebyfile.mjs

+29-29
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,20 +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-
}
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");
9793
}
94+
95+
await fs.promises.writeFile(lib.target, output);
9896
}
9997
},
10098
});
@@ -108,9 +106,7 @@ export const generateDiagnostics = task({
108106
name: "generate-diagnostics",
109107
description: "Generates a diagnostic file in TypeScript based on an input JSON file",
110108
run: async () => {
111-
if (needsUpdate(diagnosticMessagesJson, [diagnosticMessagesGeneratedJson, diagnosticInformationMapTs])) {
112-
await exec(process.execPath, ["scripts/processDiagnosticMessages.mjs", diagnosticMessagesJson]);
113-
}
109+
await exec(process.execPath, ["scripts/processDiagnosticMessages.mjs", diagnosticMessagesJson]);
114110
}
115111
});
116112

@@ -408,7 +404,11 @@ export const dtsServices = task({
408404
name: "dts-services",
409405
description: "Bundles typescript.d.ts",
410406
dependencies: [buildServices],
411-
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+
},
412412
});
413413

414414

@@ -462,7 +462,11 @@ export const dtsLssl = task({
462462
name: "dts-lssl",
463463
description: "Bundles tsserverlibrary.d.ts",
464464
dependencies: [buildLssl],
465-
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+
}
466470
});
467471

468472
export const dts = task({
@@ -558,11 +562,9 @@ export const generateTypesMap = task({
558562
run: async () => {
559563
const source = "src/server/typesMap.json";
560564
const target = "built/local/typesMap.json";
561-
if (needsUpdate(source, target)) {
562-
const contents = await fs.promises.readFile(source, "utf-8");
563-
JSON.parse(contents);
564-
await fs.promises.writeFile(target, contents);
565-
}
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);
566568
}
567569
});
568570

@@ -575,11 +577,9 @@ const copyBuiltLocalDiagnosticMessages = task({
575577
name: "copy-built-local-diagnostic-messages",
576578
dependencies: [generateDiagnostics],
577579
run: async () => {
578-
if (needsUpdate(diagnosticMessagesGeneratedJson, builtLocalDiagnosticMessagesGeneratedJson)) {
579-
const contents = await fs.promises.readFile(diagnosticMessagesGeneratedJson, "utf-8");
580-
JSON.parse(contents);
581-
await fs.promises.writeFile(builtLocalDiagnosticMessagesGeneratedJson, contents);
582-
}
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);
583583
}
584584
});
585585

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)