Skip to content

Commit f166eea

Browse files
committed
Search dep in other tables and update tests
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent 7f3fc2b commit f166eea

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

src/cargo/util/toml_mut/manifest.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,31 @@ impl LocalManifest {
367367
pub fn remove_from_table(&mut self, table_path: &[String], name: &str) -> CargoResult<()> {
368368
let parent_table = self.get_table_mut(table_path)?;
369369

370-
let dep = parent_table
371-
.get_mut(name)
372-
.filter(|t| !t.is_none())
373-
.ok_or_else(|| non_existent_dependency_err(name, table_path.join(".")))?;
374-
375-
// remove the dependency
376-
*dep = toml_edit::Item::None;
370+
match parent_table.get_mut(name).filter(|t| !t.is_none()) {
371+
Some(dep) => {
372+
// remove the dependency
373+
*dep = toml_edit::Item::None;
374+
375+
// remove table if empty
376+
if parent_table.as_table_like().unwrap().is_empty() {
377+
*parent_table = toml_edit::Item::None;
378+
}
379+
}
380+
None => {
381+
// Search in other tables.
382+
let sections = self.get_sections();
383+
let found_table_path = sections.iter().find_map(|(t, i)| {
384+
let table_path: Vec<String> =
385+
t.to_table().iter().map(|s| s.to_string()).collect();
386+
i.get(name).is_some().then(|| table_path.join("."))
387+
});
377388

378-
// remove table if empty
379-
if parent_table.as_table_like().unwrap().is_empty() {
380-
*parent_table = toml_edit::Item::None;
389+
return Err(non_existent_dependency_err(
390+
name,
391+
table_path.join("."),
392+
found_table_path,
393+
));
394+
}
381395
}
382396

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

538552
fn non_existent_dependency_err(
539553
name: impl std::fmt::Display,
540-
table: impl std::fmt::Display,
554+
search_table: impl std::fmt::Display,
555+
found_table: Option<impl std::fmt::Display>,
541556
) -> anyhow::Error {
542-
anyhow::format_err!("the dependency `{name}` could not be found in `{table}`.")
557+
let mut msg = format!("the dependency `{name}` could not be found in `{search_table}`");
558+
if let Some(found_table) = found_table {
559+
msg.push_str(&format!("; it is present in `{found_table}`",));
560+
}
561+
anyhow::format_err!(msg)
543562
}
544563

545564
fn remove_array_index(array: &mut toml_edit::Array, index: usize) {
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Removing invalid_dependency_name from dependencies
2-
error: the dependency `invalid_dependency_name` could not be found in `dependencies`.
2+
error: the dependency `invalid_dependency_name` could not be found in `dependencies`
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Removing docopt from build-dependencies
2-
error: the dependency `docopt` could not be found in `build-dependencies`.
2+
error: the dependency `docopt` could not be found in `build-dependencies`; it is present in `dependencies`
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Removing semver from dev-dependencies
2-
error: the dependency `semver` could not be found in `dev-dependencies`.
2+
error: the dependency `semver` could not be found in `dev-dependencies`; it is present in `dependencies`
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Removing dbus from dependencies for target `powerpc-unknown-linux-gnu`
2-
error: the dependency `dbus` could not be found in `target.powerpc-unknown-linux-gnu.dependencies`.
2+
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`
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Removing toml from dependencies for target `x86_64-unknown-linux-gnu`
2-
error: the dependency `toml` could not be found in `target.x86_64-unknown-linux-gnu.dependencies`.
2+
error: the dependency `toml` could not be found in `target.x86_64-unknown-linux-gnu.dependencies`; it is present in `dependencies`

0 commit comments

Comments
 (0)