From 4623a71c44ff8c76fbf30acfb592d223486eaa07 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Sun, 3 Dec 2023 10:54:12 +1100 Subject: [PATCH] improve error reporting when feature not found in activated_features --- src/cargo/core/resolver/features.rs | 52 ++++++++++++++--------------- tests/testsuite/artifact_dep.rs | 2 +- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/cargo/core/resolver/features.rs b/src/cargo/core/resolver/features.rs index b7f857224c7e..c930daaa7519 100644 --- a/src/cargo/core/resolver/features.rs +++ b/src/cargo/core/resolver/features.rs @@ -319,8 +319,32 @@ impl ResolvedFeatures { pkg_id: PackageId, features_for: FeaturesFor, ) -> Vec { - self.activated_features_int(pkg_id, features_for) - .expect("activated_features for invalid package") + let fk = features_for.apply_opts(&self.opts); + let key = (pkg_id, fk); + if let Some(fs) = self.activated_features.get(&key) { + fs.iter().cloned().collect() + } else { + panic!( + "did not find features for {key:?} within activated_features:\n{:#?}", + self.activated_features.keys() + ) + } + } + + /// Variant of `activated_features` that returns `None` if this is + /// not a valid pkg_id/is_build combination. Used in places which do + /// not know which packages are activated (like `cargo clean`). + pub fn activated_features_unverified( + &self, + pkg_id: PackageId, + features_for: FeaturesFor, + ) -> Option> { + let fk = features_for.apply_opts(&self.opts); + if let Some(fs) = self.activated_features.get(&(pkg_id, fk)) { + Some(fs.iter().cloned().collect()) + } else { + None + } } /// Returns if the given dependency should be included. @@ -340,30 +364,6 @@ impl ResolvedFeatures { .unwrap_or(false) } - /// Variant of `activated_features` that returns `None` if this is - /// not a valid pkg_id/is_build combination. Used in places which do - /// not know which packages are activated (like `cargo clean`). - pub fn activated_features_unverified( - &self, - pkg_id: PackageId, - features_for: FeaturesFor, - ) -> Option> { - self.activated_features_int(pkg_id, features_for).ok() - } - - fn activated_features_int( - &self, - pkg_id: PackageId, - features_for: FeaturesFor, - ) -> CargoResult> { - let fk = features_for.apply_opts(&self.opts); - if let Some(fs) = self.activated_features.get(&(pkg_id, fk)) { - Ok(fs.iter().cloned().collect()) - } else { - bail!("features did not find {:?} {:?}", pkg_id, fk) - } - } - /// Compares the result against the original resolver behavior. /// /// Used by `cargo fix --edition` to display any differences. diff --git a/tests/testsuite/artifact_dep.rs b/tests/testsuite/artifact_dep.rs index c4d1f9ce15d5..f0b32ebb2292 100644 --- a/tests/testsuite/artifact_dep.rs +++ b/tests/testsuite/artifact_dep.rs @@ -1573,7 +1573,7 @@ fn artifact_dep_target_specified() { .masquerade_as_nightly_cargo(&["bindeps"]) .with_stdout_data("") .with_stderr_data(r#"... -[..]activated_features for invalid package: features did not find PackageId { name: "bindep", version: "0.0.0", source: "[..]" } NormalOrDev[..] +[..]did not find features for (PackageId { name: "bindep", version: "0.0.0", source: "[..]" }, NormalOrDev) within activated_features:[..] ... "#) .with_status(101)