Skip to content

Commit 577222c

Browse files
committed
change config to actually read the env variables through config.env snapshot
1 parent 747cb6f commit 577222c

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/cargo/util/config/mod.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use std::cell::{RefCell, RefMut};
5454
use std::collections::hash_map::Entry::{Occupied, Vacant};
5555
use std::collections::{HashMap, HashSet};
5656
use std::env;
57-
use std::ffi::{OsStr, OsString};
57+
use std::ffi::OsStr;
5858
use std::fmt;
5959
use std::fs::{self, File};
6060
use std::io::prelude::*;
@@ -765,14 +765,24 @@ impl Config {
765765
}
766766
}
767767

768-
/// Wrapper around `std::env::var`.
769-
pub fn get_env(&self, key: impl AsRef<OsStr>) -> CargoResult<String> {
770-
Ok(std::env::var(key)?)
768+
/// Get the value of environment variable `key` through the `Config` snapshot.
769+
///
770+
/// This can be used similarly to `std::env::var`.
771+
pub fn get_env(&self, key: impl AsRef<str>) -> CargoResult<String> {
772+
match self.env.get(key.as_ref()) {
773+
Some(s) => Ok(s.clone()),
774+
None => bail!(
775+
"{} could not be found in the environment snapshot",
776+
key.as_ref()
777+
),
778+
}
771779
}
772780

773-
/// Wrapper around `std::env::var`.
774-
pub fn get_env_os(&self, key: impl AsRef<OsStr>) -> Option<OsString> {
775-
std::env::var_os(key)
781+
/// Get the value of environment variable `key` through the `Config` snapshot.
782+
///
783+
/// This can be used similarly to `std::env::var_os`.
784+
pub fn get_env_os(&self, key: impl AsRef<str>) -> Option<String> {
785+
self.env.get(key.as_ref()).cloned()
776786
}
777787

778788
/// Check if the [`Config`] contains a given [`ConfigKey`].
@@ -1629,10 +1639,7 @@ impl Config {
16291639

16301640
match self.get_env_os(&var) {
16311641
Some(tool_path) => {
1632-
let maybe_relative = match tool_path.to_str() {
1633-
Some(s) => s.contains('/') || s.contains('\\'),
1634-
None => false,
1635-
};
1642+
let maybe_relative = tool_path.contains('/') || tool_path.contains('\\');
16361643
let path = if maybe_relative {
16371644
self.cwd.join(tool_path)
16381645
} else {

0 commit comments

Comments
 (0)