From 887ee6cc70af53be1ecfc48a3436dff49c2d528e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 3 Mar 2020 11:46:37 -0800 Subject: [PATCH] Fix bug with new feature resolver and required-features. --- src/cargo/core/resolver/features.rs | 4 +-- src/cargo/ops/cargo_compile.rs | 4 +-- tests/testsuite/features2.rs | 40 ++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/cargo/core/resolver/features.rs b/src/cargo/core/resolver/features.rs index 446e8b670f7..3af957fefe1 100644 --- a/src/cargo/core/resolver/features.rs +++ b/src/cargo/core/resolver/features.rs @@ -194,8 +194,8 @@ impl ResolvedFeatures { } /// Variant of `activated_features` that returns an empty Vec if this is - /// not a valid pkg_id/is_build combination. Used by `cargo clean` which - /// doesn't know the exact set. + /// 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, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 89ea39ca0c1..4749191598d 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -956,8 +956,8 @@ fn resolve_all_features( } else { FeaturesFor::NormalOrDev }; - for feature in resolved_features.activated_features(dep_id, features_for) { - features.insert(dep.name_in_toml().to_string() + "/" + &feature); + for feature in resolved_features.activated_features_unverified(dep_id, features_for) { + features.insert(format!("{}/{}", dep.name_in_toml(), feature)); } } } diff --git a/tests/testsuite/features2.rs b/tests/testsuite/features2.rs index 85866cb6646..a358ac52ce4 100644 --- a/tests/testsuite/features2.rs +++ b/tests/testsuite/features2.rs @@ -1,7 +1,7 @@ //! Tests for the new feature resolver. -use cargo_test_support::project; use cargo_test_support::registry::{Dependency, Package}; +use cargo_test_support::{basic_manifest, project}; #[cargo_test] fn inactivate_targets() { @@ -893,3 +893,41 @@ fn disabled_shared_build_dep() { .with_stdout("hello from somedep") .run(); } + +#[cargo_test] +fn required_features_inactive_dep() { + // required-features with an inactivated dep. + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [target.'cfg(whatever)'.dependencies] + bar = {path="bar"} + + [[bin]] + name = "foo" + required-features = ["feat1"] + + [features] + feat1 = [] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("bar/src/lib.rs", "") + .build(); + + p.cargo("check -Zfeatures=itarget") + .masquerade_as_nightly_cargo() + .with_stderr("[FINISHED] [..]") + .run(); + + p.cargo("check -Zfeatures=itarget --features=feat1") + .masquerade_as_nightly_cargo() + .with_stderr("[CHECKING] foo[..]\n[FINISHED] [..]") + .run(); +}