Skip to content

Commit c54a8e8

Browse files
authored
fix(remove): On error, suggest other dependencies (#14818)
### What does this PR try to resolve? `cargo remove` already will suggest other tables if appropriate. `cargo add` auto-corrects `_` and `-`. This is an extension of the two by suggesting existing dependencies within the same table that are "close". Related to #14814 ### How should we test and review this PR? I chose to make alt tables and alt deps mutually exclusive in the message because I didn't wantto deal with how to separate things if both show up. Most likely, people will only expect one or the other and if its in an alt table, then most likely they mean that. ### Additional information
2 parents cfea065 + 33d1361 commit c54a8e8

File tree

7 files changed

+40
-11
lines changed

7 files changed

+40
-11
lines changed

src/cargo/util/toml_mut/manifest.rs

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use anyhow::Context as _;
99
use super::dependency::Dependency;
1010
use crate::core::dependency::DepKind;
1111
use crate::core::{FeatureValue, Features, Workspace};
12+
use crate::util::closest;
1213
use crate::util::interning::InternedString;
1314
use crate::{CargoResult, GlobalContext};
1415

@@ -381,6 +382,13 @@ impl LocalManifest {
381382
}
382383
}
383384
None => {
385+
let names = parent_table
386+
.as_table_like()
387+
.map(|t| t.iter())
388+
.into_iter()
389+
.flatten();
390+
let alt_name = closest(name, names.map(|(k, _)| k), |k| k).map(|n| n.to_owned());
391+
384392
// Search in other tables.
385393
let sections = self.get_sections();
386394
let found_table_path = sections.iter().find_map(|(t, i)| {
@@ -393,6 +401,7 @@ impl LocalManifest {
393401
name,
394402
table_path.join("."),
395403
found_table_path,
404+
alt_name.as_deref(),
396405
));
397406
}
398407
}
@@ -605,10 +614,13 @@ fn non_existent_dependency_err(
605614
name: impl std::fmt::Display,
606615
search_table: impl std::fmt::Display,
607616
found_table: Option<impl std::fmt::Display>,
617+
alt_name: Option<&str>,
608618
) -> anyhow::Error {
609619
let mut msg = format!("the dependency `{name}` could not be found in `{search_table}`");
610620
if let Some(found_table) = found_table {
611621
msg.push_str(&format!("; it is present in `{found_table}`",));
622+
} else if let Some(alt_name) = alt_name {
623+
msg.push_str(&format!("; dependency `{alt_name}` exists",));
612624
}
613625
anyhow::format_err!(msg)
614626
}

tests/testsuite/cargo_remove/invalid_dep/in

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "cargo-remove-test-fixture"
3+
version = "0.1.0"
4+
edition = "2015"
5+
6+
[[bin]]
7+
name = "main"
8+
path = "src/main.rs"
9+
10+
[build-dependencies]
11+
semver = "0.1.0"
12+
13+
[dependencies]
14+
invalid-dependency-name = "0.6"
15+
docopt = "0.6"
16+
semver = "0.1"
17+
18+
[dev-dependencies]
19+
serde = "1.0.90"
20+
21+
[features]
22+
std = ["serde/std", "semver/std"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/testsuite/cargo_remove/invalid_dep/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ use cargo_test_support::Project;
99
#[cargo_test]
1010
fn case() {
1111
cargo_test_support::registry::init();
12-
cargo_test_support::registry::Package::new("clippy", "0.4.0+my-package").publish();
12+
cargo_test_support::registry::Package::new("invalid-dependency-name", "0.6.2+my-package")
13+
.publish();
1314
cargo_test_support::registry::Package::new("docopt", "0.6.2+my-package").publish();
14-
cargo_test_support::registry::Package::new("regex", "0.1.1+my-package").publish();
15-
cargo_test_support::registry::Package::new("rustc-serialize", "0.4.0+my-package").publish();
16-
cargo_test_support::registry::Package::new("toml", "0.1.1+my-package").publish();
1715
cargo_test_support::registry::Package::new("semver", "0.1.1")
1816
.feature("std", &[])
1917
.publish();

tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ path = "src/main.rs"
1111
semver = "0.1.0"
1212

1313
[dependencies]
14+
invalid-dependency-name = "0.6"
1415
docopt = "0.6"
15-
rustc-serialize = "0.4"
1616
semver = "0.1"
17-
toml = "0.1"
18-
clippy = "0.4"
1917

2018
[dev-dependencies]
21-
regex = "0.1.1"
2219
serde = "1.0.90"
2320

2421
[features]

tests/testsuite/cargo_remove/invalid_dep/stderr.term.svg

+2-2
Loading

0 commit comments

Comments
 (0)