Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion scripts/ci-pretest-cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import { createHash } from "node:crypto";
import { execFileSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, utimesSync, writeFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";

Expand Down Expand Up @@ -128,6 +128,28 @@ export function markerIsFresh(distDir, hash) {
}
}

/**
* A verified-fresh dist must also be MTIME-fresh (#4548): the actions/cache
* restore brings dist files back with their archived mtimes (older than this
* run's checkout), so every mtime-based freshness consumer — the #3325
* golden-drive guard, tsup/esbuild incrementals, anything else — sees a stale
* dist even though the marker just proved its bytes fresh. Refresh the whole
* dist tree to "now" on every HIT; on MISS the real build already does the
* equivalent implicitly.
*/
function touchTree(dir) {
const now = new Date();
const walk = (d) => {
for (const e of readdirSync(d, { withFileTypes: true })) {
const p = join(d, e.name);
if (e.isDirectory()) walk(p);
else if (e.isFile()) utimesSync(p, now, now);
}
utimesSync(d, now, now);
};
walk(dir);
}

/**
* The pretest loop. For each component: hash → fresh marker? HIT, skip :
* MISS → run the real build → stamp the marker. `build`/`log` are test seams
Expand All @@ -144,6 +166,7 @@ export function runPretestCache(opts = {}) {
const hash = hashComponent(root, comp);
const distDir = join(root, comp.dir, "dist");
if (markerIsFresh(distDir, hash)) {
touchTree(distDir);
log(`pretest-cache: HIT (${comp.name}) — dist fresh for hash ${hash.slice(0, 12)}`);
results.push({ name: comp.name, hit: true, ms: 0 });
continue;
Expand Down
40 changes: 39 additions & 1 deletion tests/ci-pretest-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { mkdtempSync, mkdirSync, readFileSync, statSync, utimesSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import * as cache from "../scripts/ci-pretest-cache.mjs";
Expand Down Expand Up @@ -163,6 +163,44 @@ test("a missing input is a hard error, never a silent HIT", () => {
assert.throws(() => cache.hashComponent(root, broken), /does-not-exist\.ts/);
});

test("a HIT must also be mtime-fresh — a cache-restored dist with archived old timestamps gets touched to now (#4548)", () => {
// Reproduces the master-red incident: actions/cache restores dist bytes
// with their archived mtimes (older than this checkout), the #3325
// golden-drive guard compares mtimes, and a verified-fresh-but-old dist
// is wrongly refused. The HIT path must refresh the whole dist tree.
const { root, write } = makeMiniRepo();
const seen: string[] = [];
cache.runPretestCache({ root, build: fakeBuild(seen) });

// Simulate the actions/cache restore: dist bytes valid, mtimes archived
// in the past — and prove the precondition actually looks stale first.
write("kernel/src/index.ts", 'export const v = "kernel";\n'); // same bytes
const old = new Date(Date.now() - 3_600_000);
utimesSync(join(root, "kernel", "dist", "index.js"), old, old);
utimesSync(join(root, "kernel", "dist"), old, old);
utimesSync(join(root, "kernel", "dist", cache.MARKER_NAME), old, old);
// Model the checkout clock: sources checked out ~1s after the cache was
// archived — strictly newer than the restored dist, older than the touch.
const srcAt = new Date(Date.now() - 1_000);
utimesSync(join(root, "kernel", "src", "index.ts"), srcAt, srcAt);
assert.ok(
statSync(join(root, "kernel", "dist", "index.js")).mtimeMs < srcAt.getTime(),
"precondition: restored dist must look mtime-stale before the run",
);

const r = cache.runPretestCache({ root, build: fakeBuild(seen) });
assert.equal(r.find((x) => x.name === "kernel")?.hit, true);
assert.equal(seen.length, 2, "HIT must not rebuild");
assert.ok(
statSync(join(root, "kernel", "dist", "index.js")).mtimeMs > srcAt.getTime(),
"HIT must touch the dist tree past the sources (#3325 guard invariant)",
);
assert.ok(
statSync(join(root, "kernel", "dist")).mtimeMs > srcAt.getTime(),
"the dist directory itself is touched too",
);
});

test("build failure propagates (CLI exit path) and stamps no marker", () => {
const { root } = makeMiniRepo();
const boom = (): void => {
Expand Down
Loading