Skip to content

Commit 45b4faa

Browse files
committed
More helpful error for invalid cargo-features = []
1 parent 5fff1e0 commit 45b4faa

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/cargo/core/features.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,32 @@ impl Features {
620620
) -> CargoResult<()> {
621621
let nightly_features_allowed = self.nightly_features_allowed;
622622
let Some((slot, feature)) = self.status(feature_name) else {
623-
bail!("unknown cargo feature `{}`", feature_name)
623+
let mut msg = format!("unknown Cargo.toml feature `{feature_name}`\n\n");
624+
let mut append_see_docs = true;
625+
626+
if feature_name.contains('_') {
627+
let _ = writeln!(msg, "Feature names must use '-' instead of '_'.");
628+
append_see_docs = false;
629+
} else {
630+
let underscore_name = feature_name.replace('-', "_");
631+
if CliUnstable::help()
632+
.iter()
633+
.any(|(option, _)| *option == underscore_name)
634+
{
635+
let _ = writeln!(
636+
msg,
637+
"This feature can be enabled via -Z{feature_name} or the `[unstable]` section in config.toml."
638+
);
639+
}
640+
}
641+
642+
if append_see_docs {
643+
let _ = writeln!(
644+
msg,
645+
"See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information."
646+
);
647+
}
648+
bail!(msg)
624649
};
625650

626651
if *slot {

tests/testsuite/cargo_features.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ fn unknown_feature() {
173173
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
174174
175175
Caused by:
176-
unknown cargo feature `foo`
176+
unknown Cargo.toml feature `foo`
177+
178+
See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information.
177179
178180
"#]])
179181
.run();
@@ -202,7 +204,10 @@ fn wrong_kind_of_feature() {
202204
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
203205
204206
Caused by:
205-
unknown cargo feature `build-dir`
207+
unknown Cargo.toml feature `build-dir`
208+
209+
This feature can be enabled via -Zbuild-dir or the `[unstable]` section in config.toml.
210+
See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information.
206211
207212
"#]])
208213
.run();
@@ -231,7 +236,9 @@ fn feature_syntax() {
231236
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
232237
233238
Caused by:
234-
unknown cargo feature `bad_feature`
239+
unknown Cargo.toml feature `bad_feature`
240+
241+
Feature names must use '-' instead of '_'.
235242
236243
"#]])
237244
.run();

0 commit comments

Comments
 (0)