Skip to content

Commit 11c82a8

Browse files
author
Nick Flueckiger
committed
Implement uppercase env map for key case mismatch
1 parent ecfabe6 commit 11c82a8

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/cargo/util/config/mod.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ pub struct Config {
167167
target_dir: Option<Filesystem>,
168168
/// Environment variables, separated to assist testing.
169169
env: HashMap<String, String>,
170+
/// Environment variables, converted to uppercase to check for case mismatch
171+
upper_case_env: HashMap<String, String>,
170172
/// Tracks which sources have been updated to avoid multiple updates.
171173
updated_sources: LazyCell<RefCell<HashSet<SourceId>>>,
172174
/// Lock, if held, of the global package cache along with the number of
@@ -211,6 +213,14 @@ impl Config {
211213
})
212214
.collect();
213215

216+
let mut upper_case_env : HashMap<String, String> = HashMap::new();
217+
218+
if !cfg!(windows) {
219+
upper_case_env = env.clone().into_iter().map(|(k, _)| {
220+
(k.to_uppercase().replace("-", "_"), k)
221+
}).collect();
222+
}
223+
214224
let cache_rustc_info = match env.get("CARGO_CACHE_RUSTC_INFO") {
215225
Some(cache) => cache != "0",
216226
_ => true,
@@ -244,6 +254,7 @@ impl Config {
244254
creation_time: Instant::now(),
245255
target_dir: None,
246256
env,
257+
upper_case_env,
247258
updated_sources: LazyCell::new(),
248259
package_cache_lock: RefCell::new(None),
249260
http_config: LazyCell::new(),
@@ -525,7 +536,10 @@ impl Config {
525536
definition,
526537
}))
527538
}
528-
None => Ok(None),
539+
None => {
540+
self.check_environment_key_case_mismatch(key);
541+
Ok(None)
542+
},
529543
}
530544
}
531545

@@ -545,9 +559,26 @@ impl Config {
545559
return true;
546560
}
547561
}
562+
self.check_environment_key_case_mismatch(key);
563+
548564
false
549565
}
550566

567+
fn check_environment_key_case_mismatch(&self, key: &ConfigKey) {
568+
if cfg!(windows) {
569+
return;
570+
}
571+
match self.upper_case_env.get(key.as_env_key()){
572+
Some(env_key) => {
573+
let _ = self.shell().warn(
574+
format!("Variables in environment require uppercase,
575+
but given variable: {}, contains lowercase or dash.", env_key)
576+
);
577+
},
578+
None => {},
579+
}
580+
}
581+
551582
/// Get a string config value.
552583
///
553584
/// See `get` for more details.
@@ -640,7 +671,10 @@ impl Config {
640671
) -> CargoResult<()> {
641672
let env_val = match self.env.get(key.as_env_key()) {
642673
Some(v) => v,
643-
None => return Ok(()),
674+
None => {
675+
self.check_environment_key_case_mismatch(key);
676+
return Ok(())
677+
},
644678
};
645679

646680
let def = Definition::Environment(key.as_env_key().to_string());

tests/testsuite/tool_paths.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@ fn custom_linker_env() {
353353
.run();
354354
}
355355

356+
357+
#[cargo_test]
358+
fn target_in_environment_contains_lower_case() {
359+
let p = project().file("src/main.rs", "fn main() {}").build();
360+
361+
let target_keys = ["CARGO_TARGET_X86_64_UNKNOWN_LINUX_musl_LINKER",
362+
"CARGO_TARGET_x86_64_unknown_linux_musl_LINKER"];
363+
364+
for target_key in &target_keys {
365+
p.cargo("build -v --target X86_64_unknown_linux_musl")
366+
.env(target_key, "nonexistent-linker")
367+
.with_status(101)
368+
.with_stderr_contains(
369+
format!("warning: Variables in environment require uppercase,
370+
but given variable: {}, contains lowercase or dash.", target_key)
371+
)
372+
.run();
373+
}
374+
}
375+
356376
#[cargo_test]
357377
fn cfg_ignored_fields() {
358378
// Test for some ignored fields in [target.'cfg()'] tables.

0 commit comments

Comments
 (0)