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

the crate ` ` could not be found at

Error message

the crate `{dependency}` could not be found at `{source}`

What it means

Thrown by cargo_add's select_package after querying the registry/path/git source for a dependency and receiving zero Candidate summaries. The dependency's source was already resolved (the expect proves it), but no index entry matched the crate name at that source. This is the 'add' analog of a 404: the name does not exist where you told cargo to look.

Solutions

  1. Verify the package name spelling matches the `name` field in the source's Cargo.toml, not the directory or git URL.
  2. For --path, confirm the path points at the directory containing the crate's Cargo.toml (not a parent or sibling).
  3. For --git, list packages in the repo first: `cargo add --git <url>` and, if multiple, append the exact package name as shown in the disambiguation hint.
  4. If using a custom registry, check the crate is published there (`cargo search <name> --registry <name>`) and that the registry index is up to date.

Example fix

// before
cargo add mycrate --path ./sibling
// after (path containing Cargo.toml)
cargo add mycrate --path ./sibling/mycrate
Defensive patterns

Strategy: validation

Validate before calling

// Before calling cargo add programmatically, confirm the source contains the crate.
use std::path::Path;
fn path_has_crate(dir: &Path, name: &str) -> bool {
    let manifest = dir.join("Cargo.toml");
    std::fs::read_to_string(manifest)
        .map(|t| t.contains(&format!("name = \"{name}\"")))
        .unwrap_or(false)
}

Prevention

When it happens

Trigger: Running `cargo add <name> --path <dir>` or `--git <url>` where the directory/repository contains no package with that name, or `cargo add <name>` against a private/alternate registry that does not index the crate. Also triggered by a typo in the crate name passed to a path/git source, or by pointing --path at a subdirectory that lacks Cargo.toml.

Common situations: Renaming a crate in the source repo but adding it by its old name; pointing --git at a monorepo and forgetting the package lives in a subdir; using a sparse/git registry mirror that has not yet replicated the crate; network/registry-index fetch silently returning an empty result set.

Related errors


AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11). Data as JSON: /api/errors/c3c5a2b2fa2bdf64. Report an issue: GitHub.

Appendix: source

Thrown at src/ops/cargo_add/mod.rs:1033

        }
        MaybeWorkspace::Other(query) => {
            let possibilities =
                crate::util::block_on(registry.query_vec(&query, QueryKind::Normalized))?;

            let possibilities: Vec<_> = possibilities
                .into_iter()
                .filter_map(|s| match s {
                    IndexSummary::Candidate(s) => Some(s),
                    _ => None,
                })
                .collect();

            match possibilities.len() {
                0 => {
                    let source = dependency
                        .source()
                        .expect("source should be resolved before here");
                    anyhow::bail!("the crate `{dependency}` could not be found at `{source}`")
                }
                1 => {
                    let mut dep = Dependency::from(&possibilities[0]);
                    if let Some(reg_name) = dependency.registry.as_deref() {
                        dep = dep.set_registry(reg_name);
                    }
                    if let Some(Source::Path(PathSource { base, .. })) = dependency.source() {
                        if let Some(Source::Path(dep_src)) = &mut dep.source {
                            dep_src.base = base.clone();
                        }
                    }
                    Ok(dep)
                }
                _ => {
                    let source = dependency
                        .source()
                        .expect("source should be resolved before here");
                    anyhow::bail!(

View on GitHub (pinned to 98a09e7e7d)