Skip to content

More helpful error for invalid cargo-features = [] #15781

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

Merged
merged 2 commits into from
Aug 12, 2025
Merged
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
27 changes: 26 additions & 1 deletion src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,32 @@ impl Features {
) -> CargoResult<()> {
let nightly_features_allowed = self.nightly_features_allowed;
let Some((slot, feature)) = self.status(feature_name) else {
bail!("unknown cargo feature `{}`", feature_name)
let mut msg = format!("unknown Cargo.toml feature `{feature_name}`\n\n");
let mut append_see_docs = true;

if feature_name.contains('_') {
let _ = writeln!(msg, "Feature names must use '-' instead of '_'.");
append_see_docs = false;
} else {
let underscore_name = feature_name.replace('-', "_");
if CliUnstable::help()
.iter()
.any(|(option, _)| *option == underscore_name)
{
let _ = writeln!(
msg,
"This feature can be enabled via -Z{feature_name} or the `[unstable]` section in config.toml."
);
}
}

if append_see_docs {
let _ = writeln!(
msg,
"See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information."
);
}
bail!(msg)
};

if *slot {
Expand Down
67 changes: 66 additions & 1 deletion tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,72 @@ fn unknown_feature() {
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
unknown cargo feature `foo`
unknown Cargo.toml feature `foo`

See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information.

"#]])
.run();
}

#[cargo_test]
fn wrong_kind_of_feature() {
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["build-dir"]

[package]
name = "a"
version = "0.0.1"
edition = "2015"
authors = []
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
unknown Cargo.toml feature `build-dir`

This feature can be enabled via -Zbuild-dir or the `[unstable]` section in config.toml.
See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information.

"#]])
.run();
}

#[cargo_test]
fn feature_syntax() {
let p = project()
.file(
"Cargo.toml",
r#"
cargo-features = ["bad_feature"]

[package]
name = "a"
version = "0.0.1"
edition = "2015"
authors = []
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
unknown Cargo.toml feature `bad_feature`

Feature names must use '-' instead of '_'.

"#]])
.run();
Expand Down
Loading