Skip to content

Commit

Permalink
Auto merge of rust-lang#7855 - ehuss:fix-required-features-renamed, r…
Browse files Browse the repository at this point in the history
…=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").
  • Loading branch information
bors committed Feb 5, 2020
2 parents acf88cc + cafec11 commit 06834dc
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,11 @@ fn resolve_all_features(

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

Expand Down
93 changes: 93 additions & 0 deletions tests/testsuite/required_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1163,3 +1163,96 @@ available binaries: foo1, foo2",
)
.run();
}

#[cargo_test]
fn renamed_required_features() {
// Test that required-features uses renamed package feature names.
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2018"
[[bin]]
name = "x"
required-features = ["a1/f1"]
[dependencies]
a1 = {path="a1", package="a"}
a2 = {path="a2", package="a"}
"#,
)
.file(
"src/bin/x.rs",
r#"
fn main() {
a1::f();
a2::f();
}
"#,
)
.file(
"a1/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
[features]
f1 = []
"#,
)
.file(
"a1/src/lib.rs",
r#"
pub fn f() {
if cfg!(feature="f1") {
println!("a1 f1");
}
}
"#,
)
.file(
"a2/Cargo.toml",
r#"
[package]
name = "a"
version = "0.2.0"
[features]
f2 = []
"#,
)
.file(
"a2/src/lib.rs",
r#"
pub fn f() {
if cfg!(feature="f2") {
println!("a2 f2");
}
}
"#,
)
.build();

p.cargo("run")
.with_status(101)
.with_stderr(
"\
[ERROR] target `x` in package `foo` requires the features: `a1/f1`
Consider enabling them by passing, e.g., `--features=\"a1/f1\"`
",
)
.run();

p.cargo("build --features a1/f1").run();
p.rename_run("x", "x_with_f1").with_stdout("a1 f1").run();

p.cargo("build --features a1/f1,a2/f2").run();
p.rename_run("x", "x_with_f1_f2")
.with_stdout("a1 f1\na2 f2")
.run();
}

0 comments on commit 06834dc

Please sign in to comment.