Rust E0786 "found invalid metadata files" (stale target) in CI
rustc tried to read a crate's metadata file and found it invalid. The target/ directory carries .rmeta/.rlib artifacts from a different toolchain or a half-written cache, so the metadata no longer parses.
What this error means
The build fails with error[E0786]: found invalid metadata files for crate X``. It often appears after a cache restore or a toolchain change and clears after a clean build.
error[E0786]: found invalid metadata files for crate `serde`
|
= note: failed to mmap file '.../target/debug/deps/libserde-abc.rmeta'Common causes
Stale target dir across toolchains
Artifacts compiled with one rustc are incompatible with another; a restored target/ from a different toolchain yields unreadable metadata.
Partially restored or corrupted cache
A CI cache that was truncated or restored mid-write leaves .rmeta files that rustc cannot parse.
How to fix it
Clean the target directory and rebuild
Remove the stale artifacts so cargo regenerates them.
cargo clean
cargo build --lockedKey the build cache on the toolchain
Include the rustc version in the cache key so a toolchain change invalidates old target artifacts.
- uses: actions/cache@v4
with:
path: target
key: target-${{ steps.toolchain.outputs.rustc }}-${{ hashFiles('Cargo.lock') }}How to prevent it
- Include the rustc/toolchain version in any
target/cache key. - Do not share a target dir across different toolchains.
- Run
cargo cleanwhen switching channels in a job.