Skip to content

Commit 6bce71a

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

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/cargo/util/toml_mut/manifest.rs

Lines changed: 31 additions & 12 deletions
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!(" But it was found in `{found_table}`.",));
560+
}
561+
anyhow::format_err!(msg)
543562
}
544563

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

0 commit comments

Comments
 (0)