Skip to content
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

Bail if targets require nonexistent feature. #4874

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ pub fn targets(manifest: &TomlManifest,
targets.push(Target::custom_build_target(&name, package_root.join(custom_build)));
}

if let Some(ref features) = manifest.features {
for t in targets.iter() {
if let Some(req_features) = t.required_features() {
for req_feature in req_features {
if !features.contains_key(req_feature) {
bail!("Target `{}` requires feature `{}`, which is \
not specified in the manifest's `[features]`-section.", t.name(), req_feature);
}
}
}
}
}

Ok(targets)
}

Expand Down
30 changes: 30 additions & 0 deletions tests/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,33 @@ fn many_cli_features_comma_and_space_delimited() {
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
", dir = p.url())));
}

#[test]
fn nonexistent_feature_in_target() {
let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.0.1"
authors = []

[features]
foo = []

[[example]]
name = "bar"
required-features = ["baz"]
"#)
.file("src/main.rs", "fn main() {}")
.file("examples/bar.rs", "fn main() {}")
.build();

assert_that(p.cargo("build"),
execs()
.with_status(101)
.with_stderr("\
[ERROR] failed to parse manifest at `[..]`

Caused by:
Target `bar` requires feature `baz`[..]"));
}