{"record":{"id":"8c3bfe39de49b7db","repo":"rust-lang/cargo","slug":"unexpectedly-found-multiple-copies-of-crate-depe","errorCode":null,"errorMessage":"unexpectedly found multiple copies of crate `{dependency}` at `{source}`","messagePattern":"unexpectedly found multiple copies of crate `(.+?)` at `(.+?)`","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":1051,"sourceCode":"                    anyhow::bail!(\"the crate `{dependency}` could not be found at `{source}`\")\n                }\n                1 => {\n                    let mut dep = Dependency::from(&possibilities[0]);\n                    if let Some(reg_name) = dependency.registry.as_deref() {\n                        dep = dep.set_registry(reg_name);\n                    }\n                    if let Some(Source::Path(PathSource { base, .. })) = dependency.source() {\n                        if let Some(Source::Path(dep_src)) = &mut dep.source {\n                            dep_src.base = base.clone();\n                        }\n                    }\n                    Ok(dep)\n                }\n                _ => {\n                    let source = dependency\n                        .source()\n                        .expect(\"source should be resolved before here\");\n                    anyhow::bail!(\n                        \"unexpectedly found multiple copies of crate `{dependency}` at `{source}`\"\n                    )\n                }\n            }\n        }\n    }\n}\n\nfn infer_package_for_git_source(\n    mut packages: Vec<Package>,\n    src: &dyn std::fmt::Display,\n) -> CargoResult<Package> {\n    let package = match packages.len() {\n        0 => unreachable!(\n            \"this function should only be called with packages from `GitSource::read_packages` \\\n            and that call should error for us when there are no packages\"\n        ),\n        1 => packages.pop().expect(\"match ensured element is present\"),","sourceCodeStart":1033,"sourceCodeEnd":1069,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/ops/cargo_add/mod.rs#L1033-L1069","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ncargo add mycrate --path ./workspace-root\n// after\ncargo add mycrate --path ./workspace-root/crates/mycrate","handlingStrategy":"validation","validationCode":"// Detect multiple packages at a path/git source before adding.\nuse std::path::{Path, PathBuf};\nfn member_count(dir: &Path) -> usize {\n    glob::glob(&format!(\"{dir}/**/Cargo.toml\"))\n        .map(|it| it.filter_map(Result::ok).count())\n        .unwrap_or(0)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["cargo-add","dependency-resolution","path-source","git-source","ambiguous-match"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}