Skip to content

Do not ignore --features when --all-features is present #10337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/cargo/core/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ fn build_requirements<'a, 'b: 'a>(
return Err(e.into_activate_error(parent, s));
}
}
} else {
for fv in features.iter() {
if let Err(e) = reqs.require_value(fv) {
return Err(e.into_activate_error(parent, s));
}
}

for fv in features.iter() {
if let Err(e) = reqs.require_value(fv) {
return Err(e.into_activate_error(parent, s));
}
handle_default(*uses_default_features, &mut reqs)?;
}
handle_default(*uses_default_features, &mut reqs)?;
}
RequestedFeatures::DepFeatures {
features,
Expand Down
21 changes: 10 additions & 11 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,19 +680,18 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
) -> Vec<FeatureValue> {
let summary = self.resolve.summary(pkg_id);
let feature_map = summary.features();

let mut result: Vec<FeatureValue> = cli_features.features.iter().cloned().collect();
let default = InternedString::new("default");
if cli_features.uses_default_features && feature_map.contains_key(&default) {
result.push(FeatureValue::Feature(default));
}

if cli_features.all_features {
feature_map
.keys()
.map(|k| FeatureValue::Feature(*k))
.collect()
} else {
let mut result: Vec<FeatureValue> = cli_features.features.iter().cloned().collect();
let default = InternedString::new("default");
if cli_features.uses_default_features && feature_map.contains_key(&default) {
result.push(FeatureValue::Feature(default));
}
result
result.extend(feature_map.keys().map(|k| FeatureValue::Feature(*k)))
}

result
}

/// Returns the dependencies for a package, filtering out inactive targets.
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ impl<'cfg> Workspace<'cfg> {
cli_features: &CliFeatures,
found_features: &mut BTreeSet<FeatureValue>,
) -> CliFeatures {
if cli_features.features.is_empty() || cli_features.all_features {
if cli_features.features.is_empty() {
return cli_features.clone();
}

Expand Down Expand Up @@ -1185,7 +1185,7 @@ impl<'cfg> Workspace<'cfg> {
}
CliFeatures {
features: Rc::new(features),
all_features: false,
all_features: cli_features.all_features,
uses_default_features: cli_features.uses_default_features,
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/cargo/ops/tree/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,12 @@ fn add_cli_features(
let mut to_add: HashSet<FeatureValue> = HashSet::new();
if cli_features.all_features {
to_add.extend(feature_map.keys().map(|feat| FeatureValue::Feature(*feat)));
} else {
if cli_features.uses_default_features {
to_add.insert(FeatureValue::Feature(InternedString::new("default")));
}
to_add.extend(cli_features.features.iter().cloned());
};
}

if cli_features.uses_default_features {
to_add.insert(FeatureValue::Feature(InternedString::new("default")));
}
to_add.extend(cli_features.features.iter().cloned());

// Add each feature as a node, and mark as "from command-line" in graph.cli_features.
for fv in to_add {
Expand Down
74 changes: 74 additions & 0 deletions tests/testsuite/features2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2398,3 +2398,77 @@ foo v0.1.0 [..]
)
.run();
}

#[cargo_test]
fn all_features_merges_with_features() {
Package::new("dep", "0.1.0")
.feature("feat1", &[])
.file(
"src/lib.rs",
r#"
#[cfg(feature="feat1")]
pub fn work() {
println!("it works");
}
"#,
)
.publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"

[features]
a = []

[dependencies]
dep = "0.1"

[[example]]
name = "ex"
required-features = ["a", "dep/feat1"]
"#,
)
.file(
"examples/ex.rs",
r#"
fn main() {
dep::work();
}
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("run --example ex --all-features --features dep/feat1")
.with_stderr(
"\
[UPDATING] [..]
[DOWNLOADING] crates ...
[DOWNLOADED] [..]
[COMPILING] dep v0.1.0
[COMPILING] foo v0.1.0 [..]
[FINISHED] [..]
[RUNNING] `target/debug/examples/ex[EXE]`
",
)
.with_stdout("it works")
.run();

switch_to_resolver_2(&p);

p.cargo("run --example ex --all-features --features dep/feat1")
.with_stderr(
"\
[FINISHED] [..]
[RUNNING] `target/debug/examples/ex[EXE]`
",
)
.with_stdout("it works")
.run();
}
3 changes: 2 additions & 1 deletion tests/testsuite/features_namespaced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ foo v0.1.0 ([ROOT]/foo)
bar v1.0.0
└── bar feature \"default\"
└── foo v0.1.0 ([ROOT]/foo)
└── foo feature \"a\" (command-line)
├── foo feature \"a\" (command-line)
└── foo feature \"default\" (command-line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I don't think that this is a bug with this PR per-se I think that this is a bug because neither of the packages here actually have a feature called default, so I don't think that this should be printed. I think this could probably be fixed by conditionally inserting the feature "default" in cargo tree depending on whether the package actually has that feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I open an issue about this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yeah I think it's probably orthogonal-enough from this PR it's best to fix later, and yeah if you wouldn't mind opening an issue that'd be great!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #10351

",
)
.run();
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/tree_graph_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ foo v0.1.0 ([..]/foo)
opt2 v1.0.0
└── opt2 feature \"default\"
└── foo v0.1.0 ([..]/foo)
├── foo feature \"default\" (command-line)
├── foo feature \"f1\" (command-line)
│ └── foo feature \"f2\" (command-line)
├── foo feature \"f2\" (command-line)
Expand Down