Skip to content

Write HTML. Render video. Built for agents.

fix(core): format generated artifacts without spawning bun from PATH (#5859)

Miguel Ángeld369159305c9
diff --git a/packages/core/scripts/writeGeneratedFile.test.ts b/packages/core/scripts/writeGeneratedFile.test.ts
index 8acc53d248..3dda014215 100644
--- a/packages/core/scripts/writeGeneratedFile.test.ts
+++ b/packages/core/scripts/writeGeneratedFile.test.ts
@@ -1,4 +1,6 @@
import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
+import { createRequire } from "node:module";
import {
closeSync,
fstatSync,
@@ -12,7 +14,8 @@ import {
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { dirname, join, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
// Identity is read through a descriptor, not a path, so the snapshot cannot be mistaken
// for a check that the following write is then trusted to still hold (the pattern
@@ -26,7 +29,7 @@ const snapshot = (path: string) => {
}
};
import { after, describe, it } from "node:test";
-import { writeGeneratedFile } from "./writeGeneratedFile.js";
+import { formatGeneratedSource, writeGeneratedFile } from "./writeGeneratedFile.js";
const workDir = mkdtempSync(join(tmpdir(), "write-generated-"));
after(() => rmSync(workDir, { recursive: true, force: true }));
@@ -83,3 +86,40 @@ describe("writeGeneratedFile", () => {
assert.equal(readFileSync(out, "utf8"), "export const A = 1;\n");
});
});
+
+describe("formatGeneratedSource", () => {
+ it("formats with the installed formatter when bun cannot be spawned from PATH", () => {
+ const require = createRequire(import.meta.url);
+ const moduleUrl = new URL("./writeGeneratedFile.ts", import.meta.url).href;
+ const source = 'export const value={quote:"a\\\"b"};';
+ const expected = formatGeneratedSource(source, target("formatted"));
+ assert.notEqual(expected, source);
+ const actual = execFileSync(
+ process.execPath,
+ [
+ "--import",
+ require.resolve("tsx"),
+ "--input-type=module",
+ "--eval",
+ `import { formatGeneratedSource } from ${JSON.stringify(moduleUrl)}; process.stdout.write(formatGeneratedSource(${JSON.stringify(source)}, ${JSON.stringify(target("formatted"))}));`,
+ ],
+ {
+ encoding: "utf8",
+ env: { ...process.env, PATH: "" },
+ cwd: resolve(dirname(fileURLToPath(import.meta.url)), "../../../.."),
+ },
+ );
+ assert.equal(actual, expected);
+ });
+
+ it("fails visibly before an invalid artifact can replace an existing file", () => {
+ const out = target("invalid");
+ const before = "export const original = true;\n";
+ writeFileSync(out, before);
+ assert.throws(
+ () => writeGeneratedFile(out, formatGeneratedSource("export const = ;", out)),
+ /format.*generated|generated.*format/i,
+ );
+ assert.equal(readFileSync(out, "utf8"), before);
+ });
+});
diff --git a/packages/core/scripts/writeGeneratedFile.ts b/packages/core/scripts/writeGeneratedFile.ts
index b9a49511fb..58abcd2975 100644
--- a/packages/core/scripts/writeGeneratedFile.ts
+++ b/packages/core/scripts/writeGeneratedFile.ts
@@ -26,6 +26,7 @@
*/
import { execFileSync } from "node:child_process";
+import { createRequire } from "node:module";
import { randomBytes } from "node:crypto";
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
import { dirname } from "node:path";
@@ -49,16 +50,21 @@ export function writeGeneratedFile(outPath: string, contents: string): boolean {
/**
* Runs the generated source through oxfmt's stdin so it is published already
* formatted. Formatting the file after publishing it would rewrite it in place and
- * reopen the very window the atomic rename closes. Best effort: an environment
- * without oxfmt still gets a valid, if unformatted, artifact.
+ * reopen the very window the atomic rename closes. Use the installed formatter with
+ * this runtime directly: Windows may only expose bun through shell-only shims.
+ * Fail before publication rather than dirtying tracked files with raw output.
*/
export function formatGeneratedSource(source: string, outPath: string): string {
try {
- return execFileSync("bun", ["x", "oxfmt", `--stdin-filepath=${outPath}`], {
+ const formatter = createRequire(import.meta.url).resolve("oxfmt/bin/oxfmt");
+ return execFileSync(process.execPath, [formatter, `--stdin-filepath=${outPath}`], {
input: source,
encoding: "utf8",
});
- } catch {
- return source;
+ } catch (cause) {
+ throw new Error(
+ `Could not format generated source for ${outPath}; install the checkout dependencies and retry.`,
+ { cause },
+ );
}
}