-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit warning on env variable case mismatch #9169
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Eh2406 (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
r? @ehuss |
@@ -167,6 +167,8 @@ pub struct Config { | |||
target_dir: Option<Filesystem>, | |||
/// Environment variables, separated to assist testing. | |||
env: HashMap<String, String>, | |||
/// Environment variables, converted to uppercase to check for case mismatch | |||
upper_case_env: HashMap<String, String>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using a HashSet
should be sufficient here, since it doesn't need the values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention behind using a HashMap instead of a HashSet was to use the value for the warning output.
Taking key.as_env_key() will result in the 'correct' key format,
whereas the value in the HashMap will be the actual user defined key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry, I missed that!
src/cargo/util/config/mod.rs
Outdated
let mut upper_case_env: HashMap<String, String> = HashMap::new(); | ||
|
||
if !cfg!(windows) { | ||
upper_case_env = env | ||
.clone() | ||
.into_iter() | ||
.map(|(k, _)| (k.to_uppercase().replace("-", "_"), k)) | ||
.collect(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a minor stylistic choice, but I think it is a little clearer to initialize only once, something like this:
let upper_case_env = if cfg!(windows) {
HashMap::new()
} else {
env.clone()
.into_iter()
.map(|(k, _)| (k.to_uppercase().replace("-", "_"), k))
.collect()
};
src/cargo/util/config/mod.rs
Outdated
match self.upper_case_env.get(key.as_env_key()) { | ||
Some(env_key) => { | ||
let _ = self.shell().warn(format!( | ||
"Variables in environment require uppercase, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a backslash to continue on the next line.
"Variables in environment require uppercase, | |
"Variables in environment require uppercase, \ |
src/cargo/util/config/mod.rs
Outdated
if cfg!(windows) { | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be good to have a comment here explaining why this is ignored on windows (for those who may not be familiar that windows automatically ASCII upper-cases keys).
(Hopefully nobody uses lower-case non-ASCII characters 😄.)
src/cargo/util/config/mod.rs
Outdated
Some(env_key) => { | ||
let _ = self.shell().warn(format!( | ||
"Variables in environment require uppercase, | ||
but given variable: {}, contains lowercase or dash.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically Cargo's error message style uses backticks to wrap a literal value.
but given variable: {}, contains lowercase or dash.", | |
but the variable `{}` contains lowercase letters or dash.", |
src/cargo/util/config/mod.rs
Outdated
match self.upper_case_env.get(key.as_env_key()) { | ||
Some(env_key) => { | ||
let _ = self.shell().warn(format!( | ||
"Variables in environment require uppercase, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a stylistic suggestion:
"Variables in environment require uppercase, | |
"Environment variables require uppercase, |
tests/testsuite/tool_paths.rs
Outdated
@@ -353,6 +353,29 @@ fn custom_linker_env() { | |||
.run(); | |||
} | |||
|
|||
#[cargo_test] | |||
#[cfg(not(windows))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to add a test to ensure that windows doesn't emit the warning, and works with a lowercase value?
src/cargo/util/config/mod.rs
Outdated
match self.upper_case_env.get(key.as_env_key()) { | ||
Some(env_key) => { | ||
let _ = self.shell().warn(format!( | ||
"Environment variables require uppercase letters, \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the language can be improved here: "Environment variables are expected to use uppercase letters and underscores, the variable {}
will be ignored and have no effect".
Thanks! I pushed a small change to combine the two tests into one. @bors r+ |
📌 Commit 0564466 has been approved by |
☀️ Test successful - checks-actions |
Update cargo 8 commits in ab64d1393b5b77c66b6534ef5023a1b89ee7bf64..bf5a5d5e5d3ae842a63bfce6d070dfd438cf6070 2021-02-10 00:19:10 +0000 to 2021-02-18 15:49:14 +0000 - Propagate `lto=off` harder (rust-lang/cargo#9182) - refactor: make deref intentions more straightforward (rust-lang/cargo#9183) - Update link for no_std attribute. (rust-lang/cargo#9174) - Remove mention of --message-format taking multiple values (rust-lang/cargo#9173) - Emit warning on env variable case mismatch (rust-lang/cargo#9169) - Implement Rustdoc versioning checks (rust-lang/cargo#8640) - Bump to 0.53.0, update changelog (rust-lang/cargo#9168) - Prevent testsuite from loading config out of sandbox. (rust-lang/cargo#9164)
When running a command like
cargo --target TRIPPLE
cargo expects to find the environment variable CARGO_TARGET_[TRIPPLE]_* with uppercase and underscores. This check emits a warning if the checked environment variable has a mismatching case and/or contains dashes rather than underscores. The warning contains the given env variable as well as an explanation for the cause of the warning.The check is skipped on windows as environment variables are treated as case insensitive on the platform.
Fixes #8285