Cotton code
69 lines · 3.0 KB · RustRaw
//! The crate's own directory, read at RUN time.//!//! The `env!` macro of that name is a COMPILE-TIME constant: whatever directory//! the compiler was standing in is baked into the binary and travels with it.//! On the CI box that is a bug with a name. The `rust` job shares one//! `CARGO_TARGET_DIR` across every run (`~/.cache/cottondb/rust/target`) while//! Forgejo hands each run a FRESH working directory//! (`/srv/ci/forgejo/ci/work/<hash>/hostexecutor`, a different hash every time).//! A test binary compiled by an earlier run is fingerprint-fresh for this one//! when the sources match, so cargo serves it — and the path inside it names a//! checkout that has been deleted.//!//! Run 1879 (2026-09-08) is the measured case: the job compiled//! `cotton-relay-protocol` in `4b97088174786c6f` and then ran a test binary//! whose baked path was `96b9a6a640379c0e`, so both `names_tests` byte-equality//! guards failed claiming `packages/relay/src/protocol/names.ts` was missing.//! The same two tests had passed 29 minutes earlier on the same code.//!//! Cargo also sets `CARGO_MANIFEST_DIR` as a RUNTIME environment variable on//! every `cargo test` / `cargo run` process, pointing at the manifest of the//! crate whose target is running. That value is always the directory the job is//! actually in, whatever binary cargo decided to serve. So every path a test//! resolves in this workspace starts here.//!//! There is deliberately NO fallback to the compile-time value: a fallback//! would restore exactly the failure this module exists to remove, and it would//! do it silently.use std::path::PathBuf;/// The directory holding the `Cargo.toml` of the crate being run.////// Panics — naming the variable — when cargo did not set it, because a binary/// started some other way has no honest answer to give and a guess here is the/// bug above coming back.#[doc(hidden)]pub fn manifest_dir() -> PathBuf {    match std::env::var("CARGO_MANIFEST_DIR") {        Ok(dir) if !dir.is_empty() => PathBuf::from(dir),        _ => panic!(            "CARGO_MANIFEST_DIR is not set in this process. Cargo sets it for every `cargo test` \             and `cargo run`, so this binary was started some other way — run it through cargo, \             or set the variable to the crate's directory. There is no fallback to the \             compile-time value on purpose: that constant is what a cached test binary from an \             earlier CI checkout carries, and it names a directory that is gone."        ),    }}#[cfg(test)]mod tests {    use super::manifest_dir;    /// Under `cargo test` the variable is set, and it names THIS crate.    #[test]    fn it_is_this_crate_s_directory_and_the_manifest_is_in_it() {        let dir = manifest_dir();        assert!(            dir.join("Cargo.toml").is_file(),            "{} holds no Cargo.toml",            dir.display()        );        assert!(            dir.ends_with("cotton-store"),            "{} is not this crate's directory",            dir.display()        );    }}