-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix(ci): generated files never read half-written, lint scaling test runner-proof, probe cache test off disk #3834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
217c8a5
fix(build): stop generated files from being read half-written during …
miguel-heygen 17728e4
fix(build): keep the shared write helper inside the core package
miguel-heygen b36b5fc
test(engine): keep the probe cache bound test off the filesystem
miguel-heygen 6e7c68a
test(lint): keep the scaling check inside the scanner's linear range
miguel-heygen c212e6f
test(core): snapshot file identity through a descriptor
miguel-heygen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { | ||
| closeSync, | ||
| fstatSync, | ||
| mkdtempSync, | ||
| openSync, | ||
| readdirSync, | ||
| readFileSync, | ||
| rmSync, | ||
| statSync, | ||
| utimesSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| // 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 | ||
| // CodeQL's file-system-race query looks for). The write under test is the point. | ||
| const snapshot = (path: string) => { | ||
| const fd = openSync(path, "r"); | ||
| try { | ||
| return fstatSync(fd); | ||
| } finally { | ||
| closeSync(fd); | ||
| } | ||
| }; | ||
| import { after, describe, it } from "node:test"; | ||
| import { writeGeneratedFile } from "./writeGeneratedFile.js"; | ||
|
|
||
| const workDir = mkdtempSync(join(tmpdir(), "write-generated-")); | ||
| after(() => rmSync(workDir, { recursive: true, force: true })); | ||
|
|
||
| const target = (name: string) => join(workDir, `${name}.ts`); | ||
|
|
||
| describe("writeGeneratedFile", () => { | ||
| it("publishes the whole file and leaves no temp behind", () => { | ||
| const out = target("published"); | ||
| const contents = `export const BIG = ${JSON.stringify("x".repeat(50_000))};\n`; | ||
|
|
||
| assert.equal(writeGeneratedFile(out, contents), true); | ||
|
|
||
| assert.equal(readFileSync(out, "utf8"), contents); | ||
| assert.deepEqual( | ||
| readdirSync(workDir).filter((f) => f.endsWith(".tmp")), | ||
| [], | ||
| ); | ||
| }); | ||
|
|
||
| it("leaves the target untouched when the content is byte-identical", () => { | ||
| const out = target("unchanged"); | ||
| const contents = "export const A = 1;\n"; | ||
| writeGeneratedFile(out, contents); | ||
| // Backdated so a republish cannot coincidentally land on the same mtime. Read the | ||
| // stamp back rather than trusting the one just set: filesystems round it. | ||
| const backdated = new Date(Date.now() - 60_000); | ||
| utimesSync(out, backdated, backdated); | ||
| const before = snapshot(out).mtimeMs; | ||
|
|
||
| assert.equal(writeGeneratedFile(out, contents), false); | ||
|
|
||
| assert.equal(statSync(out).mtimeMs, before); | ||
| }); | ||
|
|
||
| it("replaces the target rather than rewriting it in place", () => { | ||
| const out = target("replaced"); | ||
| writeFileSync(out, "export const A = 1;\n", "utf8"); | ||
| const before = snapshot(out).ino; | ||
|
|
||
| assert.equal(writeGeneratedFile(out, "export const A = 2;\n"), true); | ||
|
|
||
| assert.equal(readFileSync(out, "utf8"), "export const A = 2;\n"); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| // A new inode is the observable proof the target was swapped in whole. Rewriting | ||
| // in place keeps the inode and exposes a truncated file to a concurrent reader. | ||
| assert.notEqual(statSync(out).ino, before); | ||
| }); | ||
|
|
||
| it("creates the directory when it does not exist yet", () => { | ||
| const out = join(workDir, "nested", "deeper", "created.ts"); | ||
|
|
||
| assert.equal(writeGeneratedFile(out, "export const A = 1;\n"), true); | ||
|
|
||
| assert.equal(readFileSync(out, "utf8"), "export const A = 1;\n"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /** | ||
| * Publish a file under `src/generated` so a concurrent reader never observes it | ||
| * half-written. | ||
| * | ||
| * The root build runs several package builds at once, and more than one of them | ||
| * regenerates files here while a sibling's `tsc` is reading them. A plain | ||
| * `writeFileSync` leaves the target truncated and then growing for the duration of | ||
| * the write, which surfaces in whichever build read it mid-flight as | ||
| * `TS1002: Unterminated string literal` — a failure that never reproduces locally, | ||
| * because locally nothing is reading at that instant. | ||
| * | ||
| * Writing to a temp file in the same directory and renaming over the target makes | ||
| * publication one atomic step: a reader sees either the whole previous file or the | ||
| * whole new one, never a prefix. Same directory matters — `rename` is only atomic | ||
| * within a filesystem. | ||
| * | ||
| * Byte-identical content is not republished, so a no-op rebuild leaves the mtime | ||
| * alone and nothing downstream that keys off mtime is needlessly invalidated. | ||
| * | ||
| * Returns whether the target was replaced. | ||
| * | ||
| * Lives inside `packages/core` rather than the repo-root `scripts/` because every | ||
| * container image that builds a package copies `packages/…` wholesale but | ||
| * cherry-picks root scripts one file at a time. A helper the core build imports | ||
| * has to travel with the package, or the image build fails on a missing module. | ||
| */ | ||
|
|
||
| import { execFileSync } from "node:child_process"; | ||
| import { randomBytes } from "node:crypto"; | ||
| import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { dirname } from "node:path"; | ||
|
|
||
| export function writeGeneratedFile(outPath: string, contents: string): boolean { | ||
| if (existsSync(outPath) && readFileSync(outPath, "utf8") === contents) return false; | ||
|
|
||
| mkdirSync(dirname(outPath), { recursive: true }); | ||
| // Not a `.ts` suffix: the temp file sits inside the package's `src` glob, and a | ||
| // concurrent `tsc` would otherwise try to compile it. | ||
| const tempPath = `${outPath}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`; | ||
| try { | ||
| writeFileSync(tempPath, contents, "utf8"); | ||
| renameSync(tempPath, outPath); | ||
| } finally { | ||
| rmSync(tempPath, { force: true }); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
| export function formatGeneratedSource(source: string, outPath: string): string { | ||
| try { | ||
| return execFileSync("bun", ["x", "oxfmt", `--stdin-filepath=${outPath}`], { | ||
| input: source, | ||
| encoding: "utf8", | ||
| }); | ||
| } catch { | ||
| return source; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.