From fb0bbe36f1b5621109a82eaafa22d7320451285c Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 26 Oct 2023 20:52:12 +0800 Subject: [PATCH] Search dep in other tables and update tests Signed-off-by: hi-rustin --- src/cargo/util/toml_mut/manifest.rs | 43 +++++++++++++------ .../cargo_remove/invalid_dep/stderr.log | 2 +- .../cargo_remove/invalid_section/stderr.log | 2 +- .../invalid_section_dep/stderr.log | 2 +- .../cargo_remove/invalid_target/stderr.log | 2 +- .../invalid_target_dep/stderr.log | 2 +- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/cargo/util/toml_mut/manifest.rs b/src/cargo/util/toml_mut/manifest.rs index 445272788bf..e859af2153a 100644 --- a/src/cargo/util/toml_mut/manifest.rs +++ b/src/cargo/util/toml_mut/manifest.rs @@ -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 = + 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(()) @@ -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, ) -> 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) { diff --git a/tests/testsuite/cargo_remove/invalid_dep/stderr.log b/tests/testsuite/cargo_remove/invalid_dep/stderr.log index eea124d6580..9bb137d1f46 100644 --- a/tests/testsuite/cargo_remove/invalid_dep/stderr.log +++ b/tests/testsuite/cargo_remove/invalid_dep/stderr.log @@ -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` diff --git a/tests/testsuite/cargo_remove/invalid_section/stderr.log b/tests/testsuite/cargo_remove/invalid_section/stderr.log index fff5ff00ae7..8cbafa98e1a 100644 --- a/tests/testsuite/cargo_remove/invalid_section/stderr.log +++ b/tests/testsuite/cargo_remove/invalid_section/stderr.log @@ -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` diff --git a/tests/testsuite/cargo_remove/invalid_section_dep/stderr.log b/tests/testsuite/cargo_remove/invalid_section_dep/stderr.log index 1926f9577e4..60c0f8b41a2 100644 --- a/tests/testsuite/cargo_remove/invalid_section_dep/stderr.log +++ b/tests/testsuite/cargo_remove/invalid_section_dep/stderr.log @@ -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` diff --git a/tests/testsuite/cargo_remove/invalid_target/stderr.log b/tests/testsuite/cargo_remove/invalid_target/stderr.log index 5075b80b70c..eae9f788762 100644 --- a/tests/testsuite/cargo_remove/invalid_target/stderr.log +++ b/tests/testsuite/cargo_remove/invalid_target/stderr.log @@ -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` diff --git a/tests/testsuite/cargo_remove/invalid_target_dep/stderr.log b/tests/testsuite/cargo_remove/invalid_target_dep/stderr.log index 54bfe085f15..635a7bd6c8f 100644 --- a/tests/testsuite/cargo_remove/invalid_target_dep/stderr.log +++ b/tests/testsuite/cargo_remove/invalid_target_dep/stderr.log @@ -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`