Skip to content

Commit 8ae0773

Browse files
committed
use String instead of OsString in case_insensitive_env so we can use unicode uppercase instead of ascii
1 parent 3c8633a commit 8ae0773

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/cargo/util/config/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ pub struct Config {
203203
env: HashMap<OsString, OsString>,
204204
/// Environment variables converted to uppercase to check for case mismatch
205205
/// (relevant on Windows, where environment variables are case-insensitive).
206-
case_insensitive_env: HashMap<OsString, OsString>,
206+
case_insensitive_env: HashMap<String, String>,
207207
/// Environment variables converted to uppercase and with "-" replaced by "_"
208-
/// (the format expected by Cargo). This only contains entries that were valid UTF-8.
208+
/// (the format expected by Cargo). This only contains entries where the key and variable are
209+
/// both valid UTF-8.
209210
normalized_env: HashMap<String, String>,
210211
/// Tracks which sources have been updated to avoid multiple updates.
211212
updated_sources: LazyCell<RefCell<HashSet<SourceId>>>,
@@ -268,7 +269,8 @@ impl Config {
268269

269270
let case_insensitive_env: HashMap<_, _> = env
270271
.keys()
271-
.map(|k| (k.to_ascii_uppercase(), k.to_owned()))
272+
.filter_map(|k| k.to_str())
273+
.map(|k| (k.to_uppercase(), k.to_owned()))
272274
.collect();
273275

274276
let normalized_env = env
@@ -742,9 +744,8 @@ impl Config {
742744
pub fn set_env(&mut self, env: HashMap<String, String>) {
743745
self.env = env.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
744746
self.case_insensitive_env = self
745-
.env
746-
.keys()
747-
.map(|k| (k.to_ascii_uppercase(), k.to_owned()))
747+
.env_keys()
748+
.map(|k| (k.to_uppercase(), k.to_owned()))
748749
.collect();
749750
self.normalized_env = self
750751
.env()
@@ -820,11 +821,12 @@ impl Config {
820821

821822
/// Wrapper for `self.env.get` when `key` should be case-insensitive.
822823
/// This is relevant on Windows, where environment variables are case-insensitive.
824+
/// Note that this only works on keys that are valid UTF-8.
823825
fn get_env_case_insensitive(&self, key: impl AsRef<OsStr>) -> Option<&OsString> {
824-
let upper_case_key = key.as_ref().to_ascii_uppercase();
826+
let upper_case_key = key.as_ref().to_str()?.to_uppercase();
825827
// `self.case_insensitive_env` holds pairs like `("PATH", "Path")`
826828
// or `("MY-VAR", "my-var")`.
827-
let env_key = self.case_insensitive_env.get(&upper_case_key)?;
829+
let env_key: &OsStr = self.case_insensitive_env.get(&upper_case_key)?.as_ref();
828830
self.env.get(env_key)
829831
}
830832

0 commit comments

Comments
 (0)