Skip to content

Commit 06834dc

Browse files
committed
Auto merge of #7855 - ehuss:fix-required-features-renamed, r=alexcrichton
Fix required-features using renamed dependencies. The dep_name/feat_name syntax in required-features should use the "name in toml" for dep_name, not the actual package name. Otherwise, it is impossible to actually enable the feature (because the `--features` flag expects a "name in toml").
2 parents acf88cc + cafec11 commit 06834dc

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

src/cargo/ops/cargo_compile.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,11 @@ fn resolve_all_features(
903903

904904
// Include features enabled for use by dependencies so targets can also use them with the
905905
// required-features field when deciding whether to be built or skipped.
906-
for (dep, _) in resolve_with_overrides.deps(package_id) {
907-
for feature in resolve_with_overrides.features(dep) {
908-
features.insert(dep.name().to_string() + "/" + feature);
906+
for (dep_id, deps) in resolve_with_overrides.deps(package_id) {
907+
for feature in resolve_with_overrides.features(dep_id) {
908+
for dep in deps {
909+
features.insert(dep.name_in_toml().to_string() + "/" + feature);
910+
}
909911
}
910912
}
911913

tests/testsuite/required_features.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,96 @@ available binaries: foo1, foo2",
11631163
)
11641164
.run();
11651165
}
1166+
1167+
#[cargo_test]
1168+
fn renamed_required_features() {
1169+
// Test that required-features uses renamed package feature names.
1170+
let p = project()
1171+
.file(
1172+
"Cargo.toml",
1173+
r#"
1174+
[package]
1175+
name = "foo"
1176+
version = "0.1.0"
1177+
edition = "2018"
1178+
1179+
[[bin]]
1180+
name = "x"
1181+
required-features = ["a1/f1"]
1182+
1183+
[dependencies]
1184+
a1 = {path="a1", package="a"}
1185+
a2 = {path="a2", package="a"}
1186+
"#,
1187+
)
1188+
.file(
1189+
"src/bin/x.rs",
1190+
r#"
1191+
fn main() {
1192+
a1::f();
1193+
a2::f();
1194+
}
1195+
"#,
1196+
)
1197+
.file(
1198+
"a1/Cargo.toml",
1199+
r#"
1200+
[package]
1201+
name = "a"
1202+
version = "0.1.0"
1203+
1204+
[features]
1205+
f1 = []
1206+
"#,
1207+
)
1208+
.file(
1209+
"a1/src/lib.rs",
1210+
r#"
1211+
pub fn f() {
1212+
if cfg!(feature="f1") {
1213+
println!("a1 f1");
1214+
}
1215+
}
1216+
"#,
1217+
)
1218+
.file(
1219+
"a2/Cargo.toml",
1220+
r#"
1221+
[package]
1222+
name = "a"
1223+
version = "0.2.0"
1224+
1225+
[features]
1226+
f2 = []
1227+
"#,
1228+
)
1229+
.file(
1230+
"a2/src/lib.rs",
1231+
r#"
1232+
pub fn f() {
1233+
if cfg!(feature="f2") {
1234+
println!("a2 f2");
1235+
}
1236+
}
1237+
"#,
1238+
)
1239+
.build();
1240+
1241+
p.cargo("run")
1242+
.with_status(101)
1243+
.with_stderr(
1244+
"\
1245+
[ERROR] target `x` in package `foo` requires the features: `a1/f1`
1246+
Consider enabling them by passing, e.g., `--features=\"a1/f1\"`
1247+
",
1248+
)
1249+
.run();
1250+
1251+
p.cargo("build --features a1/f1").run();
1252+
p.rename_run("x", "x_with_f1").with_stdout("a1 f1").run();
1253+
1254+
p.cargo("build --features a1/f1,a2/f2").run();
1255+
p.rename_run("x", "x_with_f1_f2")
1256+
.with_stdout("a1 f1\na2 f2")
1257+
.run();
1258+
}

0 commit comments

Comments
 (0)