/** * A RUST TEST RESOLVES ITS PATHS AT RUN TIME, NEVER FROM A COMPILE-TIME CONSTANT. * * `env!("CARGO_MANIFEST_DIR")` is baked into the binary by the compiler. On the * CI box that turns a cache hit into a failure that reads like a code bug: the * `rust` job shares one `CARGO_TARGET_DIR` (`~/.cache/cottondb/rust/target`) * across runs, while Forgejo hands every run a FRESH working directory * (`/srv/ci/forgejo/ci/work//hostexecutor`, a different hash each time). * A test binary compiled by an earlier run is fingerprint-fresh for this one * when the sources match, so cargo serves it — and every path baked into it * names a checkout that has been deleted. * * Run 1879 (2026-09-08, on PR #399's own merge commit) is the measured case: * the job compiled a crate in `4b97088174786c6f` and then ran a lib-test * binary whose baked path was `96b9a6a640379c0e`, so two guards failed * claiming a file they read was missing. The same two tests had passed 29 * minutes earlier on the same code. The cargo-cache retry wrapper read it * correctly and declined: a real failure, not a missing binary. * * The fix is the RUNTIME variable of the same name, which cargo sets on every * `cargo test` / `cargo run` process and which therefore always names the * directory the job is actually in. One helper holds it, and this guard says * no second spelling comes back. * * ABOUT THIS FILE'S OWN TEXT: the sweep reads `.rs` files only, so writing the * banned macro out here is not an offence against it. */ import { execFileSync } from "node:child_process"; import { readFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); const CRATES = "crates/"; /** The helper: `cotton-store` is a dependency of every crate that reads a path. */ const HELPERS = [`${CRATES}cotton-store/src/manifest.rs`]; /** * The compile-time reader, in every spelling rustfmt can produce. * * `env !` with a space and a line break before the string are both legal, so * the macro name, its parenthesis and the variable are matched with the * whitespace open rather than as one literal. */ const COMPILE_TIME = /\benv\s*!\s*\(\s*"CARGO_MANIFEST_DIR"/; /** And the `Option` form, which fails the same way — quietly, with a `None`. */ const COMPILE_TIME_OPTION = /\boption_env\s*!\s*\(\s*"CARGO_MANIFEST_DIR"/; /** Every tracked or untracked `.rs` file under the Rust workspace. */ function rustSources(): string[] { return execFileSync("git", ["ls-files", "--cached", "--others", "--exclude-standard"], { cwd: REPO_ROOT, encoding: "utf8", maxBuffer: 32 * 1024 * 1024, }) .split("\n") .filter((path) => path.startsWith(CRATES) && path.endsWith(".rs")); } const read = (path: string): string => readFileSync(join(REPO_ROOT, path), "utf8"); describe("a Rust test reads the manifest directory at run time", () => { it("sweeps the whole Rust workspace, not an empty list", () => { // The loud-null rule: a sweep that reaches nothing passes for the wrong // reason. This number is the floor, not the count. expect(rustSources().length).toBeGreaterThan(100); }); it("no source bakes the compile-time CARGO_MANIFEST_DIR into a binary", () => { const offenders = rustSources().filter((path) => { const text = read(path); return COMPILE_TIME.test(text) || COMPILE_TIME_OPTION.test(text); }); expect(offenders).toEqual([]); }); for (const helper of HELPERS) { describe(helper, () => { it("reads the runtime variable", () => { const text = read(helper); expect(text).not.toBe(""); expect(text).toContain('std::env::var("CARGO_MANIFEST_DIR")'); expect(text).toContain("pub fn manifest_dir() -> PathBuf"); }); it("fails loudly, naming the variable, rather than falling back", () => { const text = read(helper); // A panic, and the variable's name inside its sentence — the message a // reader gets when a binary is started outside cargo. expect(text).toMatch(/panic!\(\s*\n?\s*"CARGO_MANIFEST_DIR is not set/); // And no second answer: the compile-time constant must not appear even // as a fallback arm, which is the shape that would silently restore // run 1879. expect(COMPILE_TIME.test(text)).toBe(false); expect(COMPILE_TIME_OPTION.test(text)).toBe(false); }); }); } it("the helper is reached by every crate that resolves a path", () => { // Not a count — a count goes stale the day a guard is added or deleted, // and this floor has now gone stale once: removing the multi-person // sharing data plane took 66 server test files with it and the readers fell // from 21 to 18. What must hold is that the readers reach a helper AT ALL, // so no crate quietly grows a private copy of the compile-time constant // back — and the assertion that actually catches that is the sweep above. // The floor stays low on purpose. const readers = rustSources().filter((path) => { const text = read(path); return ( text.includes("cotton_store::manifest_dir()") || text.includes("manifest::manifest_dir()") ); }); expect(readers.length).toBeGreaterThan(10); }); });