Skip to content

Commit

Permalink
fix(env): Get environment variables from the latest environment config
Browse files Browse the repository at this point in the history
  • Loading branch information
heisen-li committed Jun 14, 2024
1 parent 5f4592d commit 877da76
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use cargo_util::{paths, ProcessBuilder};
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::hash_map::{Entry, HashMap};
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::str::{self, FromStr};

Expand Down Expand Up @@ -588,6 +590,10 @@ impl TargetInfo {
.iter()
.any(|sup| sup.as_str() == split.as_str())
}

pub fn get_target_envs(&self) -> &BTreeMap<String, Option<OsString>> {
return self.crate_type_process.get_envs();
}
}

/// Takes rustc output (using specialized command line args), and calculates the file prefix and
Expand Down
30 changes: 26 additions & 4 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ mod dirty_reason;

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

use std::collections::BTreeMap;
use std::env;
use std::ffi::OsString;
use std::hash::{self, Hash, Hasher};
use std::io;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -772,10 +774,18 @@ impl LocalFingerprint {
// TODO: This is allowed at this moment. Should figure out if it makes
// sense if permitting to read env from the config system.
#[allow(clippy::disallowed_methods)]
fn from_env<K: AsRef<str>>(key: K) -> LocalFingerprint {
fn from_env<K: AsRef<str>>(
key: K,
envs: &BTreeMap<String, Option<OsString>>,
) -> LocalFingerprint {
let key = key.as_ref();
let var = key.to_owned();
let val = env::var(key).ok();
let val = envs
.get(key)
.map(|v| v.to_owned())
.or_else(|| Some(env::var_os(key)))
.and_then(|os_str| os_str?.into_string().ok());

LocalFingerprint::RerunIfEnvChanged { var, val }
}

Expand Down Expand Up @@ -1608,6 +1618,12 @@ fn build_script_local_fingerprints(
bool,
) {
assert!(unit.mode.is_run_custom_build());
let envs = build_runner
.bcx
.target_data
.info(unit.kind)
.get_target_envs()
.clone();
// First up, if this build script is entirely overridden, then we just
// return the hash of what we overrode it with. This is the easy case!
if let Some(fingerprint) = build_script_override_fingerprint(build_runner, unit) {
Expand Down Expand Up @@ -1660,7 +1676,12 @@ fn build_script_local_fingerprints(
// Ok so now we're in "new mode" where we can have files listed as
// dependencies as well as env vars listed as dependencies. Process
// them all here.
Ok(Some(local_fingerprints_deps(deps, &target_dir, &pkg_root)))
Ok(Some(local_fingerprints_deps(
deps,
&target_dir,
&pkg_root,
&envs,
)))
};

// Note that `false` == "not overridden"
Expand Down Expand Up @@ -1695,6 +1716,7 @@ fn local_fingerprints_deps(
deps: &BuildDeps,
target_root: &Path,
pkg_root: &Path,
envs: &BTreeMap<String, Option<OsString>>,
) -> Vec<LocalFingerprint> {
debug!("new local fingerprints deps {:?}", pkg_root);
let mut local = Vec::new();
Expand All @@ -1719,7 +1741,7 @@ fn local_fingerprints_deps(
local.extend(
deps.rerun_if_env_changed
.iter()
.map(LocalFingerprint::from_env),
.map(|v| LocalFingerprint::from_env(v, &envs)),
);

local
Expand Down
12 changes: 2 additions & 10 deletions tests/testsuite/build_script_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,12 @@ fn rerun_if_env_changes_config() {
"#,
);

p.cargo("check")
.with_stderr_data(str![[r#"
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();

p.cargo("clean").run();
p.cargo("check")
.with_status(101)
.with_stderr_data(
"\
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[ERROR] failed to run custom build command for `foo v0.1.0 ([..])`
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
...",
)
.run();
Expand Down

0 comments on commit 877da76

Please sign in to comment.