Skip to content

Commit

Permalink
improve error reporting when feature not found in activated_features
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored and elchukc committed Oct 5, 2024
1 parent ad074ab commit 4623a71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
52 changes: 26 additions & 26 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,32 @@ impl ResolvedFeatures {
pkg_id: PackageId,
features_for: FeaturesFor,
) -> Vec<InternedString> {
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<Vec<InternedString>> {
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.
Expand All @@ -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<Vec<InternedString>> {
self.activated_features_int(pkg_id, features_for).ok()
}

fn activated_features_int(
&self,
pkg_id: PackageId,
features_for: FeaturesFor,
) -> CargoResult<Vec<InternedString>> {
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.
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4623a71

Please sign in to comment.