Skip to content
Latchkey

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.

cargo
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.

Terminal
cargo clean
cargo build --locked

Key the build cache on the toolchain

Include the rustc version in the cache key so a toolchain change invalidates old target artifacts.

.github/workflows/ci.yml
- 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 clean when switching channels in a job.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →