Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestions when cargo add multiple packages #11186

Merged
merged 5 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ fn resolve_dependency(
} else {
let mut source = crate::sources::GitSource::new(src.source_id()?, config)?;
let packages = source.read_packages()?;
let package = infer_package(packages, &src)?;
let package = infer_package_for_git_source(packages, &src)?;
Dependency::from(package.summary())
};
selected
Expand All @@ -313,8 +313,7 @@ fn resolve_dependency(
selected
} else {
let source = crate::sources::PathSource::new(&path, src.source_id()?, config);
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
let packages = source.read_packages()?;
let package = infer_package(packages, &src)?;
let package = source.read_packages()?.pop().expect("at least one package");
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
Dependency::from(package.summary())
};
selected
Expand Down Expand Up @@ -599,19 +598,32 @@ fn select_package(
}
}

fn infer_package(mut packages: Vec<Package>, src: &dyn std::fmt::Display) -> CargoResult<Package> {
fn infer_package_for_git_source(
mut packages: Vec<Package>,
src: &dyn std::fmt::Display,
) -> CargoResult<Package> {
let package = match packages.len() {
0 => {
anyhow::bail!("no packages found at `{src}`");
}
0 => unreachable!(),
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
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}`: {}", names.join(", "));
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 {
Rustin170506 marked this conversation as resolved.
Show resolved Hide resolved
Ok(format!("{x}, {y}"))
} else {
Err((x, y))
})
.into_iter()
.format("\n "),
);
}
};
Ok(package)
Expand Down
35 changes: 35 additions & 0 deletions tests/testsuite/cargo_add/git_inferred_name_multiple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,41 @@ fn git_inferred_name_multiple() {
&cargo_test_support::basic_manifest("my-package2", "0.3.0+my-package2"),
)
.file("p2/src/lib.rs", "")
.file(
"p3/Cargo.toml",
&cargo_test_support::basic_manifest("my-package3", "0.3.0+my-package2"),
)
.file("p3/src/lib.rs", "")
.file(
"p4/Cargo.toml",
&cargo_test_support::basic_manifest("my-package4", "0.3.0+my-package2"),
)
.file("p4/src/lib.rs", "")
.file(
"p5/Cargo.toml",
&cargo_test_support::basic_manifest("my-package5", "0.3.0+my-package2"),
)
.file("p5/src/lib.rs", "")
.file(
"p6/Cargo.toml",
&cargo_test_support::basic_manifest("my-package6", "0.3.0+my-package2"),
)
.file("p6/src/lib.rs", "")
.file(
"p7/Cargo.toml",
&cargo_test_support::basic_manifest("my-package7", "0.3.0+my-package2"),
)
.file("p7/src/lib.rs", "")
.file(
"p8/Cargo.toml",
&cargo_test_support::basic_manifest("my-package8", "0.3.0+my-package2"),
)
.file("p8/src/lib.rs", "")
.file(
"p9/Cargo.toml",
&cargo_test_support::basic_manifest("my-package9", "0.3.0+my-package2"),
)
.file("p9/src/lib.rs", "")
});
let git_url = git_dep.url().to_string();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Updating git repository `[ROOTURL]/git-package`
error: multiple packages found at `[ROOTURL]/git-package`: my-package1, my-package2
error: multiple packages found at `[ROOTURL]/git-package`:
my-package1, my-package2, my-package3, my-package4, my-package5, my-package6
my-package7, my-package8, my-package9
To disambiguate, run `cargo add --git [ROOTURL]/git-package <package>`