Skip to content

Commit 2148392

Browse files
committed
fix: trace env vars from [env] table
1 parent 7d81fc5 commit 2148392

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

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

+10-4
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};
@@ -2124,6 +2124,9 @@ enum DepInfoPathType {
21242124
///
21252125
/// The serialized Cargo format will contain a list of files, all of which are
21262126
/// relative if they're under `root`. or absolute if they're elsewhere.
2127+
///
2128+
/// The `config_envs` argument is a set of environment variables that are
2129+
/// defined in `[env]` table of the `config.toml`.
21272130
pub fn translate_dep_info(
21282131
rustc_dep_info: &Path,
21292132
cargo_dep_info: &Path,
@@ -2132,6 +2135,7 @@ pub fn translate_dep_info(
21322135
target_root: &Path,
21332136
rustc_cmd: &ProcessBuilder,
21342137
allow_package: bool,
2138+
config_envs: &Arc<HashMap<String, EnvConfigValue>>,
21352139
) -> CargoResult<()> {
21362140
let depinfo = parse_rustc_dep_info(rustc_dep_info)?;
21372141

@@ -2168,9 +2172,11 @@ pub fn translate_dep_info(
21682172
// This also includes `CARGO` since if the code is explicitly wanting to
21692173
// know that path, it should be rebuilt if it changes. The CARGO path is
21702174
// 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);
2175+
//
2176+
// For issue#13280, We trace env vars that are defined in the `config.toml`.
2177+
on_disk_info.env.retain(|(key, _)| {
2178+
config_envs.contains_key(key) || !rustc_cmd.get_envs().contains_key(key) || key == CARGO_ENV
2179+
});
21742180

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

src/cargo/core/compiler/mod.rs

+2-1
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!(

src/cargo/util/context/mod.rs

+4-4
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,10 +1833,10 @@ impl GlobalContext {
18331833
&self.progress_config
18341834
}
18351835

1836-
pub fn env_config(&self) -> CargoResult<&EnvConfig> {
1836+
pub fn env_config(&self) -> CargoResult<&Arc<EnvConfig>> {
18371837
let env_config = self
18381838
.env_config
1839-
.try_borrow_with(|| self.get::<EnvConfig>("env"))?;
1839+
.try_borrow_with(|| CargoResult::Ok(Arc::new(self.get::<EnvConfig>("env")?)))?;
18401840

18411841
// Reasons for disallowing these values:
18421842
//

tests/testsuite/cargo_env_config.rs

+4-2
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
@@ -355,10 +356,11 @@ one
355356

356357
p.cargo(r#"run --config 'env.ENV_TEST="two"'"#)
357358
.with_stdout_data(str![[r#"
358-
one
359+
two
359360
360361
"#]])
361362
.with_stderr_data(str![[r#"
363+
[COMPILING] foo v0.5.0 ([ROOT]/foo)
362364
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
363365
[RUNNING] `target/debug/foo[EXE]`
364366

0 commit comments

Comments
 (0)