{"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":1032,"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":1014,"sourceCodeEnd":1050,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L1014-L1050","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Point `--path` at the specific member directory rather than the workspace root.","For a git source with multiple packages, use `cargo add --git <url> <package>` to disambiguate (see also error 74).","Audit the source for duplicate package names and rename one of them."],"exampleFix":"# before (path is workspace root)\ncargo add --path ./workspace mycrate\n\n# after (point at the member)\ncargo add --path ./workspace/crates/mycrate mycrate","handlingStrategy":"validation","validationCode":"// Count packages with the target name at the source; require exactly one.\nfn count_packages_at(path: &std::path::Path, name: &str) -> usize {\n    // walk path for Cargo.toml files, parse [package].name, count matches\n    // return the count; bail yourself if > 1 before calling cargo add\n    unimplemented!()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Point `--path` at a single member, never a workspace root, for path adds.","For git sources with duplicate names, disambiguate with the explicit package name.","Run a duplicate-name lint in your workspace CI."],"tags":["cargo-add","path","git","ambiguous"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}