Skip to content

Commit

Permalink
Add suggestions when cargo add multiple packages
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Oct 7, 2022
1 parent c71f344 commit 6137d25
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 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(packages, &src, true)?;
Dependency::from(package.summary())
};
selected
Expand All @@ -314,7 +314,7 @@ fn resolve_dependency(
} else {
let source = crate::sources::PathSource::new(&path, src.source_id()?, config);
let packages = source.read_packages()?;
let package = infer_package(packages, &src)?;
let package = infer_package(packages, &src, false)?;
Dependency::from(package.summary())
};
selected
Expand Down Expand Up @@ -599,7 +599,11 @@ fn select_package(
}
}

fn infer_package(mut packages: Vec<Package>, src: &dyn std::fmt::Display) -> CargoResult<Package> {
fn infer_package(
mut packages: Vec<Package>,
src: &dyn std::fmt::Display,
is_git_source: bool,
) -> CargoResult<Package> {
let package = match packages.len() {
0 => {
anyhow::bail!("no packages found at `{src}`");
Expand All @@ -611,7 +615,24 @@ fn infer_package(mut packages: Vec<Package>, src: &dyn std::fmt::Display) -> Car
.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 {}{}",
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 "),
if is_git_source {
format!("\nTo disambiguate, run `cargo add --git {src} <crate>`")
} else {
"".to_owned()
}
);
}
};
Ok(package)
Expand Down

0 comments on commit 6137d25

Please sign in to comment.