Skip to content

Commit d126728

Browse files
committed
fix: trace env vars from [env] table
1 parent 4a96a40 commit d126728

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

src/cargo/core/compiler/fingerprint/mod.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@
362362
mod dirty_reason;
363363

364364
use std::collections::hash_map::{Entry, HashMap};
365-
366365
use std::env;
367366
use std::fmt::{self, Display};
368367
use std::fs::{self, File};
@@ -383,6 +382,7 @@ use tracing::{debug, info};
383382

384383
use crate::core::compiler::unit_graph::UnitDep;
385384
use crate::core::Package;
385+
use crate::util::context::EnvConfigValue;
386386
use crate::util::errors::CargoResult;
387387
use crate::util::interning::InternedString;
388388
use crate::util::{self, try_canonicalize};
@@ -849,7 +849,14 @@ impl LocalFingerprint {
849849
.to_string(),
850850
)
851851
} else {
852-
gctx.get_env(key).ok()
852+
if let Some(value) = gctx.env_config()?.get(key) {
853+
value
854+
.resolve(gctx)
855+
.to_str()
856+
.and_then(|s| Some(s.to_string()))
857+
} else {
858+
gctx.get_env(key).ok()
859+
}
853860
};
854861
if current == *previous {
855862
continue;
@@ -2124,6 +2131,9 @@ enum DepInfoPathType {
21242131
///
21252132
/// The serialized Cargo format will contain a list of files, all of which are
21262133
/// relative if they're under `root`. or absolute if they're elsewhere.
2134+
///
2135+
/// The `config_envs` argument is a set of environment variables that are
2136+
/// defined in `[env]` table of the `config.toml`.
21272137
pub fn translate_dep_info(
21282138
rustc_dep_info: &Path,
21292139
cargo_dep_info: &Path,
@@ -2132,6 +2142,7 @@ pub fn translate_dep_info(
21322142
target_root: &Path,
21332143
rustc_cmd: &ProcessBuilder,
21342144
allow_package: bool,
2145+
config_envs: &Arc<HashMap<String, EnvConfigValue>>,
21352146
) -> CargoResult<()> {
21362147
let depinfo = parse_rustc_dep_info(rustc_dep_info)?;
21372148

@@ -2168,9 +2179,11 @@ pub fn translate_dep_info(
21682179
// This also includes `CARGO` since if the code is explicitly wanting to
21692180
// know that path, it should be rebuilt if it changes. The CARGO path is
21702181
// not tracked elsewhere in the fingerprint.
2171-
on_disk_info
2172-
.env
2173-
.retain(|(key, _)| !rustc_cmd.get_envs().contains_key(key) || key == CARGO_ENV);
2182+
//
2183+
// For issue#13280, We trace env vars that are defined in the `config.toml`.
2184+
on_disk_info.env.retain(|(key, _)| {
2185+
config_envs.contains_key(key) || !rustc_cmd.get_envs().contains_key(key) || key == CARGO_ENV
2186+
});
21742187

21752188
let serialize_path = |file| {
21762189
// The path may be absolute or relative, canonical or not. Make sure

src/cargo/core/compiler/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn rustc(
330330
if hide_diagnostics_for_scrape_unit {
331331
output_options.show_diagnostics = false;
332332
}
333-
333+
let config_envs = Arc::clone(build_runner.bcx.gctx.env_config()?);
334334
return Ok(Work::new(move |state| {
335335
// Artifacts are in a different location than typical units,
336336
// hence we must assure the crate- and target-dependent
@@ -459,6 +459,7 @@ fn rustc(
459459
&rustc,
460460
// Do not track source files in the fingerprint for registry dependencies.
461461
is_local,
462+
&config_envs,
462463
)
463464
.with_context(|| {
464465
internal(format!(
@@ -1987,10 +1988,7 @@ pub(crate) fn apply_env_config(
19871988
if cmd.get_envs().contains_key(key) {
19881989
continue;
19891990
}
1990-
1991-
if value.is_force() || gctx.get_env_os(key).is_none() {
1992-
cmd.env(key, value.resolve(gctx));
1993-
}
1991+
cmd.env(key, value.resolve(gctx));
19941992
}
19951993
Ok(())
19961994
}

src/cargo/util/context/mod.rs

Lines changed: 37 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<EnvConfig>>,
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,41 @@ 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<EnvConfig>> {
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(|(k, v)| v.is_force() || self.get_env_os(k).is_none())
1868+
.collect()
1869+
}))
1870+
})?;
18641871

18651872
Ok(env_config)
18661873
}

tests/testsuite/cargo_env_config.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,11 @@ from-config
315315
p.cargo("run")
316316
.env("ENV_TEST", "from-env")
317317
.with_stdout_data(str![[r#"
318-
from-config
318+
from-env
319319
320320
"#]])
321321
.with_stderr_data(str![[r#"
322+
[COMPILING] foo v0.5.0 ([ROOT]/foo)
322323
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
323324
[RUNNING] `target/debug/foo[EXE]`
324325
@@ -328,12 +329,12 @@ from-config
328329
p.cargo("run")
329330
.env("ENV_TEST", "from-env")
330331
.with_stdout_data(str![[r#"
331-
from-config
332+
from-env
332333
333334
"#]])
334335
.with_stderr_data(str![[r#"
335336
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
336-
[RUNNING] `target/debug/foo[EXE]`
337+
[RUNNING] `target/debug/foo`
337338
338339
"#]])
339340
.run();
@@ -417,10 +418,11 @@ one
417418

418419
p.cargo(r#"run --config 'env.ENV_TEST="two"'"#)
419420
.with_stdout_data(str![[r#"
420-
one
421+
two
421422
422423
"#]])
423424
.with_stderr_data(str![[r#"
425+
[COMPILING] foo v0.5.0 ([ROOT]/foo)
424426
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
425427
[RUNNING] `target/debug/foo[EXE]`
426428
@@ -429,7 +431,7 @@ one
429431

430432
p.cargo(r#"run --config 'env.ENV_TEST="two"'"#)
431433
.with_stdout_data(str![[r#"
432-
one
434+
two
433435
434436
"#]])
435437
.with_stderr_data(str![[r#"

0 commit comments

Comments
 (0)