From f7f2206474d751c3137f35809c26837ab100f509 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 13 Nov 2024 15:22:54 -0600 Subject: [PATCH 1/4] test(remove): Don't use shared fixture --- tests/testsuite/cargo_remove/invalid_dep/in | 1 - .../cargo_remove/invalid_dep/in/Cargo.toml | 25 +++++++++++++++++++ .../cargo_remove/invalid_dep/in/src/lib.rs | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) delete mode 120000 tests/testsuite/cargo_remove/invalid_dep/in create mode 100644 tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml create mode 100644 tests/testsuite/cargo_remove/invalid_dep/in/src/lib.rs diff --git a/tests/testsuite/cargo_remove/invalid_dep/in b/tests/testsuite/cargo_remove/invalid_dep/in deleted file mode 120000 index 7fd0ba5ebcc..00000000000 --- a/tests/testsuite/cargo_remove/invalid_dep/in +++ /dev/null @@ -1 +0,0 @@ -../remove-basic.in/ \ No newline at end of file diff --git a/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml b/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml new file mode 100644 index 00000000000..fe49a10648d --- /dev/null +++ b/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml @@ -0,0 +1,25 @@ +[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] +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] +std = ["serde/std", "semver/std"] diff --git a/tests/testsuite/cargo_remove/invalid_dep/in/src/lib.rs b/tests/testsuite/cargo_remove/invalid_dep/in/src/lib.rs new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/tests/testsuite/cargo_remove/invalid_dep/in/src/lib.rs @@ -0,0 +1 @@ + From cad379f3e34b3c7167ff67186cbb55039e6ea857 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 13 Nov 2024 15:24:12 -0600 Subject: [PATCH 2/4] test(remove): Reduce the fixture size --- tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml | 4 ---- tests/testsuite/cargo_remove/invalid_dep/mod.rs | 4 ---- tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml | 4 ---- 3 files changed, 12 deletions(-) diff --git a/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml b/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml index fe49a10648d..c38ef4c9d71 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml +++ b/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml @@ -12,13 +12,9 @@ semver = "0.1.0" [dependencies] 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] diff --git a/tests/testsuite/cargo_remove/invalid_dep/mod.rs b/tests/testsuite/cargo_remove/invalid_dep/mod.rs index 50eb967f2d4..a563f481f1e 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/mod.rs +++ b/tests/testsuite/cargo_remove/invalid_dep/mod.rs @@ -9,11 +9,7 @@ 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("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(); diff --git a/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml b/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml index fe49a10648d..c38ef4c9d71 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml +++ b/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml @@ -12,13 +12,9 @@ semver = "0.1.0" [dependencies] 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] From 4da1b2ee7892bccf6a0fad8d5643e96f05195c80 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 13 Nov 2024 15:25:15 -0600 Subject: [PATCH 3/4] test(remove): Update fixture for a suggestion --- tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml | 1 + tests/testsuite/cargo_remove/invalid_dep/mod.rs | 2 ++ tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml | 1 + 3 files changed, 4 insertions(+) diff --git a/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml b/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml index c38ef4c9d71..1a373b7dae4 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml +++ b/tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml @@ -11,6 +11,7 @@ path = "src/main.rs" semver = "0.1.0" [dependencies] +invalid-dependency-name = "0.6" docopt = "0.6" semver = "0.1" diff --git a/tests/testsuite/cargo_remove/invalid_dep/mod.rs b/tests/testsuite/cargo_remove/invalid_dep/mod.rs index a563f481f1e..40da2984207 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/mod.rs +++ b/tests/testsuite/cargo_remove/invalid_dep/mod.rs @@ -9,6 +9,8 @@ use cargo_test_support::Project; #[cargo_test] fn case() { cargo_test_support::registry::init(); + 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("semver", "0.1.1") .feature("std", &[]) diff --git a/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml b/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml index c38ef4c9d71..1a373b7dae4 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml +++ b/tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml @@ -11,6 +11,7 @@ path = "src/main.rs" semver = "0.1.0" [dependencies] +invalid-dependency-name = "0.6" docopt = "0.6" semver = "0.1" From 33d1361bb88ae974733c16aaca977f59a958bca5 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 13 Nov 2024 15:26:11 -0600 Subject: [PATCH 4/4] fix(remove): On error, suggest other dependencies `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". 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. Related to #14814 --- src/cargo/util/toml_mut/manifest.rs | 12 ++++++++++++ .../cargo_remove/invalid_dep/stderr.term.svg | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/toml_mut/manifest.rs b/src/cargo/util/toml_mut/manifest.rs index e166a24f2be..98033531e3a 100644 --- a/src/cargo/util/toml_mut/manifest.rs +++ b/src/cargo/util/toml_mut/manifest.rs @@ -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}; @@ -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)| { @@ -393,6 +401,7 @@ impl LocalManifest { name, table_path.join("."), found_table_path, + alt_name.as_deref(), )); } } @@ -605,10 +614,13 @@ fn non_existent_dependency_err( name: impl std::fmt::Display, search_table: impl std::fmt::Display, found_table: Option, + 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) } diff --git a/tests/testsuite/cargo_remove/invalid_dep/stderr.term.svg b/tests/testsuite/cargo_remove/invalid_dep/stderr.term.svg index 3b7fb2b807d..5eecfe5e7da 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/stderr.term.svg +++ b/tests/testsuite/cargo_remove/invalid_dep/stderr.term.svg @@ -1,4 +1,4 @@ - +