Skip to content

Commit 77a13c0

Browse files
borsehuss
authored andcommitted
Auto merge of rust-lang#9275 - ehuss:features-non-member, r=alexcrichton
Fix --feature pkg/feat for V1 resolver for non-member. rust-lang#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 rust-lang#9265
1 parent b6bcaf3 commit 77a13c0

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
@@ -1076,7 +1076,8 @@ impl<'cfg> Workspace<'cfg> {
10761076
for feature in requested_features.features.iter() {
10771077
if let Some(index) = feature.find('/') {
10781078
let name = &feature[..index];
1079-
if specs.iter().any(|spec| spec.name() == name) {
1079+
let is_member = self.members().any(|member| member.name() == name);
1080+
if is_member && specs.iter().any(|spec| spec.name() == name) {
10801081
member_specific_features
10811082
.entry(name)
10821083
.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)