|
1 | 1 | #[cfg(procmacro2_semver_exempt)]
|
2 | 2 | use std::collections::HashSet;
|
3 | 3 | use std::collections::{hash_map, HashMap};
|
4 |
| -#[cfg(procmacro2_semver_exempt)] |
5 | 4 | use std::hash::{BuildHasherDefault, DefaultHasher};
|
6 | 5 | use std::path::{Path, PathBuf};
|
7 | 6 | use std::sync::{Arc, LazyLock, Mutex};
|
@@ -422,19 +421,26 @@ where
|
422 | 421 | static TRACKED_ENV_VARS: Mutex<HashSet<String, BuildHasherDefault<DefaultHasher>>> =
|
423 | 422 | Mutex::new(HashSet::with_hasher(BuildHasherDefault::new()));
|
424 | 423 |
|
| 424 | +static LOADED_ENV_VARS: Mutex<HashMap<String, String, BuildHasherDefault<DefaultHasher>>> = |
| 425 | + Mutex::new(HashMap::with_hasher(BuildHasherDefault::new())); |
| 426 | + |
425 | 427 | /// Get the value of an environment variable, telling the compiler about it if applicable.
|
426 | 428 | fn env(name: &str) -> Result<String, std::env::VarError> {
|
427 | 429 | #[cfg(procmacro2_semver_exempt)]
|
428 |
| - if TRACKED_ENV_VARS.lock().unwrap().insert(name.to_string()) { |
| 430 | + let tracked_value = if TRACKED_ENV_VARS.lock().unwrap().insert(name.to_string()) { |
429 | 431 | // Avoid tracking the same env var multiple times, which would undesirably modify
|
430 | 432 | // build system state and thus behavior in case we change var values.
|
431 | 433 | proc_macro::tracked_env::var(name)
|
432 | 434 | } else {
|
433 |
| - std::env::var(name) |
434 |
| - } |
435 |
| - |
| 435 | + None |
| 436 | + }; |
436 | 437 | #[cfg(not(procmacro2_semver_exempt))]
|
437 |
| - std::env::var(name) |
| 438 | + let tracked_value = None; |
| 439 | + |
| 440 | + tracked_value |
| 441 | + .or_else(|| std::env::var(name).ok()) |
| 442 | + .or_else(|| LOADED_ENV_VARS.lock().unwrap().get(name).cloned()) |
| 443 | + .ok_or(std::env::VarError::NotPresent) |
438 | 444 | }
|
439 | 445 |
|
440 | 446 | /// Load configuration environment variables from a `.env` file, without overriding existing
|
@@ -487,7 +493,7 @@ fn load_env(manifest_dir: &Path, config: &Config) {
|
487 | 493 | };
|
488 | 494 |
|
489 | 495 | if loadable_vars.contains(&&*key) && std::env::var(&key).is_err() {
|
490 |
| - std::env::set_var(key, value); |
| 496 | + LOADED_ENV_VARS.lock().unwrap().insert(key, value); |
491 | 497 | }
|
492 | 498 | }
|
493 | 499 | }
|
|
0 commit comments