Skip to content

Commit d744ae0

Browse files
committed
More helpful error for invalid cargo-features = []
1 parent 37eeab7 commit d744ae0

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-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+
"unknown Cargo.toml feature `{feature_name}`\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/nightly/cargo/reference/unstable.html for more information."
645+
);
646+
bail!(msg)
624647
};
625648

626649
if *slot {

tests/testsuite/cargo_features.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,73 @@ 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.
179+
180+
"#]])
181+
.run();
182+
}
183+
184+
#[cargo_test]
185+
fn wrong_kind_of_feature() {
186+
let p = project()
187+
.file(
188+
"Cargo.toml",
189+
r#"
190+
cargo-features = ["build-dir"]
191+
192+
[package]
193+
name = "a"
194+
version = "0.0.1"
195+
edition = "2015"
196+
authors = []
197+
"#,
198+
)
199+
.file("src/lib.rs", "")
200+
.build();
201+
p.cargo("check")
202+
.with_status(101)
203+
.with_stderr_data(str![[r#"
204+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
205+
206+
Caused by:
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.
211+
212+
"#]])
213+
.run();
214+
}
215+
216+
#[cargo_test]
217+
fn feature_syntax() {
218+
let p = project()
219+
.file(
220+
"Cargo.toml",
221+
r#"
222+
cargo-features = ["bad_feature"]
223+
224+
[package]
225+
name = "a"
226+
version = "0.0.1"
227+
edition = "2015"
228+
authors = []
229+
"#,
230+
)
231+
.file("src/lib.rs", "")
232+
.build();
233+
p.cargo("check")
234+
.with_status(101)
235+
.with_stderr_data(str![[r#"
236+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
237+
238+
Caused by:
239+
unknown Cargo.toml feature `bad_feature`
240+
241+
Feature names must use '-' instead of '_'.
242+
See https://doc.rust-lang.org/nightly/cargo/reference/unstable.html for more information.
177243
178244
"#]])
179245
.run();

0 commit comments

Comments
 (0)