Skip to content

Commit f5ea929

Browse files
authored
Merge pull request #19551 from github/redsun82/rust-all-features-by-default
Rust: use all features by default
2 parents 437246f + 55791a6 commit f5ea929

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

rust/codeql-extractor.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ options:
4545
cargo_features:
4646
title: Cargo features to turn on
4747
description: >
48-
Comma-separated list of features to turn on. If any value is `*` all features
49-
are turned on. By default only default cargo features are enabled. Can be
50-
repeated.
48+
Comma-separated list of features to turn on. By default all features are enabled.
49+
If any features are specified, then only those features are enabled. The `default`
50+
feature must be explicitly specified if only default features are desired.
51+
Can be repeated.
5152
type: array
5253
cargo_cfg_overrides:
5354
title: Cargo cfg overrides

rust/extractor/src/config.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ impl Config {
129129
}
130130
}
131131

132+
fn cargo_features(&self) -> CargoFeatures {
133+
// '*' is to be considered deprecated but still kept in for backward compatibility
134+
if self.cargo_features.is_empty() || self.cargo_features.iter().any(|f| f == "*") {
135+
CargoFeatures::All
136+
} else {
137+
CargoFeatures::Selected {
138+
features: self
139+
.cargo_features
140+
.iter()
141+
.filter(|f| *f != "default")
142+
.cloned()
143+
.collect(),
144+
no_default_features: !self.cargo_features.iter().any(|f| f == "default"),
145+
}
146+
}
147+
}
148+
132149
pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) {
133150
let sysroot = self.sysroot(dir);
134151
(
@@ -159,16 +176,7 @@ impl Config {
159176
.unwrap_or_else(|| self.scratch_dir.join("target")),
160177
)
161178
.ok(),
162-
features: if self.cargo_features.is_empty() {
163-
Default::default()
164-
} else if self.cargo_features.contains(&"*".to_string()) {
165-
CargoFeatures::All
166-
} else {
167-
CargoFeatures::Selected {
168-
features: self.cargo_features.clone(),
169-
no_default_features: false,
170-
}
171-
},
179+
features: self.cargo_features(),
172180
target: self.cargo_target.clone(),
173181
cfg_overrides: to_cfg_overrides(&self.cargo_cfg_overrides),
174182
wrap_rustc_in_build_scripts: false,
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
@pytest.mark.ql_test(expected=".all.expected")
34
def test_default(codeql, rust):
45
codeql.database.create()
56

@@ -8,10 +9,33 @@ def test_default(codeql, rust):
89
pytest.param(p,
910
marks=pytest.mark.ql_test(expected=f".{e}.expected"))
1011
for p, e in (
12+
("default", "none"),
1113
("foo", "foo"),
1214
("bar", "bar"),
1315
("*", "all"),
14-
("foo,bar", "all"))
16+
("foo,bar", "all"),
17+
("default,foo", "foo"),
18+
("default,bar", "bar"),
19+
)
1520
])
1621
def test_features(codeql, rust, features):
1722
codeql.database.create(extractor_option=f"cargo_features={features}")
23+
24+
@pytest.mark.parametrize("features",
25+
[
26+
pytest.param(p,
27+
marks=pytest.mark.ql_test(expected=f".{e}.expected"))
28+
for p, e in (
29+
("default", "foo"),
30+
("foo", "foo"),
31+
("bar", "bar"),
32+
("*", "all"),
33+
("foo,bar", "all"),
34+
("default,foo", "foo"),
35+
("default,bar", "all"),
36+
)
37+
])
38+
def test_features_with_default(codeql, rust, features):
39+
with open("Cargo.toml", "a") as f:
40+
print('default = ["foo"]', file=f)
41+
codeql.database.create(extractor_option=f"cargo_features={features}")

0 commit comments

Comments
 (0)