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

unexpectedly found multiple copies of crate `{dependency}` a

Error message

unexpectedly found multiple copies of crate `{dependency}` at `{source}`

What it means

The inverse of the not-found case: `select_package` expects exactly one matching package, but the source returned multiple candidates with the same name. This is an unexpected/ambiguous state for a path or git source (which normally yield one package per name), so cargo-add bails at mod.rs:1032 rather than guessing.

Source

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

                    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!(
                        "unexpectedly found multiple copies of crate `{dependency}` at `{source}`"
                    )
                }
            }
        }
    }
}

fn infer_package_for_git_source(
    mut packages: Vec<Package>,
    src: &dyn std::fmt::Display,
) -> CargoResult<Package> {
    let package = match packages.len() {
        0 => unreachable!(
            "this function should only be called with packages from `GitSource::read_packages` \
            and that call should error for us when there are no packages"
        ),
        1 => packages.pop().expect("match ensured element is present"),

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Point `--path` at the specific member directory rather than the workspace root.
  2. For a git source with multiple packages, use `cargo add --git <url> <package>` to disambiguate (see also error 74).
  3. Audit the source for duplicate package names and rename one of them.

Example fix

# before (path is workspace root)
cargo add --path ./workspace mycrate

# after (point at the member)
cargo add --path ./workspace/crates/mycrate mycrate
Defensive patterns

Strategy: validation

Validate before calling

// Count packages with the target name at the source; require exactly one.
fn count_packages_at(path: &std::path::Path, name: &str) -> usize {
    // walk path for Cargo.toml files, parse [package].name, count matches
    // return the count; bail yourself if > 1 before calling cargo add
    unimplemented!()
}

Prevention

When it happens

Trigger: A `--path` directory containing multiple manifests defining the same package name (e.g. nested crates with duplicate names), or a git repo checkout where the query resolves to duplicate package entries.

Common situations: A path pointing at a workspace root instead of a specific member, or a git source whose workspace has duplicate package names after a merge.

Related errors


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