Expand description
Is a test binary running the code of the checkout that runs it?
Cargo names a workspace’s own crates the same in every clone of the
repository. Two clones that share one target directory (CARGO_TARGET_DIR)
therefore write the same artifacts, and cargo, which checks the source files
the existing artifact was built from, can call another clone’s build fresh:
cargo test in one clone then runs the other clone’s code and reports its
results as this clone’s. Seen on 2026-10-17, with four clones sharing a
target dir: passes and failures that belonged to other branches, and a
“cannot find type” for code that was plainly there.
Every test binary of the workspace (each crate’s unit tests and each file
under tests/) invokes [checkout_guard!]. It runs [guard] when the
binary starts, before the test harness, so a test-name filter can’t skip
it. The crate folder compiled into the binary (CARGO_MANIFEST_DIR at build
time) must be the one cargo runs it for (CARGO_MANIFEST_DIR at run time);
and each workspace library it links must have been compiled in the same
workspace, since cargo can rebuild the test binary yet link another
checkout’s library it thinks fresh. Each library exports the folder it was
compiled in (COMPILED_IN here, CHECKOUT_DIR in the others) for that.
Constants§
- COMPILED_
IN - The folder this library was compiled in, for [
checkout_guard!].
Functions§
- check
Errwith what to do when the test binary was compiled from another checkout than the one running it.compiledis the invoking crate’senv!("CARGO_MANIFEST_DIR").Okwhen the binary isn’t run by cargo (noCARGO_MANIFEST_DIRat run time), so it can’t tell.- check_
linked Errwhen a workspace librarylinkedinto the test binary was compiled in another workspace than the binary itself (compiled): their workspace roots, two folders up, must be the same folder.