Skip to content

Commit 1cd4ef3

Browse files
committed
Auto merge of #12029 - weihanglo:issue/12028, r=epage
fix: apply `[env]` to target info discovery rustc ### What does this PR try to resolve? The first rustc invoked by Cargo is for learning target specific info. However, Cargo didn't apply `[env]` to that rustc invocation. This was an oversight when implementing `[env]` configuration.
2 parents e0910a2 + 00484fe commit 1cd4ef3

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Tests for `[env]` config.
22
3+
use cargo_test_support::basic_manifest;
34
use cargo_test_support::{basic_bin_manifest, project};
45

56
#[cargo_test]
@@ -179,3 +180,60 @@ fn env_no_override() {
179180
.with_stdout_contains("CARGO_PKG_NAME:unchanged")
180181
.run();
181182
}
183+
184+
#[cargo_test]
185+
fn env_applied_to_target_info_discovery_rustc() {
186+
let wrapper = project()
187+
.at("wrapper")
188+
.file("Cargo.toml", &basic_manifest("wrapper", "1.0.0"))
189+
.file(
190+
"src/main.rs",
191+
r#"
192+
fn main() {
193+
let mut args = std::env::args().skip(1);
194+
let env_test = std::env::var("ENV_TEST").unwrap();
195+
eprintln!("WRAPPER ENV_TEST:{env_test}");
196+
let status = std::process::Command::new(&args.next().unwrap())
197+
.args(args).status().unwrap();
198+
std::process::exit(status.code().unwrap_or(1));
199+
}
200+
"#,
201+
)
202+
.build();
203+
wrapper.cargo("build").run();
204+
let wrapper = &wrapper.bin("wrapper");
205+
206+
let p = project()
207+
.file("Cargo.toml", &basic_bin_manifest("foo"))
208+
.file(
209+
"src/main.rs",
210+
r#"
211+
fn main() {
212+
eprintln!( "MAIN ENV_TEST:{}", std::env!("ENV_TEST") );
213+
}
214+
"#,
215+
)
216+
.file(
217+
".cargo/config",
218+
r#"
219+
[env]
220+
ENV_TEST = "from-config"
221+
"#,
222+
)
223+
.build();
224+
225+
p.cargo("run")
226+
.env("RUSTC_WORKSPACE_WRAPPER", wrapper)
227+
.with_stderr_contains("WRAPPER ENV_TEST:from-config")
228+
.with_stderr_contains("MAIN ENV_TEST:from-config")
229+
.run();
230+
231+
// Ensure wrapper also maintains the same overridden priority for envs.
232+
p.cargo("clean").run();
233+
p.cargo("run")
234+
.env("ENV_TEST", "from-env")
235+
.env("RUSTC_WORKSPACE_WRAPPER", wrapper)
236+
.with_stderr_contains("WRAPPER ENV_TEST:from-env")
237+
.with_stderr_contains("MAIN ENV_TEST:from-env")
238+
.run();
239+
}

0 commit comments

Comments
 (0)