Rollup merge of #92835 - iwanders:issue-66450-improve-cfg-error-message, r=nagisa

Improve error message for key="value" cfg arguments.

Hi, I ran into difficulties using the `--cfg` flag syntax, first hit when googling for the error was issue https://github.com/rust-lang/rust/issues/66450. Reading that issue, it sounded like the best way to improve the experience was to improve the error message, this is low risk and doesn't introduce any additional argument parsing.

The issue mentions that it is entirely dependent on the shell, while this may be true, I think guiding the the user into the realization that the quotes may need to be escaped is helpful. The two suggested escapings both work in Bash and in the Windows command prompt.

fyi `@ehuss`
This commit is contained in:
Matthias Krüger 2022-01-21 22:03:14 +01:00 committed by GitHub
commit e38cbc78aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 3 deletions

View File

@ -124,7 +124,16 @@ macro_rules! error {
Err(errs) => errs.into_iter().for_each(|mut err| err.cancel()), Err(errs) => errs.into_iter().for_each(|mut err| err.cancel()),
} }
// If the user tried to use a key="value" flag, but is missing the quotes, provide
// a hint about how to resolve this.
if s.contains("=") && !s.contains("=\"") && !s.ends_with("\"") {
error!(concat!(
r#"expected `key` or `key="value"`, ensure escaping is appropriate"#,
r#" for your shell, try 'key="value"' or key=\"value\""#
));
} else {
error!(r#"expected `key` or `key="value"`"#); error!(r#"expected `key` or `key="value"`"#);
}
}) })
.collect::<CrateConfig>(); .collect::<CrateConfig>();
cfg.into_iter().map(|(a, b)| (a.to_string(), b.map(|b| b.to_string()))).collect() cfg.into_iter().map(|(a, b)| (a.to_string(), b.map(|b| b.to_string()))).collect()

View File

@ -1,3 +1,3 @@
// compile-flags: --cfg a(b=c) // compile-flags: --cfg a(b=c)
// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`) // error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")
fn main() {} fn main() {}

View File

@ -1,2 +1,2 @@
error: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`) error: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")

View File

@ -0,0 +1,4 @@
// Test for missing quotes around value, issue #66450.
// compile-flags: --cfg key=value
// error-pattern: invalid `--cfg` argument: `key=value` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")
fn main() {}

View File

@ -0,0 +1,2 @@
error: invalid `--cfg` argument: `key=value` (expected `key` or `key="value"`, ensure escaping is appropriate for your shell, try 'key="value"' or key=\"value\")