{"id":"e21b17b8ae30fa3c","repo":"rust-lang/cargo","slug":"multiple-packages-found-at-src-to-disa","errorCode":null,"errorMessage":"multiple packages found at `{src}`:\n    {}\nTo disambiguate, run `cargo add --git {src} <package>`","messagePattern":"multiple packages found at `(.+?)`:\n    (.+?)\nTo disambiguate, run `cargo add --git (.+?) <package>`","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/ops/cargo_add/mod.rs","lineNumber":1057,"sourceCode":"}\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\"),\n        _ => {\n            let mut names: Vec<_> = packages\n                .iter()\n                .map(|p| p.name().as_str().to_owned())\n                .collect();\n            names.sort_unstable();\n            anyhow::bail!(\n                \"multiple packages found at `{src}`:\\n    {}\\nTo disambiguate, run `cargo add --git {src} <package>`\",\n                names\n                    .iter()\n                    .map(|s| s.to_string())\n                    .coalesce(|x, y| if x.len() + y.len() < 78 {\n                        Ok(format!(\"{x}, {y}\"))\n                    } else {\n                        Err((x, y))\n                    })\n                    .into_iter()\n                    .format(\"\\n    \"),\n            );\n        }\n    };\n    Ok(package)\n}\n\nfn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency {","sourceCodeStart":1039,"sourceCodeEnd":1075,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_add/mod.rs#L1039-L1075","documentation":"When a git source is given *without* an explicit crate spec, `infer_package_for_git_source` is asked to pick the single package. If the repo contains more than one package, cargo cannot guess which one, so it bails at mod.rs:1057 listing all package names and showing the disambiguating command `cargo add --git <src> <package>`.","triggerScenarios":"`cargo add --git <url>` against a monorepo/workspace with several crates and no positional package name.","commonSituations":"Adding a crate from a multi-crate git workspace (common for libraries that ship multiple sub-crates). The user expects a default but cargo requires an explicit choice.","solutions":["Re-run with the exact package name from the printed list: `cargo add --git <url> <package>`.","Inspect the repo's workspace members first (e.g. via the repo's root `Cargo.toml`) to pick the right name.","If you only ever need one crate from that repo, script the disambiguated command into a wrapper."],"exampleFix":"# before\ncargo add --git https://example/repo.git\n# error: multiple packages found: crate-a, crate-b\n\n# after\ncargo add --git https://example/repo.git crate-a","handlingStrategy":"validation","validationCode":"// Probe the git repo's package list before invoking cargo add without a name.\nfn list_git_packages(url: &str) -> Vec<String> {\n    // e.g. shallow clone + parse root [workspace].members, or use `cargo git-list`-style metadata\n    unimplemented!()\n}\n// let pkgs = list_git_packages(url);\n// assert_eq!(pkgs.len(), 1, \"disambiguate with: cargo add --git {url} <one-of:{pkgs:?}>\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass an explicit package name when adding from multi-package git repos.","Cache the repo's package list in your dependency tooling.","Prefer registry crates or published versions when a git repo has many crates."],"tags":["cargo-add","git","disambiguation","workspace"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}