Skip to content

Commit

Permalink
fix: trace env vars from [env] table
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Oct 18, 2024
1 parent 7d81fc5 commit 1297f40
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@
mod dirty_reason;

use std::collections::hash_map::{Entry, HashMap};

use std::collections::HashSet;
use std::env;
use std::fmt::{self, Display};
use std::fs::{self, File};
Expand Down Expand Up @@ -2124,6 +2124,9 @@ enum DepInfoPathType {
///
/// The serialized Cargo format will contain a list of files, all of which are
/// relative if they're under `root`. or absolute if they're elsewhere.
///
/// The `config_envs` argument is a set of environment variables that are
/// defined in `[env]` table of the `config.toml`.
pub fn translate_dep_info(
rustc_dep_info: &Path,
cargo_dep_info: &Path,
Expand All @@ -2132,6 +2135,7 @@ pub fn translate_dep_info(
target_root: &Path,
rustc_cmd: &ProcessBuilder,
allow_package: bool,
config_envs: &HashSet<String>,
) -> CargoResult<()> {
let depinfo = parse_rustc_dep_info(rustc_dep_info)?;

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

let serialize_path = |file| {
// The path may be absolute or relative, canonical or not. Make sure
Expand Down
9 changes: 8 additions & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,13 @@ fn rustc(
if hide_diagnostics_for_scrape_unit {
output_options.show_diagnostics = false;
}

let config_envs: HashSet<String> = build_runner
.bcx
.gctx
.env_config()?
.keys()
.cloned()
.collect();
return Ok(Work::new(move |state| {
// Artifacts are in a different location than typical units,
// hence we must assure the crate- and target-dependent
Expand Down Expand Up @@ -459,6 +465,7 @@ fn rustc(
&rustc,
// Do not track source files in the fingerprint for registry dependencies.
is_local,
&config_envs,
)
.with_context(|| {
internal(format!(
Expand Down
6 changes: 4 additions & 2 deletions tests/testsuite/cargo_env_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,11 @@ from-config
p.cargo("run")
.env("ENV_TEST", "from-env")
.with_stdout_data(str![[r#"
from-config
from-env
"#]])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.5.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
Expand Down Expand Up @@ -355,10 +356,11 @@ one

p.cargo(r#"run --config 'env.ENV_TEST="two"'"#)
.with_stdout_data(str![[r#"
one
two
"#]])
.with_stderr_data(str![[r#"
[COMPILING] foo v0.5.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] `target/debug/foo[EXE]`
Expand Down

0 comments on commit 1297f40

Please sign in to comment.