Skip to content

Commit 3f025a2

Browse files
committed
Search dep in other tables
Signed-off-by: hi-rustin <[email protected]>
1 parent 7f3fc2b commit 3f025a2

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/cargo/util/toml_mut/manifest.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,33 @@ 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, _)| {
384+
let table_path: Vec<String> =
385+
t.to_table().iter().map(|s| s.to_string()).collect();
386+
self.get_table(&table_path)
387+
.ok()
388+
.and_then(|t| t.get(name).is_some().then(|| table_path.join(".")))
389+
});
377390

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

383399
Ok(())
@@ -537,9 +553,14 @@ fn non_existent_table_err(table: impl std::fmt::Display) -> anyhow::Error {
537553

538554
fn non_existent_dependency_err(
539555
name: impl std::fmt::Display,
540-
table: impl std::fmt::Display,
556+
search_table: impl std::fmt::Display,
557+
found_table: Option<impl std::fmt::Display>,
541558
) -> anyhow::Error {
542-
anyhow::format_err!("the dependency `{name}` could not be found in `{table}`.")
559+
let mut msg = format!("the dependency `{name}` could not be found in `{search_table}`.");
560+
if let Some(found_table) = found_table {
561+
msg.push_str(&format!(" But it was found in `{found_table}`.",));
562+
}
563+
anyhow::format_err!(msg)
543564
}
544565

545566
fn remove_array_index(array: &mut toml_edit::Array, index: usize) {

0 commit comments

Comments
 (0)