-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Emit warning on env variable case mismatch #9169
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
Changes from 4 commits
11c82a8
a7ae635
dd4b95f
6ea8abc
6af3194
c863102
86c6e42
d3aed35
4234077
0564466
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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>, | ||||||||||
| /// Tracks which sources have been updated to avoid multiple updates. | ||||||||||
| updated_sources: LazyCell<RefCell<HashSet<SourceId>>>, | ||||||||||
| /// Lock, if held, of the global package cache along with the number of | ||||||||||
|
|
@@ -211,6 +213,16 @@ impl Config { | |||||||||
| }) | ||||||||||
| .collect(); | ||||||||||
|
|
||||||||||
| 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(); | ||||||||||
| } | ||||||||||
|
||||||||||
|
|
||||||||||
| let cache_rustc_info = match env.get("CARGO_CACHE_RUSTC_INFO") { | ||||||||||
| Some(cache) => cache != "0", | ||||||||||
| _ => true, | ||||||||||
|
|
@@ -244,6 +256,7 @@ impl Config { | |||||||||
| creation_time: Instant::now(), | ||||||||||
| target_dir: None, | ||||||||||
| env, | ||||||||||
| upper_case_env, | ||||||||||
| updated_sources: LazyCell::new(), | ||||||||||
| package_cache_lock: RefCell::new(None), | ||||||||||
| http_config: LazyCell::new(), | ||||||||||
|
|
@@ -525,7 +538,10 @@ impl Config { | |||||||||
| definition, | ||||||||||
| })) | ||||||||||
| } | ||||||||||
| None => Ok(None), | ||||||||||
| None => { | ||||||||||
| self.check_environment_key_case_mismatch(key); | ||||||||||
| Ok(None) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -545,9 +561,27 @@ impl Config { | |||||||||
| return true; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| self.check_environment_key_case_mismatch(key); | ||||||||||
|
|
||||||||||
| false | ||||||||||
| } | ||||||||||
|
|
||||||||||
| fn check_environment_key_case_mismatch(&self, key: &ConfigKey) { | ||||||||||
| if cfg!(windows) { | ||||||||||
| return; | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😄.) |
||||||||||
| } | ||||||||||
| match self.upper_case_env.get(key.as_env_key()) { | ||||||||||
| Some(env_key) => { | ||||||||||
| let _ = self.shell().warn(format!( | ||||||||||
| "Variables in environment require uppercase, | ||||||||||
|
||||||||||
| "Variables in environment require uppercase, | |
| "Variables in environment require uppercase, \ |
Outdated
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, |
Outdated
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.", |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -353,6 +353,29 @@ fn custom_linker_env() { | |
| .run(); | ||
| } | ||
|
|
||
| #[cargo_test] | ||
| #[cfg(not(windows))] | ||
|
||
| fn target_in_environment_contains_lower_case() { | ||
| let p = project().file("src/main.rs", "fn main() {}").build(); | ||
|
|
||
| let target_keys = [ | ||
| "CARGO_TARGET_X86_64_UNKNOWN_LINUX_musl_LINKER", | ||
| "CARGO_TARGET_x86_64_unknown_linux_musl_LINKER", | ||
| ]; | ||
|
|
||
| for target_key in &target_keys { | ||
| p.cargo("build -v --target x86_64-unknown-linux-musl") | ||
| .env(target_key, "nonexistent-linker") | ||
| .with_status(101) | ||
| .with_stderr_contains(format!( | ||
| "warning: Variables in environment require uppercase, | ||
| but given variable: {}, contains lowercase or dash.", | ||
| target_key | ||
| )) | ||
| .run(); | ||
| } | ||
| } | ||
|
|
||
| #[cargo_test] | ||
| fn cfg_ignored_fields() { | ||
| // Test for some ignored fields in [target.'cfg()'] tables. | ||
|
|
||
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
HashSetshould be sufficient here, since it doesn't need the values.Uh oh!
There was an error while loading. Please reload this page.
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!