Skip to content

Commit 841179e

Browse files
committed
Auto merge of #9275 - ehuss:features-non-member, r=alexcrichton
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
2 parents 8b08998 + 6b320cb commit 841179e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/cargo/core/workspace.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,8 @@ impl<'cfg> Workspace<'cfg> {
11821182
for feature in requested_features.features.iter() {
11831183
if let Some(index) = feature.find('/') {
11841184
let name = &feature[..index];
1185-
if specs.iter().any(|spec| spec.name() == name) {
1185+
let is_member = self.members().any(|member| member.name() == name);
1186+
if is_member && specs.iter().any(|spec| spec.name() == name) {
11861187
member_specific_features
11871188
.entry(name)
11881189
.or_default()

tests/testsuite/package_features.rs

+31
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,34 @@ fn resolver1_member_features() {
458458
.with_stdout("m1-feature set")
459459
.run();
460460
}
461+
462+
#[cargo_test]
463+
fn resolver1_non_member_optional_feature() {
464+
// --features x/y for an optional dependency `x` with the v1 resolver.
465+
Package::new("bar", "1.0.0")
466+
.feature("feat1", &[])
467+
.file(
468+
"src/lib.rs",
469+
r#"
470+
#[cfg(not(feature = "feat1"))]
471+
compile_error!("feat1 should be activated");
472+
"#,
473+
)
474+
.publish();
475+
let p = project()
476+
.file(
477+
"Cargo.toml",
478+
r#"
479+
[package]
480+
name = "foo"
481+
version = "0.1.0"
482+
483+
[dependencies]
484+
bar = { version="1.0", optional=true }
485+
"#,
486+
)
487+
.file("src/lib.rs", "")
488+
.build();
489+
490+
p.cargo("check -p bar --features bar/feat1").run();
491+
}

0 commit comments

Comments
 (0)