Skip to content

Commit 33d1361

Browse files
committed
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
1 parent 4da1b2e commit 33d1361

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
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/stderr.term.svg

+2-2
Loading

0 commit comments

Comments
 (0)