mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
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
This commit is contained in:
parent
4da1b2ee78
commit
33d1361bb8
@ -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<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)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
<svg width="740px" height="74px" xmlns="http://www.w3.org/2000/svg">
|
||||
<svg width="1104px" height="74px" xmlns="http://www.w3.org/2000/svg">
|
||||
<style>
|
||||
.fg { fill: #AAAAAA }
|
||||
.bg { background: #000000 }
|
||||
@ -21,7 +21,7 @@
|
||||
<text xml:space="preserve" class="container fg">
|
||||
<tspan x="10px" y="28px"><tspan class="fg-green bold"> Removing</tspan><tspan> invalid_dependency_name from dependencies</tspan>
|
||||
</tspan>
|
||||
<tspan x="10px" y="46px"><tspan class="fg-red bold">error</tspan><tspan class="bold">:</tspan><tspan> the dependency `invalid_dependency_name` could not be found in `dependencies`</tspan>
|
||||
<tspan x="10px" y="46px"><tspan class="fg-red bold">error</tspan><tspan class="bold">:</tspan><tspan> the dependency `invalid_dependency_name` could not be found in `dependencies`; dependency `invalid-dependency-name` exists</tspan>
|
||||
</tspan>
|
||||
<tspan x="10px" y="64px">
|
||||
</tspan>
|
||||
|
Before Width: | Height: | Size: 979 B After Width: | Height: | Size: 1.0 KiB |
Loading…
x
Reference in New Issue
Block a user