Skip to content

Commit 00484fe

Browse files
committed
fix: apply [env] to target info discovery rustc
1 parent e1c7ce8 commit 00484fe

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//! * [`RustcTargetData::info`] to get a [`TargetInfo`] for an in-depth query.
88
//! * [`TargetInfo::rustc_outputs`] to get a list of supported file types.
99
10+
use crate::core::compiler::apply_env_config;
1011
use crate::core::compiler::{
1112
BuildOutput, CompileKind, CompileMode, CompileTarget, Context, CrateType,
1213
};
@@ -175,6 +176,7 @@ impl TargetInfo {
175176
//
176177
// Search `--print` to see what we query so far.
177178
let mut process = rustc.workspace_process();
179+
apply_env_config(config, &mut process)?;
178180
process
179181
.arg("-")
180182
.arg("--crate-name")

src/cargo/core/compiler/compilation.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::path::PathBuf;
77
use cargo_platform::CfgExpr;
88
use cargo_util::{paths, ProcessBuilder};
99

10-
use super::BuildContext;
10+
use crate::core::compiler::apply_env_config;
11+
use crate::core::compiler::BuildContext;
1112
use crate::core::compiler::{CompileKind, Metadata, Unit};
1213
use crate::core::Package;
1314
use crate::util::{config, CargoResult, Config};
@@ -349,17 +350,7 @@ impl<'cfg> Compilation<'cfg> {
349350
)
350351
.cwd(pkg.root());
351352

352-
// Apply any environment variables from the config
353-
for (key, value) in self.config.env_config()?.iter() {
354-
// never override a value that has already been set by cargo
355-
if cmd.get_envs().contains_key(key) {
356-
continue;
357-
}
358-
359-
if value.is_force() || self.config.get_env_os(key).is_none() {
360-
cmd.env(key, value.resolve(self.config));
361-
}
362-
}
353+
apply_env_config(self.config, &mut cmd)?;
363354

364355
Ok(cmd)
365356
}

src/cargo/core/compiler/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,3 +1805,18 @@ fn descriptive_pkg_name(name: &str, target: &Target, mode: &CompileMode) -> Stri
18051805
};
18061806
format!("`{name}` ({desc_name}{mode})")
18071807
}
1808+
1809+
/// Applies environment variables from config `[env]` to [`ProcessBuilder`].
1810+
fn apply_env_config(config: &crate::Config, cmd: &mut ProcessBuilder) -> CargoResult<()> {
1811+
for (key, value) in config.env_config()?.iter() {
1812+
// never override a value that has already been set by cargo
1813+
if cmd.get_envs().contains_key(key) {
1814+
continue;
1815+
}
1816+
1817+
if value.is_force() || config.get_env_os(key).is_none() {
1818+
cmd.env(key, value.resolve(config));
1819+
}
1820+
}
1821+
Ok(())
1822+
}

tests/testsuite/cargo_env_config.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,8 @@ fn env_applied_to_target_info_discovery_rustc() {
224224

225225
p.cargo("run")
226226
.env("RUSTC_WORKSPACE_WRAPPER", wrapper)
227-
.with_stderr_contains("error: failed to run `rustc` to learn about target-specific information")
228-
.with_stderr_contains("[..]thread '[..]' panicked at [..]unwrap[..]") // env::var().unwrap()
229-
.with_stderr_does_not_contain("WRAPPER ENV_TEST:from-config")
230-
.with_stderr_does_not_contain("MAIN ENV_TEST:from-config")
231-
.with_status(101)
227+
.with_stderr_contains("WRAPPER ENV_TEST:from-config")
228+
.with_stderr_contains("MAIN ENV_TEST:from-config")
232229
.run();
233230

234231
// Ensure wrapper also maintains the same overridden priority for envs.

0 commit comments

Comments
 (0)