rust-lang/cargo · error · anyhow::Error
unexpectedly found multiple copies of crate
Error message
unexpectedly found multiple copies of crate `{dependency}` at `{source}` What it means
Thrown by select_package when a path or git dependency query returns MORE than one Candidate summary. Path and git sources are expected to resolve to a single package; multiple candidates indicate the source directory holds several crates with colliding identity or the query matched more than expected. Cargo refuses to guess.
Solutions
- Narrow the source to the specific member crate directory: `cargo add <name> --path ./member-dir`.
- For git, specify the package explicitly: `cargo add <name> --git <url> <package>`.
- Audit the source directory for duplicate Cargo.toml files and remove or restructure the collision.
- Clear cargo's registry/path cache if you suspect stale duplicate summaries.
Example fix
// before cargo add mycrate --path ./workspace-root // after cargo add mycrate --path ./workspace-root/crates/mycrate
Defensive patterns
Strategy: validation
Validate before calling
// Detect multiple packages at a path/git source before adding.
use std::path::{Path, PathBuf};
fn member_count(dir: &Path) -> usize {
glob::glob(&format!("{dir}/**/Cargo.toml"))
.map(|it| it.filter_map(Result::ok).count())
.unwrap_or(0)
} Prevention
- Point --path at the specific member crate directory, never a workspace root.
- For git, always pass the package name: `cargo add --git <url> <pkg>`.
- Audit vendored directories for duplicate Cargo.toml files.
When it happens
Trigger: Pointing `cargo add --path ./workspace-root` at a directory whose Cargo.toml is a virtual manifest with multiple members; a git source whose repo root contains several crates all matching the dependency query; a path source with multiple Cargo.toml files resolved by the query.
Common situations: Cloning a workspace repo and adding by root path instead of the member path; vendored directory containing both a library and a same-named binary crate; stale registry cache returning duplicate summaries for one source.
Related errors
- the crate ` ` could not be found at
- multiple packages found at
- all versions of crate
- can't checkout from ' ': you are in the offline mode ( )
- cannot add ` ` as a dependency to itself
AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11).
Data as JSON: /api/errors/8c3bfe39de49b7db.
Report an issue: GitHub.
Appendix: source
Thrown at src/ops/cargo_add/mod.rs:1051
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 98a09e7e7d)