Skip to content

Commit 0a264a9

Browse files
committed
fix(env): Get environment variables from the latest environment config
1 parent 5f4592d commit 0a264a9

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use cargo_util::{paths, ProcessBuilder};
2121
use serde::{Deserialize, Serialize};
2222
use std::cell::RefCell;
2323
use std::collections::hash_map::{Entry, HashMap};
24+
use std::collections::BTreeMap;
25+
use std::ffi::OsString;
2426
use std::path::{Path, PathBuf};
2527
use std::str::{self, FromStr};
2628

@@ -588,6 +590,10 @@ impl TargetInfo {
588590
.iter()
589591
.any(|sup| sup.as_str() == split.as_str())
590592
}
593+
594+
pub fn get_target_envs(&self) -> CargoResult<&BTreeMap<String, Option<OsString>>> {
595+
return Ok(self.crate_type_process.get_envs());
596+
}
591597
}
592598

593599
/// Takes rustc output (using specialized command line args), and calculates the file prefix and

src/cargo/core/compiler/fingerprint/mod.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ mod dirty_reason;
357357

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

360+
use std::collections::BTreeMap;
360361
use std::env;
362+
use std::ffi::OsString;
361363
use std::hash::{self, Hash, Hasher};
362364
use std::io;
363365
use std::path::{Path, PathBuf};
@@ -772,10 +774,20 @@ impl LocalFingerprint {
772774
// TODO: This is allowed at this moment. Should figure out if it makes
773775
// sense if permitting to read env from the config system.
774776
#[allow(clippy::disallowed_methods)]
775-
fn from_env<K: AsRef<str>>(key: K) -> LocalFingerprint {
777+
fn from_env<K: AsRef<str>>(
778+
key: K,
779+
envs: &BTreeMap<String, Option<OsString>>,
780+
) -> LocalFingerprint {
776781
let key = key.as_ref();
777782
let var = key.to_owned();
778-
let val = env::var(key).ok();
783+
let val = envs
784+
.get(key)
785+
.and_then(|opt| {
786+
opt.as_ref()
787+
.and_then(|os_str| os_str.clone().into_string().ok())
788+
})
789+
.or_else(|| env::var_os(key).and_then(|os_str| os_str.into_string().ok()));
790+
779791
LocalFingerprint::RerunIfEnvChanged { var, val }
780792
}
781793

@@ -1608,6 +1620,13 @@ fn build_script_local_fingerprints(
16081620
bool,
16091621
) {
16101622
assert!(unit.mode.is_run_custom_build());
1623+
let envs = build_runner
1624+
.bcx
1625+
.target_data
1626+
.info(unit.kind)
1627+
.get_target_envs()
1628+
.unwrap()
1629+
.clone();
16111630
// First up, if this build script is entirely overridden, then we just
16121631
// return the hash of what we overrode it with. This is the easy case!
16131632
if let Some(fingerprint) = build_script_override_fingerprint(build_runner, unit) {
@@ -1660,7 +1679,12 @@ fn build_script_local_fingerprints(
16601679
// Ok so now we're in "new mode" where we can have files listed as
16611680
// dependencies as well as env vars listed as dependencies. Process
16621681
// them all here.
1663-
Ok(Some(local_fingerprints_deps(deps, &target_dir, &pkg_root)))
1682+
Ok(Some(local_fingerprints_deps(
1683+
deps,
1684+
&target_dir,
1685+
&pkg_root,
1686+
&envs,
1687+
)))
16641688
};
16651689

16661690
// Note that `false` == "not overridden"
@@ -1695,6 +1719,7 @@ fn local_fingerprints_deps(
16951719
deps: &BuildDeps,
16961720
target_root: &Path,
16971721
pkg_root: &Path,
1722+
envs: &BTreeMap<String, Option<OsString>>,
16981723
) -> Vec<LocalFingerprint> {
16991724
debug!("new local fingerprints deps {:?}", pkg_root);
17001725
let mut local = Vec::new();
@@ -1719,7 +1744,7 @@ fn local_fingerprints_deps(
17191744
local.extend(
17201745
deps.rerun_if_env_changed
17211746
.iter()
1722-
.map(LocalFingerprint::from_env),
1747+
.map(|v| LocalFingerprint::from_env(v, &envs)),
17231748
);
17241749

17251750
local

tests/testsuite/build_script_env.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,12 @@ fn rerun_if_env_changes_config() {
4646
"#,
4747
);
4848

49-
p.cargo("check")
50-
.with_stderr_data(str![[r#"
51-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
52-
53-
"#]])
54-
.run();
55-
56-
p.cargo("clean").run();
5749
p.cargo("check")
5850
.with_status(101)
5951
.with_stderr_data(
6052
"\
61-
[COMPILING] foo v0.1.0 ([ROOT]/foo)
62-
[ERROR] failed to run custom build command for `foo v0.1.0 ([..])`
53+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
54+
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
6355
...",
6456
)
6557
.run();

0 commit comments

Comments
 (0)