Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better error message when it can not find the search section #12865

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions src/cargo/util/toml_mut/manifest.rs
Original file line number Diff line number Diff line change
@@ -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(())
@@ -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) {
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`