Skip to content

Commit adead0c

Browse files
committed
More helpful error for invalid cargo-features = []
1 parent c629f5b commit adead0c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/cargo/core/features.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,30 @@ 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!(
624+
"feature `{feature_name}` is not a valid unstable feature for Cargo.toml\n\n"
625+
);
626+
627+
if feature_name.contains('_') {
628+
let _ = writeln!(msg, "Feature names must use '-' instead of '_'.");
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+
let _ = writeln!(
643+
msg,
644+
"See https://doc.rust-lang.org/cargo/reference/unstable.html for more information."
645+
);
646+
bail!(msg)
624647
};
625648

626649
if *slot {

tests/testsuite/cargo_features.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +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+
feature `foo` is not a valid unstable feature for Cargo.toml
177177
178+
See https://doc.rust-lang.org/cargo/reference/unstable.html for more information.
178179
"#]])
179180
.run();
180181
}

0 commit comments

Comments
 (0)