-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Handle case mismatches when looking up env vars in the Config snapshot #11824
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8353396
add unit test for config and integration test with cargo --list
kylematsuda 0255c30
store uppercased env var names; look for uppercased version on Window…
kylematsuda 3c8633a
change set_env to update case_insensitive_env and normalized_env (nee…
kylematsuda 8ae0773
use String instead of OsString in case_insensitive_env so we can use …
kylematsuda 35b53b8
add free function to generate auxiliary envs
kylematsuda f810234
factor out three hashmaps in Config into a new struct for better enca…
kylematsuda 7af55d8
improve docstrings for config::environment
kylematsuda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
//! Encapsulates snapshotting of environment variables. | ||
|
||
use std::collections::HashMap; | ||
use std::ffi::{OsStr, OsString}; | ||
|
||
use crate::util::errors::CargoResult; | ||
use anyhow::{anyhow, bail}; | ||
|
||
/// Generate `case_insensitive_env` and `normalized_env` from the `env`. | ||
fn make_case_insensitive_and_normalized_env( | ||
env: &HashMap<OsString, OsString>, | ||
) -> (HashMap<String, String>, HashMap<String, String>) { | ||
let case_insensitive_env: HashMap<_, _> = env | ||
.keys() | ||
.filter_map(|k| k.to_str()) | ||
.map(|k| (k.to_uppercase(), k.to_owned())) | ||
.collect(); | ||
let normalized_env = env | ||
.iter() | ||
// Only keep entries where both the key and value are valid UTF-8, | ||
// since the config env vars only support UTF-8 keys and values. | ||
// Otherwise, the normalized map warning could incorrectly warn about entries that can't be | ||
// read by the config system. | ||
// Please see the docs for `Env` for more context. | ||
.filter_map(|(k, v)| Some((k.to_str()?, v.to_str()?))) | ||
.map(|(k, _)| (k.to_uppercase().replace("-", "_"), k.to_owned())) | ||
.collect(); | ||
(case_insensitive_env, normalized_env) | ||
} | ||
|
||
/// A snapshot of the environment variables available to [`super::Config`]. | ||
/// | ||
/// Currently, the [`Config`](super::Config) supports lookup of environment variables | ||
/// through two different means: | ||
/// | ||
/// - [`Config::get_env`](super::Config::get_env) | ||
/// and [`Config::get_env_os`](super::Config::get_env_os) | ||
/// for process environment variables (similar to [`std::env::var`] and [`std::env::var_os`]), | ||
/// - Typed Config Value API via [`Config::get`](super::Config::get). | ||
/// This is only available for `CARGO_` prefixed environment keys. | ||
/// | ||
/// This type contains the env var snapshot and helper methods for both APIs. | ||
#[derive(Debug)] | ||
pub struct Env { | ||
/// A snapshot of the process's environment variables. | ||
env: HashMap<OsString, OsString>, | ||
/// Used in the typed Config value API for warning messages when config keys are | ||
/// given in the wrong format. | ||
/// | ||
/// Maps from "normalized" (upper case and with "-" replaced by "_") env keys | ||
/// to the actual keys in the environment. | ||
/// The normalized format is the one expected by Cargo. | ||
/// | ||
/// This only holds env keys that are valid UTF-8, since [`super::ConfigKey`] only supports UTF-8 keys. | ||
/// In addition, this only holds env keys whose value in the environment is also valid UTF-8, | ||
/// since the typed Config value API only supports UTF-8 values. | ||
normalized_env: HashMap<String, String>, | ||
/// Used to implement `get_env` and `get_env_os` on Windows, where env keys are case-insensitive. | ||
/// | ||
/// Maps from uppercased env keys to the actual key in the environment. | ||
/// For example, this might hold a pair `("PATH", "Path")`. | ||
/// Currently only supports UTF-8 keys and values. | ||
case_insensitive_env: HashMap<String, String>, | ||
} | ||
|
||
impl Env { | ||
/// Create a new `Env` from process's environment variables. | ||
pub fn new() -> Self { | ||
let env: HashMap<_, _> = std::env::vars_os().collect(); | ||
let (case_insensitive_env, normalized_env) = make_case_insensitive_and_normalized_env(&env); | ||
Self { | ||
env, | ||
case_insensitive_env, | ||
normalized_env, | ||
} | ||
} | ||
|
||
/// Set the env directly from a `HashMap`. | ||
/// This should be used for debugging purposes only. | ||
pub(super) fn from_map(env: HashMap<String, String>) -> Self { | ||
let env = env.into_iter().map(|(k, v)| (k.into(), v.into())).collect(); | ||
let (case_insensitive_env, normalized_env) = make_case_insensitive_and_normalized_env(&env); | ||
Self { | ||
env, | ||
case_insensitive_env, | ||
normalized_env, | ||
} | ||
} | ||
|
||
/// Returns all environment variables as an iterator, | ||
/// keeping only entries where both the key and value are valid UTF-8. | ||
pub fn iter_str(&self) -> impl Iterator<Item = (&str, &str)> { | ||
self.env | ||
.iter() | ||
.filter_map(|(k, v)| Some((k.to_str()?, v.to_str()?))) | ||
} | ||
|
||
/// Returns all environment variable keys, filtering out keys that are not valid UTF-8. | ||
pub fn keys_str(&self) -> impl Iterator<Item = &str> { | ||
self.env.keys().filter_map(|k| k.to_str()) | ||
} | ||
|
||
/// Get the value of environment variable `key` through the `Config` snapshot. | ||
/// | ||
/// This can be used similarly to `std::env::var_os`. | ||
/// On Windows, we check for case mismatch since environment keys are case-insensitive. | ||
pub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<OsString> { | ||
match self.env.get(key.as_ref()) { | ||
Some(s) => Some(s.clone()), | ||
None => { | ||
if cfg!(windows) { | ||
self.get_env_case_insensitive(key).cloned() | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Get the value of environment variable `key` through the `self.env` snapshot. | ||
/// | ||
/// This can be used similarly to `std::env::var`. | ||
/// On Windows, we check for case mismatch since environment keys are case-insensitive. | ||
pub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<String> { | ||
let key = key.as_ref(); | ||
let s = self | ||
.get_env_os(key) | ||
.ok_or_else(|| anyhow!("{key:?} could not be found in the environment snapshot"))?; | ||
|
||
match s.to_str() { | ||
Some(s) => Ok(s.to_owned()), | ||
None => bail!("environment variable value is not valid unicode: {s:?}"), | ||
} | ||
} | ||
|
||
/// Performs a case-insensitive lookup of `key` in the environment. | ||
/// | ||
/// This is relevant on Windows, where environment variables are case-insensitive. | ||
/// Note that this only works on keys that are valid UTF-8 and it uses Unicode uppercase, | ||
/// which may differ from the OS's notion of uppercase. | ||
fn get_env_case_insensitive(&self, key: impl AsRef<OsStr>) -> Option<&OsString> { | ||
let upper_case_key = key.as_ref().to_str()?.to_uppercase(); | ||
let env_key: &OsStr = self.case_insensitive_env.get(&upper_case_key)?.as_ref(); | ||
self.env.get(env_key) | ||
} | ||
|
||
/// Get the value of environment variable `key` as a `&str`. | ||
/// Returns `None` if `key` is not in `self.env` or if the value is not valid UTF-8. | ||
/// | ||
/// This is intended for use in private methods of `Config`, | ||
/// and does not check for env key case mismatch. | ||
/// | ||
/// This is case-sensitive on Windows (even though environment keys on Windows are usually | ||
/// case-insensitive) due to an unintended regression in 1.28 (via #5552). | ||
/// This should only affect keys used for cargo's config-system env variables (`CARGO_` | ||
/// prefixed ones), which are currently all uppercase. | ||
/// We may want to consider rectifying it if users report issues. | ||
/// One thing that adds a wrinkle here is the unstable advanced-env option that *requires* | ||
/// case-sensitive keys. | ||
/// | ||
/// Do not use this for any other purposes. | ||
/// Use [`Env::get_env_os`] or [`Env::get_env`] instead, which properly handle case | ||
/// insensitivity on Windows. | ||
pub(super) fn get_str(&self, key: impl AsRef<OsStr>) -> Option<&str> { | ||
self.env.get(key.as_ref()).and_then(|s| s.to_str()) | ||
} | ||
|
||
/// Check if the environment contains `key`. | ||
/// | ||
/// This is intended for use in private methods of `Config`, | ||
/// and does not check for env key case mismatch. | ||
/// See the docstring of [`Env::get_str`] for more context. | ||
pub(super) fn contains_key(&self, key: impl AsRef<OsStr>) -> bool { | ||
self.env.contains_key(key.as_ref()) | ||
} | ||
|
||
/// Looks up a normalized `key` in the `normalized_env`. | ||
/// Returns the corresponding (non-normalized) env key if it exists, else `None`. | ||
/// | ||
/// This is used by [`super::Config::check_environment_key_case_mismatch`]. | ||
pub(super) fn get_normalized(&self, key: &str) -> Option<&str> { | ||
self.normalized_env.get(key).map(|s| s.as_ref()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could this include a comment explaining why it is case-sensitive?
I was looking over the history, and it looks like this was an unintended regression in 1.28 via #5552. I think I somehow forgot about windows case sensitivity at that time, and have been confused on a few occasions (like #9169) due to the fact that
Command
used to force keys to be uppercase on Windows (fixed in 1.55 via rust-lang/rust#85270). When launchingcargo
from within cargo's testsuite, it usedCommand
which ended up mucking with the environment keys.I don't think this needs to be fixed here in this PR. I don't think anyone has complained about this since then, so maybe it's best just to leave it.
So, a comment could explain something like: