From 00484fe00a8ccc2f7653138ebd5218ebab42a7bf Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Mon, 24 Apr 2023 18:59:01 +0100 Subject: [PATCH] fix: apply `[env]` to target info discovery rustc --- .../core/compiler/build_context/target_info.rs | 2 ++ src/cargo/core/compiler/compilation.rs | 15 +++------------ src/cargo/core/compiler/mod.rs | 15 +++++++++++++++ tests/testsuite/cargo_env_config.rs | 7 ++----- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index bf8bb157877..d134524e6c4 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -7,6 +7,7 @@ //! * [`RustcTargetData::info`] to get a [`TargetInfo`] for an in-depth query. //! * [`TargetInfo::rustc_outputs`] to get a list of supported file types. +use crate::core::compiler::apply_env_config; use crate::core::compiler::{ BuildOutput, CompileKind, CompileMode, CompileTarget, Context, CrateType, }; @@ -175,6 +176,7 @@ impl TargetInfo { // // Search `--print` to see what we query so far. let mut process = rustc.workspace_process(); + apply_env_config(config, &mut process)?; process .arg("-") .arg("--crate-name") diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 29642f13d79..b263119b039 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -7,7 +7,8 @@ use std::path::PathBuf; use cargo_platform::CfgExpr; use cargo_util::{paths, ProcessBuilder}; -use super::BuildContext; +use crate::core::compiler::apply_env_config; +use crate::core::compiler::BuildContext; use crate::core::compiler::{CompileKind, Metadata, Unit}; use crate::core::Package; use crate::util::{config, CargoResult, Config}; @@ -349,17 +350,7 @@ impl<'cfg> Compilation<'cfg> { ) .cwd(pkg.root()); - // Apply any environment variables from the config - for (key, value) in self.config.env_config()?.iter() { - // never override a value that has already been set by cargo - if cmd.get_envs().contains_key(key) { - continue; - } - - if value.is_force() || self.config.get_env_os(key).is_none() { - cmd.env(key, value.resolve(self.config)); - } - } + apply_env_config(self.config, &mut cmd)?; Ok(cmd) } diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 1c37cc1aa17..0af59049c24 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1805,3 +1805,18 @@ fn descriptive_pkg_name(name: &str, target: &Target, mode: &CompileMode) -> Stri }; format!("`{name}` ({desc_name}{mode})") } + +/// Applies environment variables from config `[env]` to [`ProcessBuilder`]. +fn apply_env_config(config: &crate::Config, cmd: &mut ProcessBuilder) -> CargoResult<()> { + for (key, value) in config.env_config()?.iter() { + // never override a value that has already been set by cargo + if cmd.get_envs().contains_key(key) { + continue; + } + + if value.is_force() || config.get_env_os(key).is_none() { + cmd.env(key, value.resolve(config)); + } + } + Ok(()) +} diff --git a/tests/testsuite/cargo_env_config.rs b/tests/testsuite/cargo_env_config.rs index bdcf0043083..0b787bebd3c 100644 --- a/tests/testsuite/cargo_env_config.rs +++ b/tests/testsuite/cargo_env_config.rs @@ -224,11 +224,8 @@ fn env_applied_to_target_info_discovery_rustc() { p.cargo("run") .env("RUSTC_WORKSPACE_WRAPPER", wrapper) - .with_stderr_contains("error: failed to run `rustc` to learn about target-specific information") - .with_stderr_contains("[..]thread '[..]' panicked at [..]unwrap[..]") // env::var().unwrap() - .with_stderr_does_not_contain("WRAPPER ENV_TEST:from-config") - .with_stderr_does_not_contain("MAIN ENV_TEST:from-config") - .with_status(101) + .with_stderr_contains("WRAPPER ENV_TEST:from-config") + .with_stderr_contains("MAIN ENV_TEST:from-config") .run(); // Ensure wrapper also maintains the same overridden priority for envs.