rust-lang/cargo · error · anyhow::Error
multiple packages found at `{src}`: {} To disambiguate,
Error message
multiple packages found at `{src}`:
{}
To disambiguate, run `cargo add --git {src} <package>` What it means
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>`.
Source
Thrown at src/ops/cargo_add/mod.rs:1057
}
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"),
_ => {
let mut names: Vec<_> = packages
.iter()
.map(|p| p.name().as_str().to_owned())
.collect();
names.sort_unstable();
anyhow::bail!(
"multiple packages found at `{src}`:\n {}\nTo disambiguate, run `cargo add --git {src} <package>`",
names
.iter()
.map(|s| s.to_string())
.coalesce(|x, y| if x.len() + y.len() < 78 {
Ok(format!("{x}, {y}"))
} else {
Err((x, y))
})
.into_iter()
.format("\n "),
);
}
};
Ok(package)
}
fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency {View on GitHub (pinned to 0e07a15537)
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.
Example fix
# before cargo add --git https://example/repo.git # error: multiple packages found: crate-a, crate-b # after cargo add --git https://example/repo.git crate-a
Defensive patterns
Strategy: validation
Validate before calling
// Probe the git repo's package list before invoking cargo add without a name.
fn list_git_packages(url: &str) -> Vec<String> {
// e.g. shallow clone + parse root [workspace].members, or use `cargo git-list`-style metadata
unimplemented!()
}
// let pkgs = list_git_packages(url);
// assert_eq!(pkgs.len(), 1, "disambiguate with: cargo add --git {url} <one-of:{pkgs:?}>"); Prevention
- 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.
When it happens
Trigger: `cargo add --git <url>` against a monorepo/workspace with several crates and no positional package name.
Common situations: 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.
Related errors
- cannot specify a git URL (`{url}`) with a version (`{v}`).
- dependency ({}) specified without providing a local path, Gi
- cannot override workspace dependency with `--default-feature
- cannot override workspace dependency with `--registry`, eith
- cannot override workspace dependency with `--rename`, either
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/e21b17b8ae30fa3c.json.
Report an issue: GitHub.