Skip to content
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

Merged
merged 10 commits into from Feb 14, 2021
Merged

Emit warning on env variable case mismatch #9169

merged 10 commits into from Feb 14, 2021

Conversation

ghost
Copy link

@ghost ghost commented Feb 13, 2021

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

@rust-highfive
Copy link

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 13, 2021
@ehuss
Copy link
Contributor

ehuss commented Feb 13, 2021

r? @ehuss

@rust-highfive rust-highfive assigned ehuss and unassigned Eh2406 Feb 13, 2021
@@ -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>,
Copy link
Contributor

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.

Copy link
Author

@ghost ghost Feb 13, 2021

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.

Copy link
Contributor

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!

Comment on lines 216 to 224
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();
}
Copy link
Contributor

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()
        };

match self.upper_case_env.get(key.as_env_key()) {
Some(env_key) => {
let _ = self.shell().warn(format!(
"Variables in environment require uppercase,
Copy link
Contributor

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.

Suggested change
"Variables in environment require uppercase,
"Variables in environment require uppercase, \

Comment on lines 570 to 571
if cfg!(windows) {
return;
Copy link
Contributor

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 😄.)

Some(env_key) => {
let _ = self.shell().warn(format!(
"Variables in environment require uppercase,
but given variable: {}, contains lowercase or dash.",
Copy link
Contributor

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.

Suggested change
but given variable: {}, contains lowercase or dash.",
but the variable `{}` contains lowercase letters or dash.",

match self.upper_case_env.get(key.as_env_key()) {
Some(env_key) => {
let _ = self.shell().warn(format!(
"Variables in environment require uppercase,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a stylistic suggestion:

Suggested change
"Variables in environment require uppercase,
"Environment variables require uppercase,

@@ -353,6 +353,29 @@ fn custom_linker_env() {
.run();
}

#[cargo_test]
#[cfg(not(windows))]
Copy link
Contributor

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?

match self.upper_case_env.get(key.as_env_key()) {
Some(env_key) => {
let _ = self.shell().warn(format!(
"Environment variables require uppercase letters, \
Copy link
Contributor

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".

@ehuss
Copy link
Contributor

ehuss commented Feb 14, 2021

Thanks! I pushed a small change to combine the two tests into one.

@bors r+

@bors
Copy link
Contributor

bors commented Feb 14, 2021

📌 Commit 0564466 has been approved by ehuss

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 14, 2021
@bors
Copy link
Contributor

bors commented Feb 14, 2021

⌛ Testing commit 0564466 with merge af564b2...

@bors
Copy link
Contributor

bors commented Feb 14, 2021

☀️ Test successful - checks-actions
Approved by: ehuss
Pushing af564b2 to master...

@bors bors merged commit af564b2 into rust-lang:master Feb 14, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 19, 2021
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)
@ehuss ehuss added this to the 1.52.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CARGO_TARGET_<triple>_LINKER environment variable is ignored during build
5 participants