Skip to content

Commit ab361f2

Browse files
committed
Auto merge of #14647 - elchukc:refactor_pkg_activated_features, r=weihanglo
improve error reporting when feature not found in `activated_features` Pulls the error message refactor out of #14593 (originally #13207) to improve error reporting when we fail to get the list of activated features enabled for the given package. It now fully lists the activated_features hashmap keys too. From the [original author](#13207 (comment)): > I moved `activated_features_int` into `activated_features` and `activated_features_unverified` as I was concerned of the performance cost of generating the full error when its not a fatal error and may occur many times. Old vs new error message: ```diff - activated_features for invalid package: features did not find PackageId { name: "bindep", version: "0.0.0", source: "[ROOT]/foo/bindep" } NormalOrDev + did not find features for (PackageId { name: "bindep", version: "0.0.0", source: "[ROOT]/foo/bindep" }, NormalOrDev) within activated_features: + [ + ( + PackageId { + name: "bindep", + version: "0.0.0", + source: "[ROOT]/foo/bindep", + }, + ArtifactDep( + CompileTarget { + name: "[ALT_TARGET]", + }, + ), + ), + ( + PackageId { + name: "foo", + version: "0.0.0", + source: "[ROOT]/foo", + }, + NormalOrDev, + ), + ] ``` r? weihanglo
2 parents ad074ab + 1623c41 commit ab361f2

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/cargo/core/resolver/features.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,30 @@ impl ResolvedFeatures {
319319
pkg_id: PackageId,
320320
features_for: FeaturesFor,
321321
) -> Vec<InternedString> {
322-
self.activated_features_int(pkg_id, features_for)
323-
.expect("activated_features for invalid package")
322+
if let Some(res) = self.activated_features_unverified(pkg_id, features_for) {
323+
res
324+
} else {
325+
panic!(
326+
"did not find features for ({pkg_id:?}, {features_for:?}) within activated_features:\n{:#?}",
327+
self.activated_features.keys()
328+
)
329+
}
330+
}
331+
332+
/// Variant of `activated_features` that returns `None` if this is
333+
/// not a valid pkg_id/is_build combination. Used in places which do
334+
/// not know which packages are activated (like `cargo clean`).
335+
pub fn activated_features_unverified(
336+
&self,
337+
pkg_id: PackageId,
338+
features_for: FeaturesFor,
339+
) -> Option<Vec<InternedString>> {
340+
let fk = features_for.apply_opts(&self.opts);
341+
if let Some(fs) = self.activated_features.get(&(pkg_id, fk)) {
342+
Some(fs.iter().cloned().collect())
343+
} else {
344+
None
345+
}
324346
}
325347

326348
/// Returns if the given dependency should be included.
@@ -340,30 +362,6 @@ impl ResolvedFeatures {
340362
.unwrap_or(false)
341363
}
342364

343-
/// Variant of `activated_features` that returns `None` if this is
344-
/// not a valid pkg_id/is_build combination. Used in places which do
345-
/// not know which packages are activated (like `cargo clean`).
346-
pub fn activated_features_unverified(
347-
&self,
348-
pkg_id: PackageId,
349-
features_for: FeaturesFor,
350-
) -> Option<Vec<InternedString>> {
351-
self.activated_features_int(pkg_id, features_for).ok()
352-
}
353-
354-
fn activated_features_int(
355-
&self,
356-
pkg_id: PackageId,
357-
features_for: FeaturesFor,
358-
) -> CargoResult<Vec<InternedString>> {
359-
let fk = features_for.apply_opts(&self.opts);
360-
if let Some(fs) = self.activated_features.get(&(pkg_id, fk)) {
361-
Ok(fs.iter().cloned().collect())
362-
} else {
363-
bail!("features did not find {:?} {:?}", pkg_id, fk)
364-
}
365-
}
366-
367365
/// Compares the result against the original resolver behavior.
368366
///
369367
/// Used by `cargo fix --edition` to display any differences.

tests/testsuite/artifact_dep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1573,7 +1573,7 @@ fn artifact_dep_target_specified() {
15731573
.masquerade_as_nightly_cargo(&["bindeps"])
15741574
.with_stdout_data("")
15751575
.with_stderr_data(r#"...
1576-
[..]activated_features for invalid package: features did not find PackageId { name: "bindep", version: "0.0.0", source: "[..]" } NormalOrDev[..]
1576+
[..]did not find features for (PackageId { name: "bindep", version: "0.0.0", source: "[..]" }, NormalOrDev) within activated_features:[..]
15771577
...
15781578
"#)
15791579
.with_status(101)

0 commit comments

Comments
 (0)