More helpful error for invalid cargo-features = []

This commit is contained in:
Kornel 2025-07-28 17:05:42 +01:00
parent 5fff1e0e76
commit 45b4faa04a
2 changed files with 36 additions and 4 deletions

View File

@ -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 {

View File

@ -173,7 +173,9 @@ 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();
@ -202,7 +204,10 @@ fn wrong_kind_of_feature() {
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
unknown cargo feature `build-dir`
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();
@ -231,7 +236,9 @@ fn feature_syntax() {
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
unknown cargo feature `bad_feature`
unknown Cargo.toml feature `bad_feature`
Feature names must use '-' instead of '_'.
"#]])
.run();