Skip to content

Commit

Permalink
Auto merge of #7507 - ehuss:virtual-ws-features, r=alexcrichton
Browse files Browse the repository at this point in the history
Reject feature flags in a virtual workspace.

This generates an error if feature flags are used in the root of a virtual workspace. Previously these flags were completely ignored.  In the interest of avoiding confusion, I think it would be good to be explicit that these don't currently work.  This could alternatively be a warning, but I think it is better to reject it outright.

cc #4753, #3620, #5015, #6195, etc.
  • Loading branch information
bors committed Oct 15, 2019
2 parents 1e764db + bf474ba commit c25db6c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ pub trait ArgMatchesExt {
if config.cli_unstable().avoid_dev_deps {
ws.set_require_optional_deps(false);
}
if ws.is_virtual() && !config.cli_unstable().package_features {
for flag in &["features", "all-features", "no-default-features"] {
if self._is_present(flag) {
bail!(
"--{} is not allowed in the root of a virtual workspace",
flag
);
}
}
}
Ok(ws)
}

Expand Down
69 changes: 69 additions & 0 deletions tests/testsuite/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,3 +1988,72 @@ fn cli_parse_ok() {

p.cargo("run --features a b").run();
}

#[cargo_test]
fn virtual_ws_flags() {
// Reject features flags in the root of a virtual workspace.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["a"]
"#,
)
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
[features]
f1 = []
"#,
)
.file("a/src/lib.rs", "")
.build();

p.cargo("build --features=f1")
.with_stderr("[ERROR] --features is not allowed in the root of a virtual workspace")
.with_status(101)
.run();

p.cargo("build --no-default-features")
.with_stderr(
"[ERROR] --no-default-features is not allowed in the root of a virtual workspace",
)
.with_status(101)
.run();

p.cargo("build --all-features")
.with_stderr("[ERROR] --all-features is not allowed in the root of a virtual workspace")
.with_status(101)
.run();

// It's OK if cwd is in a member.
p.cargo("check --features=f1 -v")
.cwd("a")
.with_stderr(
"\
[CHECKING] a [..]
[RUNNING] `rustc --crate-name a a/src/lib.rs [..]--cfg [..]feature[..]f1[..]
[FINISHED] dev [..]
",
)
.run();

p.cargo("clean").run();

// And -Zpackage-features is OK because it is designed to support this.
p.cargo("check --features=f1 -p a -Z package-features -v")
.masquerade_as_nightly_cargo()
.with_stderr(
"\
[CHECKING] a [..]
[RUNNING] `rustc --crate-name a a/src/lib.rs [..]--cfg [..]feature[..]f1[..]
[FINISHED] dev [..]
",
)
.run();
}

0 comments on commit c25db6c

Please sign in to comment.