import { isolateScriptFixtures } from"./helpers/fixture-sandbox";isolateScriptFixtures();import { spawnSync } from"node:child_process";import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from"node:fs";import { tmpdir } from"node:os";import { join, resolve } from"node:path";import { afterAll, beforeEach, describe, expect, it } from"vitest";import { publishDiskReading } from"./helpers/disk-reading";/** * A BUILD NEVER WAITS ON A SILENT SCCACHE SERVER. * * Cargo runs every rustc through the device policy's wrapper * (scripts/mac-rustc-policy.sh → scripts/lib/mac_disk_policy.py), with the * sccache the worktree found in COTTON_RUSTC_CACHE_WRAPPER. The fake sccache * here logs every call; "silent" makes its server calls sleep, as one Mac's did on * 14 Oct. The fake rustc says it ran and whether sccache sat in front of it. * COTTON_DISK_MONITOR_PID names this test process, so the policy takes the * nested path (an outer build owns the admission) and the case is about the * probe alone. */const WRAPPER = resolve(__dirname, "../mac-rustc-policy.sh");const made: string[] = [];let home: string;let sccache: string;let rustc: string;let log: string;let silent: string;function stub(path: string, body: string): void { writeFileSync(path, body); chmodSync(path, 0o755);}function compile(...args: string[]): { status: number | null; out: string; err: string } {const r = spawnSync("bash", [WRAPPER, rustc, ...args], { encoding: "utf8", timeout: 20_000, env: { ...process.env, HOME: home, COTTON_RUSTC_CACHE_WRAPPER: sccache, COTTON_SCCACHE_PROBE_SECONDS: "2", COTTON_DISK_MONITOR_PID: String(process.pid), }, });return { status: r.status, out: r.stdout, err: r.stderr };}const calls = (): string[] => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n") : []);beforeEach(() => {const root = mkdtempSync(join(tmpdir(), "sccache-probe-")); made.push(root); home = join(root, "home"); mkdirSync(home); publishDiskReading(home);const bin = join(root, "bin"); mkdirSync(bin); log = join(root, "calls.log"); silent = join(root, "silent"); sccache = join(bin, "sccache"); rustc = join(bin, "rustc"); stub( sccache,`#!/usr/bin/env bash\n` +`echo "$*" >> ${JSON.stringify(log)}\n` +`case "$1" in --show-stats|--start-server) [ -e ${JSON.stringify(silent)} ] && exec sleep 30; exit 0;; esac\n` +`SCCACHE_WRAPPED=1 exec "$@"\n`, ); stub(rustc, `#!/usr/bin/env bash\necho "rustc ran wrapped=\${SCCACHE_WRAPPED:-0} $*"\n`);});afterAll(() => {for (const dir of made) rmSync(dir, { recursive: true, force: true });});describe("the rustc wrapper probes the sccache server before a build", () => {// RULE: Every cargo run asks the sccache server to answer, within a bound, before its first rustc goes through it, so a server that stopped answering never holds a build at 0% CPU. it("asks the server once per cargo run, then compiles through it", () => {const first = compile("--crate-name", "a"); expect(first.status, first.err).toBe(0); expect(first.out).toContain("rustc ran wrapped=1 --crate-name a");const second = compile("--crate-name", "b"); expect(second.out).toContain("rustc ran wrapped=1 --crate-name b"); expect(calls()[0]).toBe("--show-stats"); expect(calls().filter((c) => c === "--show-stats")).toHaveLength(1); });// RULE: A server silent after one start is left out of this cargo run — every rustc runs without the cache wrapper — and the build says so once on stderr, never waits. it("starts a silent server once, then compiles without the wrapper and says so", () => { writeFileSync(silent, "");const started = Date.now();const first = compile("--crate-name", "a"); expect(first.status, first.err).toBe(0); expect(first.out).toContain("rustc ran wrapped=0 --crate-name a"); expect(first.err).toContain("compiles without the cache"); expect(calls()).toEqual(["--show-stats", "--start-server", "--show-stats"]);const second = compile("--crate-name", "b"); expect(second.out).toContain("rustc ran wrapped=0 --crate-name b"); expect(second.err).not.toContain("compiles without the cache"); expect(calls()).toHaveLength(3); expect(Date.now() - started).toBeLessThan(15_000); });});