rust-lang/cargo · error · anyhow::Error

failed to find entry for `{}` in directory source

Error message

failed to find entry for `{}` in directory source

What it means

In the directory source `verify`: a `PackageId` passed to `verify` was not present in the source's in-memory package map (`packages.get(&id)` returned `None`). Directory sources are populated by scanning their `.cargo-checksum.json`/index; an unknown id means the package was never registered in that directory source.

Source

Thrown at src/sources/directory.rs:236

            .get(&id)
            .map(|p| &p.0)
            .cloned()
            .map(MaybePackage::Ready)
            .ok_or_else(|| anyhow::format_err!("failed to find package with id: {}", id))
    }

    async fn finish_download(&self, _id: PackageId, _data: Vec<u8>) -> CargoResult<Package> {
        panic!("no downloads to do")
    }

    fn fingerprint(&self, pkg: &Package) -> CargoResult<String> {
        Ok(pkg.package_id().version().to_string())
    }

    fn verify(&self, id: PackageId) -> CargoResult<()> {
        let packages = self.packages.borrow_mut();
        let Some((pkg, cksum)) = packages.get(&id) else {
            anyhow::bail!("failed to find entry for `{}` in directory source", id);
        };

        for (file, cksum) in cksum.files.iter() {
            let file = pkg.root().join(file);
            let actual = Sha256::new()
                .update_path(&file)
                .with_context(|| format!("failed to calculate checksum of: {}", file.display()))?
                .finish_hex();
            if &*actual != cksum {
                anyhow::bail!(
                    "the listed checksum of `{}` has changed:\n\
                     expected: {}\n\
                     actual:   {}\n\
                     \n\
                     directory sources are not intended to be edited, if \
                     modifications are required then it is recommended \
                     that `[patch]` is used with a forked copy of the \
                     source\

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Re-run `cargo vendor` to regenerate the complete vendor directory.
  2. Verify the directory path in `.cargo/config.toml` `[source.<name>] directory = ` points at the right location.
  3. Check that every `[dependencies]` crate has a corresponding folder under the vendor directory.

Example fix

# before: vendor dir missing crate folder
# after
cargo vendor vendor  # regenerates all required crates
Defensive patterns

Strategy: validation

Validate before calling

# Verify the vendor dir contains every dependency before building offline:
cargo metadata --format-version 1 | jq -r '.packages[].name' | sort -u > /tmp/needed
ls vendor | sort -u > /tmp/present
comm -23 /tmp/needed /tmp/present  # empty = OK

Prevention

When it happens

Trigger: A `[source]` pointing at a directory (`directory = "..."`) that doesn't contain an entry for the requested package id — e.g. the vendored directory is missing a crate folder, or the id's name/version doesn't match any subdirectory. Reached when Cargo calls `Source::verify` on a directory source.

Common situations: Incomplete `cargo vendor` output (a crate folder deleted/missing); pointing `replace-with` at the wrong directory; a stale vendor directory after changing dependencies without re-running `cargo vendor`; case-sensitivity mismatch on case-insensitive filesystems.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/c7dcb73b6b0d0216.json. Report an issue: GitHub.