Skip to content

Commit

Permalink
fix(remove): On error, suggest other dependencies (rust-lang#14818)
Browse files Browse the repository at this point in the history
### 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 rust-lang#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
  • Loading branch information
ehuss authored Nov 14, 2024
2 parents cfea065 + 33d1361 commit c54a8e8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/cargo/util/toml_mut/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use anyhow::Context as _;
use super::dependency::Dependency;
use crate::core::dependency::DepKind;
use crate::core::{FeatureValue, Features, Workspace};
use crate::util::closest;
use crate::util::interning::InternedString;
use crate::{CargoResult, GlobalContext};

Expand Down Expand Up @@ -381,6 +382,13 @@ impl LocalManifest {
}
}
None => {
let names = parent_table
.as_table_like()
.map(|t| t.iter())
.into_iter()
.flatten();
let alt_name = closest(name, names.map(|(k, _)| k), |k| k).map(|n| n.to_owned());

// Search in other tables.
let sections = self.get_sections();
let found_table_path = sections.iter().find_map(|(t, i)| {
Expand All @@ -393,6 +401,7 @@ impl LocalManifest {
name,
table_path.join("."),
found_table_path,
alt_name.as_deref(),
));
}
}
Expand Down Expand Up @@ -605,10 +614,13 @@ fn non_existent_dependency_err(
name: impl std::fmt::Display,
search_table: impl std::fmt::Display,
found_table: Option<impl std::fmt::Display>,
alt_name: Option<&str>,
) -> anyhow::Error {
let mut msg = format!("the dependency `{name}` could not be found in `{search_table}`");
if let Some(found_table) = found_table {
msg.push_str(&format!("; it is present in `{found_table}`",));
} else if let Some(alt_name) = alt_name {
msg.push_str(&format!("; dependency `{alt_name}` exists",));
}
anyhow::format_err!(msg)
}
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/cargo_remove/invalid_dep/in

This file was deleted.

22 changes: 22 additions & 0 deletions tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "cargo-remove-test-fixture"
version = "0.1.0"
edition = "2015"

[[bin]]
name = "main"
path = "src/main.rs"

[build-dependencies]
semver = "0.1.0"

[dependencies]
invalid-dependency-name = "0.6"
docopt = "0.6"
semver = "0.1"

[dev-dependencies]
serde = "1.0.90"

[features]
std = ["serde/std", "semver/std"]
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/invalid_dep/in/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 2 additions & 4 deletions tests/testsuite/cargo_remove/invalid_dep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use cargo_test_support::Project;
#[cargo_test]
fn case() {
cargo_test_support::registry::init();
cargo_test_support::registry::Package::new("clippy", "0.4.0+my-package").publish();
cargo_test_support::registry::Package::new("invalid-dependency-name", "0.6.2+my-package")
.publish();
cargo_test_support::registry::Package::new("docopt", "0.6.2+my-package").publish();
cargo_test_support::registry::Package::new("regex", "0.1.1+my-package").publish();
cargo_test_support::registry::Package::new("rustc-serialize", "0.4.0+my-package").publish();
cargo_test_support::registry::Package::new("toml", "0.1.1+my-package").publish();
cargo_test_support::registry::Package::new("semver", "0.1.1")
.feature("std", &[])
.publish();
Expand Down
5 changes: 1 addition & 4 deletions tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ path = "src/main.rs"
semver = "0.1.0"

[dependencies]
invalid-dependency-name = "0.6"
docopt = "0.6"
rustc-serialize = "0.4"
semver = "0.1"
toml = "0.1"
clippy = "0.4"

[dev-dependencies]
regex = "0.1.1"
serde = "1.0.90"

[features]
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/cargo_remove/invalid_dep/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c54a8e8

Please sign in to comment.