Skip to content

Commit 668d82e

Browse files
committed
fix: trace [env] config table into fingerprint.
1 parent edd0732 commit 668d82e

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

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

+15-5
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@
362362
mod dirty_reason;
363363

364364
use std::collections::hash_map::{Entry, HashMap};
365-
366365
use std::env;
366+
use std::ffi::OsString;
367367
use std::fmt::{self, Display};
368368
use std::fs::{self, File};
369369
use std::hash::{self, Hash, Hasher};
@@ -849,7 +849,11 @@ 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.to_str().and_then(|s| Some(s.to_string()))
854+
} else {
855+
gctx.get_env(key).ok()
856+
}
853857
};
854858
if current == *previous {
855859
continue;
@@ -2124,6 +2128,9 @@ enum DepInfoPathType {
21242128
///
21252129
/// The serialized Cargo format will contain a list of files, all of which are
21262130
/// relative if they're under `root`. or absolute if they're elsewhere.
2131+
///
2132+
/// The `env_config` argument is a set of environment variables that are
2133+
/// defined in `[env]` table of the `config.toml`.
21272134
pub fn translate_dep_info(
21282135
rustc_dep_info: &Path,
21292136
cargo_dep_info: &Path,
@@ -2132,6 +2139,7 @@ pub fn translate_dep_info(
21322139
target_root: &Path,
21332140
rustc_cmd: &ProcessBuilder,
21342141
allow_package: bool,
2142+
env_config: &Arc<HashMap<String, OsString>>,
21352143
) -> CargoResult<()> {
21362144
let depinfo = parse_rustc_dep_info(rustc_dep_info)?;
21372145

@@ -2168,9 +2176,11 @@ pub fn translate_dep_info(
21682176
// This also includes `CARGO` since if the code is explicitly wanting to
21692177
// know that path, it should be rebuilt if it changes. The CARGO path is
21702178
// 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);
2179+
//
2180+
// For cargo#13280, We trace env vars that are defined in the `[env]` config table.
2181+
on_disk_info.env.retain(|(key, _)| {
2182+
env_config.contains_key(key) || !rustc_cmd.get_envs().contains_key(key) || key == CARGO_ENV
2183+
});
21742184

21752185
let serialize_path = |file| {
21762186
// 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 env_config = 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+
&env_config,
462463
)
463464
.with_context(|| {
464465
internal(format!(

tests/testsuite/cargo_env_config.rs

+6-4
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,7 +329,7 @@ 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#"
@@ -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
// This identical cargo invocation is to ensure no rebuild happen.
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)