A copy of a workspace that shares CARGO_TARGET_DIR replaces the build output of the original
Symptom#
You build a second copy of the same workspace in another folder, such as git checkout-index -a --prefix=/tmp/copy/ to test the staged index. Both copies use the same CARGO_TARGET_DIR. After that, cargo clippy in the original workspace fails with errors such as unresolved import rmaestro_term::CursorShape and no field shape on type Cursor, although the source in the original has those items.
Cause#
The two copies have workspace crates with the same names and versions. Cargo gives them the same artifact names in the shared target folder. The copy with the older source writes its library over the library of the original. Cargo then does not see that the original must build again, so the original links against the old library.
Fix#
Touch the source files of the affected crate in the original workspace, then build again:
To prevent it, give the copy its own target folder: CARGO_TARGET_DIR=/tmp/copy-target cargo test. Or delete the copy before you build the original.
Evidence#
The working tree had the type, and git diff HEAD was empty for the crate, but clippy reported 5 errors for the missing type. After touch on the 3 source files of the crate, the same clippy command finished with no errors.
Also found by searching
- unresolved import after building a copy of the workspace
- a second copy of a workspace with the same CARGO_TARGET_DIR breaks the first build
- how do I test a staged git index with cargo without breaking my build
- cargo uses an old version of my own crate