rust-lang/cargo · error · anyhow::Error

multiple packages found at

Error message

multiple packages found at `{src}`:
    {}
To disambiguate, run `cargo add --git {src} <package>`

What it means

Thrown by infer_package_for_git_source when `cargo add --git <url>` resolves a repository that contains two or more packages and the user did not name which one to add. Unlike error 61, this is expected user-input ambiguity, so cargo prints the sorted package list and the exact disambiguation command.

Solutions

  1. Re-run with the package name appended, exactly as the error's `cargo add --git {src} <package>` hint shows.
  2. Pick the correct name from the printed sorted list (it is the `name = "..."` in each member's Cargo.toml).
  3. If only one crate is intended, refactor the source repo to a single package so the disambiguation is unnecessary.

Example fix

# before
cargo add --git https://github.com/org/repo
# after (name from the printed list)
cargo add --git https://github.com/org/repo mycrate
Defensive patterns

Strategy: validation

Validate before calling

// Enumerate packages in a git repo before adding.
// `git ls-remote` won't list packages; instead clone shallowly and scan:
fn packages_in_repo(url: &str) -> Vec<String> { /* clone --depth 1, walk Cargo.toml */ vec![] }

Prevention

When it happens

Trigger: Running `cargo add --git https://github.com/org/repo` against a repo with multiple workspace members; a git source whose root Cargo.toml is virtual; adding from a fork that added extra crates.

Common situations: Adding a crate that lives in a multi-crate monorepo (e.g., most larger Rust projects) without specifying the member; vendoring from a fork; CI scripts that add by URL only.

Related errors


AI-assisted analysis of rust-lang/cargo@98a09e7e7d (2026-08-11). Data as JSON: /api/errors/e21b17b8ae30fa3c. Report an issue: GitHub.

Appendix: source

Thrown at src/ops/cargo_add/mod.rs:1076

}

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 98a09e7e7d)