]> git.proxmox.com Git - cargo.git/commitdiff
Auto merge of #9275 - ehuss:features-non-member, r=alexcrichton
authorbors <bors@rust-lang.org>
Tue, 16 Mar 2021 20:55:20 +0000 (20:55 +0000)
committerEric Huss <eric@huss.org>
Tue, 16 Mar 2021 21:55:38 +0000 (14:55 -0700)
Fix --feature pkg/feat for V1 resolver for non-member.

#8997 had an unintended regression where `-p foo --feature foo/feat` syntax where `foo` is an **optional non-member** fails with an error that `foo` did not match any packages.  The issue is that the member/feature selection routine needed to slot this into the features for the package in the current working directory (it was incorrectly treating `foo` as a workspace member).

V2 outright does not allow specifying features for non-workspace members.

Fixes #9265

src/cargo/core/workspace.rs
tests/testsuite/package_features.rs

index e78814233b4fe80cef2780a8e9e3720e7fcb0447..45276d45b587de2f770ca85b38e489891a8b7b3a 100644 (file)
@@ -1076,7 +1076,8 @@ impl<'cfg> Workspace<'cfg> {
         for feature in requested_features.features.iter() {
             if let Some(index) = feature.find('/') {
                 let name = &feature[..index];
-                if specs.iter().any(|spec| spec.name() == name) {
+                let is_member = self.members().any(|member| member.name() == name);
+                if is_member && specs.iter().any(|spec| spec.name() == name) {
                     member_specific_features
                         .entry(name)
                         .or_default()
index 0893e0c5bf3bda69025a3f6b2e1949f74439f623..15ed37e5cc9cab931d1d618cae84d5dcb23eaf44 100644 (file)
@@ -458,3 +458,34 @@ fn resolver1_member_features() {
         .with_stdout("m1-feature set")
         .run();
 }
+
+#[cargo_test]
+fn resolver1_non_member_optional_feature() {
+    // --features x/y for an optional dependency `x` with the v1 resolver.
+    Package::new("bar", "1.0.0")
+        .feature("feat1", &[])
+        .file(
+            "src/lib.rs",
+            r#"
+                #[cfg(not(feature = "feat1"))]
+                compile_error!("feat1 should be activated");
+            "#,
+        )
+        .publish();
+    let p = project()
+        .file(
+            "Cargo.toml",
+            r#"
+                [package]
+                name = "foo"
+                version = "0.1.0"
+
+                [dependencies]
+                bar = { version="1.0", optional=true }
+            "#,
+        )
+        .file("src/lib.rs", "")
+        .build();
+
+    p.cargo("check -p bar --features bar/feat1").run();
+}