Skip to content

Commit edd0732

Browse files
committed
refactor: Use Arc wraps GlobalContext::env_config and resolve value in advance.
1 parent 02d2c77 commit edd0732

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,10 +1987,7 @@ pub(crate) fn apply_env_config(
19871987
if cmd.get_envs().contains_key(key) {
19881988
continue;
19891989
}
1990-
1991-
if value.is_force() || gctx.get_env_os(key).is_none() {
1992-
cmd.env(key, value.resolve(gctx));
1993-
}
1990+
cmd.env(key, value);
19941991
}
19951992
Ok(())
19961993
}

src/cargo/util/context/mod.rs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use std::io::SeekFrom;
6363
use std::mem;
6464
use std::path::{Path, PathBuf};
6565
use std::str::FromStr;
66-
use std::sync::Once;
66+
use std::sync::{Arc, Once};
6767
use std::time::Instant;
6868

6969
use self::ConfigValue as CV;
@@ -227,7 +227,7 @@ pub struct GlobalContext {
227227
target_cfgs: LazyCell<Vec<(String, TargetCfgConfig)>>,
228228
doc_extern_map: LazyCell<RustdocExternMap>,
229229
progress_config: ProgressConfig,
230-
env_config: LazyCell<EnvConfig>,
230+
env_config: LazyCell<Arc<HashMap<String, OsString>>>,
231231
/// This should be false if:
232232
/// - this is an artifact of the rustc distribution process for "stable" or for "beta"
233233
/// - this is an `#[test]` that does not opt in with `enable_nightly_features`
@@ -1833,34 +1833,47 @@ impl GlobalContext {
18331833
&self.progress_config
18341834
}
18351835

1836-
pub fn env_config(&self) -> CargoResult<&EnvConfig> {
1837-
let env_config = self
1838-
.env_config
1839-
.try_borrow_with(|| self.get::<EnvConfig>("env"))?;
1840-
1841-
// Reasons for disallowing these values:
1842-
//
1843-
// - CARGO_HOME: The initial call to cargo does not honor this value
1844-
// from the [env] table. Recursive calls to cargo would use the new
1845-
// value, possibly behaving differently from the outer cargo.
1846-
//
1847-
// - RUSTUP_HOME and RUSTUP_TOOLCHAIN: Under normal usage with rustup,
1848-
// this will have no effect because the rustup proxy sets
1849-
// RUSTUP_HOME and RUSTUP_TOOLCHAIN, and that would override the
1850-
// [env] table. If the outer cargo is executed directly
1851-
// circumventing the rustup proxy, then this would affect calls to
1852-
// rustc (assuming that is a proxy), which could potentially cause
1853-
// problems with cargo and rustc being from different toolchains. We
1854-
// consider this to be not a use case we would like to support,
1855-
// since it will likely cause problems or lead to confusion.
1856-
for disallowed in &["CARGO_HOME", "RUSTUP_HOME", "RUSTUP_TOOLCHAIN"] {
1857-
if env_config.contains_key(*disallowed) {
1858-
bail!(
1859-
"setting the `{disallowed}` environment variable is not supported \
1860-
in the `[env]` configuration table"
1861-
);
1862-
}
1863-
}
1836+
/// Get the env vars from the config `[env]` table which
1837+
/// are `force = true` or don't exist in the env snapshot [`GlobalContext::get_env`].
1838+
pub fn env_config(&self) -> CargoResult<&Arc<HashMap<String, OsString>>> {
1839+
let env_config = self.env_config.try_borrow_with(|| {
1840+
CargoResult::Ok(Arc::new({
1841+
let env_config = self.get::<EnvConfig>("env")?;
1842+
// Reasons for disallowing these values:
1843+
//
1844+
// - CARGO_HOME: The initial call to cargo does not honor this value
1845+
// from the [env] table. Recursive calls to cargo would use the new
1846+
// value, possibly behaving differently from the outer cargo.
1847+
//
1848+
// - RUSTUP_HOME and RUSTUP_TOOLCHAIN: Under normal usage with rustup,
1849+
// this will have no effect because the rustup proxy sets
1850+
// RUSTUP_HOME and RUSTUP_TOOLCHAIN, and that would override the
1851+
// [env] table. If the outer cargo is executed directly
1852+
// circumventing the rustup proxy, then this would affect calls to
1853+
// rustc (assuming that is a proxy), which could potentially cause
1854+
// problems with cargo and rustc being from different toolchains. We
1855+
// consider this to be not a use case we would like to support,
1856+
// since it will likely cause problems or lead to confusion.
1857+
for disallowed in &["CARGO_HOME", "RUSTUP_HOME", "RUSTUP_TOOLCHAIN"] {
1858+
if env_config.contains_key(*disallowed) {
1859+
bail!(
1860+
"setting the `{disallowed}` environment variable is not supported \
1861+
in the `[env]` configuration table"
1862+
);
1863+
}
1864+
}
1865+
env_config
1866+
.into_iter()
1867+
.filter_map(|(k, v)| {
1868+
if v.is_force() || self.get_env_os(&k).is_none() {
1869+
Some((k, v.resolve(self).to_os_string()))
1870+
} else {
1871+
None
1872+
}
1873+
})
1874+
.collect()
1875+
}))
1876+
})?;
18641877

18651878
Ok(env_config)
18661879
}

0 commit comments

Comments
 (0)