Skip to content

Commit

Permalink
Auto merge of rust-lang#12865 - hi-rustin:rustin-patch-remove, r=epage
Browse files Browse the repository at this point in the history
Add better error message when it can not find the search section
  • Loading branch information
bors committed Nov 7, 2023
2 parents f095603 + fb0bbe3 commit d899b51
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
43 changes: 31 additions & 12 deletions src/cargo/util/toml_mut/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,31 @@ impl LocalManifest {
pub fn remove_from_table(&mut self, table_path: &[String], name: &str) -> CargoResult<()> {
let parent_table = self.get_table_mut(table_path)?;

let dep = parent_table
.get_mut(name)
.filter(|t| !t.is_none())
.ok_or_else(|| non_existent_dependency_err(name, table_path.join(".")))?;

// remove the dependency
*dep = toml_edit::Item::None;
match parent_table.get_mut(name).filter(|t| !t.is_none()) {
Some(dep) => {
// remove the dependency
*dep = toml_edit::Item::None;

// remove table if empty
if parent_table.as_table_like().unwrap().is_empty() {
*parent_table = toml_edit::Item::None;
}
}
None => {
// Search in other tables.
let sections = self.get_sections();
let found_table_path = sections.iter().find_map(|(t, i)| {
let table_path: Vec<String> =
t.to_table().iter().map(|s| s.to_string()).collect();
i.get(name).is_some().then(|| table_path.join("."))
});

// remove table if empty
if parent_table.as_table_like().unwrap().is_empty() {
*parent_table = toml_edit::Item::None;
return Err(non_existent_dependency_err(
name,
table_path.join("."),
found_table_path,
));
}
}

Ok(())
Expand Down Expand Up @@ -537,9 +551,14 @@ fn non_existent_table_err(table: impl std::fmt::Display) -> anyhow::Error {

fn non_existent_dependency_err(
name: impl std::fmt::Display,
table: impl std::fmt::Display,
search_table: impl std::fmt::Display,
found_table: Option<impl std::fmt::Display>,
) -> anyhow::Error {
anyhow::format_err!("the dependency `{name}` could not be found in `{table}`.")
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}`",));
}
anyhow::format_err!(msg)
}

fn remove_array_index(array: &mut toml_edit::Array, index: usize) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_remove/invalid_dep/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Removing invalid_dependency_name from dependencies
error: the dependency `invalid_dependency_name` could not be found in `dependencies`.
error: the dependency `invalid_dependency_name` could not be found in `dependencies`
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_remove/invalid_section/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Removing docopt from build-dependencies
error: the dependency `docopt` could not be found in `build-dependencies`.
error: the dependency `docopt` could not be found in `build-dependencies`; it is present in `dependencies`
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Removing semver from dev-dependencies
error: the dependency `semver` could not be found in `dev-dependencies`.
error: the dependency `semver` could not be found in `dev-dependencies`; it is present in `dependencies`
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_remove/invalid_target/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Removing dbus from dependencies for target `powerpc-unknown-linux-gnu`
error: the dependency `dbus` could not be found in `target.powerpc-unknown-linux-gnu.dependencies`.
error: the dependency `dbus` could not be found in `target.powerpc-unknown-linux-gnu.dependencies`; it is present in `target.x86_64-unknown-linux-gnu.dependencies`
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_remove/invalid_target_dep/stderr.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Removing toml from dependencies for target `x86_64-unknown-linux-gnu`
error: the dependency `toml` could not be found in `target.x86_64-unknown-linux-gnu.dependencies`.
error: the dependency `toml` could not be found in `target.x86_64-unknown-linux-gnu.dependencies`; it is present in `dependencies`

0 comments on commit d899b51

Please sign in to comment.