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

fix(remove): On error, suggest other dependencies #14818

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/cargo/util/toml_mut/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use anyhow::Context as _;
use super::dependency::Dependency;
use crate::core::dependency::DepKind;
use crate::core::{FeatureValue, Features, Workspace};
use crate::util::closest;
use crate::util::interning::InternedString;
use crate::{CargoResult, GlobalContext};

Expand Down Expand Up @@ -381,6 +382,13 @@ impl LocalManifest {
}
}
None => {
let names = parent_table
.as_table_like()
.map(|t| t.iter())
.into_iter()
.flatten();
let alt_name = closest(name, names.map(|(k, _)| k), |k| k).map(|n| n.to_owned());

// Search in other tables.
let sections = self.get_sections();
let found_table_path = sections.iter().find_map(|(t, i)| {
Expand All @@ -393,6 +401,7 @@ impl LocalManifest {
name,
table_path.join("."),
found_table_path,
alt_name.as_deref(),
));
}
}
Expand Down Expand Up @@ -605,10 +614,13 @@ fn non_existent_dependency_err(
name: impl std::fmt::Display,
search_table: impl std::fmt::Display,
found_table: Option<impl std::fmt::Display>,
alt_name: Option<&str>,
) -> anyhow::Error {
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}`",));
} else if let Some(alt_name) = alt_name {
msg.push_str(&format!("; dependency `{alt_name}` exists",));
}
anyhow::format_err!(msg)
}
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/cargo_remove/invalid_dep/in

This file was deleted.

22 changes: 22 additions & 0 deletions tests/testsuite/cargo_remove/invalid_dep/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "cargo-remove-test-fixture"
version = "0.1.0"
edition = "2015"

[[bin]]
name = "main"
path = "src/main.rs"

[build-dependencies]
semver = "0.1.0"

[dependencies]
invalid-dependency-name = "0.6"
docopt = "0.6"
semver = "0.1"

[dev-dependencies]
serde = "1.0.90"

[features]
std = ["serde/std", "semver/std"]
1 change: 1 addition & 0 deletions tests/testsuite/cargo_remove/invalid_dep/in/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

6 changes: 2 additions & 4 deletions tests/testsuite/cargo_remove/invalid_dep/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ use cargo_test_support::Project;
#[cargo_test]
fn case() {
cargo_test_support::registry::init();
cargo_test_support::registry::Package::new("clippy", "0.4.0+my-package").publish();
cargo_test_support::registry::Package::new("invalid-dependency-name", "0.6.2+my-package")
.publish();
cargo_test_support::registry::Package::new("docopt", "0.6.2+my-package").publish();
cargo_test_support::registry::Package::new("regex", "0.1.1+my-package").publish();
cargo_test_support::registry::Package::new("rustc-serialize", "0.4.0+my-package").publish();
cargo_test_support::registry::Package::new("toml", "0.1.1+my-package").publish();
cargo_test_support::registry::Package::new("semver", "0.1.1")
.feature("std", &[])
.publish();
Expand Down
5 changes: 1 addition & 4 deletions tests/testsuite/cargo_remove/invalid_dep/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ path = "src/main.rs"
semver = "0.1.0"

[dependencies]
invalid-dependency-name = "0.6"
docopt = "0.6"
rustc-serialize = "0.4"
semver = "0.1"
toml = "0.1"
clippy = "0.4"

[dev-dependencies]
regex = "0.1.1"
serde = "1.0.90"

[features]
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/cargo_remove/invalid_dep/stderr.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.