Skip to content

Commit

Permalink
fix: apply [env] to target info discovery rustc
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Apr 24, 2023
1 parent e1c7ce8 commit 00484fe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 3 additions & 12 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}
Expand Down
15 changes: 15 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
7 changes: 2 additions & 5 deletions tests/testsuite/cargo_env_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 00484fe

Please sign in to comment.