@@ -374,6 +374,7 @@ mod dirty_reason;
374
374
375
375
use std:: collections:: hash_map:: { Entry , HashMap } ;
376
376
use std:: env;
377
+ use std:: ffi:: OsString ;
377
378
use std:: fs;
378
379
use std:: fs:: File ;
379
380
use std:: hash:: { self , Hash , Hasher } ;
@@ -509,7 +510,7 @@ pub fn prepare_target(
509
510
// thunk we can invoke on a foreign thread to calculate this.
510
511
let build_script_outputs = Arc :: clone ( & build_runner. build_script_outputs ) ;
511
512
let metadata = build_runner. get_run_build_script_metadata ( unit) ;
512
- let ( gen_local, _overridden) = build_script_local_fingerprints ( build_runner, unit) ;
513
+ let ( gen_local, _overridden) = build_script_local_fingerprints ( build_runner, unit) ? ;
513
514
let output_path = build_runner. build_explicit_deps [ unit]
514
515
. build_script_output
515
516
. clone ( ) ;
@@ -801,15 +802,24 @@ pub enum StaleItem {
801
802
802
803
impl LocalFingerprint {
803
804
/// Read the environment variable of the given env `key`, and creates a new
804
- /// [`LocalFingerprint::RerunIfEnvChanged`] for it.
805
+ /// [`LocalFingerprint::RerunIfEnvChanged`] for it. The `env_config` is used firstly
806
+ /// to check if the env var is set in the config system as some envs need to be overridden.
807
+ /// If not, it will fallback to `std::env::var`.
805
808
///
806
- // TODO: This is allowed at this moment. Should figure out if it makes
807
- // sense if permitting to read env from the config system .
809
+ // TODO: `std::env::var` is allowed at this moment. Should figure out
810
+ // if it makes sense if permitting to read env from the env snapshot .
808
811
#[ allow( clippy:: disallowed_methods) ]
809
- fn from_env < K : AsRef < str > > ( key : K ) -> LocalFingerprint {
812
+ fn from_env < K : AsRef < str > > (
813
+ key : K ,
814
+ env_config : & Arc < HashMap < String , OsString > > ,
815
+ ) -> LocalFingerprint {
810
816
let key = key. as_ref ( ) ;
811
817
let var = key. to_owned ( ) ;
812
- let val = env:: var ( key) . ok ( ) ;
818
+ let val = if let Some ( val) = env_config. get ( key) {
819
+ val. to_str ( ) . map ( ToOwned :: to_owned)
820
+ } else {
821
+ env:: var ( key) . ok ( )
822
+ } ;
813
823
LocalFingerprint :: RerunIfEnvChanged { var, val }
814
824
}
815
825
@@ -1577,7 +1587,7 @@ fn calculate_run_custom_build(
1577
1587
// the build script this means we'll be watching files and env vars.
1578
1588
// Otherwise if we haven't previously executed it we'll just start watching
1579
1589
// the whole crate.
1580
- let ( gen_local, overridden) = build_script_local_fingerprints ( build_runner, unit) ;
1590
+ let ( gen_local, overridden) = build_script_local_fingerprints ( build_runner, unit) ? ;
1581
1591
let deps = & build_runner. build_explicit_deps [ unit] ;
1582
1592
let local = ( gen_local) (
1583
1593
deps,
@@ -1671,7 +1681,7 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change
1671
1681
fn build_script_local_fingerprints (
1672
1682
build_runner : & mut BuildRunner < ' _ , ' _ > ,
1673
1683
unit : & Unit ,
1674
- ) -> (
1684
+ ) -> CargoResult < (
1675
1685
Box <
1676
1686
dyn FnOnce (
1677
1687
& BuildDeps ,
@@ -1680,20 +1690,20 @@ fn build_script_local_fingerprints(
1680
1690
+ Send ,
1681
1691
> ,
1682
1692
bool ,
1683
- ) {
1693
+ ) > {
1684
1694
assert ! ( unit. mode. is_run_custom_build( ) ) ;
1685
1695
// First up, if this build script is entirely overridden, then we just
1686
1696
// return the hash of what we overrode it with. This is the easy case!
1687
1697
if let Some ( fingerprint) = build_script_override_fingerprint ( build_runner, unit) {
1688
1698
debug ! ( "override local fingerprints deps {}" , unit. pkg) ;
1689
- return (
1699
+ return Ok ( (
1690
1700
Box :: new (
1691
1701
move |_: & BuildDeps , _: Option < & dyn Fn ( ) -> CargoResult < String > > | {
1692
1702
Ok ( Some ( vec ! [ fingerprint] ) )
1693
1703
} ,
1694
1704
) ,
1695
1705
true , // this is an overridden build script
1696
- ) ;
1706
+ ) ) ;
1697
1707
}
1698
1708
1699
1709
// ... Otherwise this is a "real" build script and we need to return a real
@@ -1705,6 +1715,7 @@ fn build_script_local_fingerprints(
1705
1715
// obvious.
1706
1716
let pkg_root = unit. pkg . root ( ) . to_path_buf ( ) ;
1707
1717
let target_dir = target_root ( build_runner) ;
1718
+ let env_config = Arc :: clone ( build_runner. bcx . gctx . env_config ( ) ?) ;
1708
1719
let calculate =
1709
1720
move |deps : & BuildDeps , pkg_fingerprint : Option < & dyn Fn ( ) -> CargoResult < String > > | {
1710
1721
if deps. rerun_if_changed . is_empty ( ) && deps. rerun_if_env_changed . is_empty ( ) {
@@ -1734,11 +1745,16 @@ fn build_script_local_fingerprints(
1734
1745
// Ok so now we're in "new mode" where we can have files listed as
1735
1746
// dependencies as well as env vars listed as dependencies. Process
1736
1747
// them all here.
1737
- Ok ( Some ( local_fingerprints_deps ( deps, & target_dir, & pkg_root) ) )
1748
+ Ok ( Some ( local_fingerprints_deps (
1749
+ deps,
1750
+ & target_dir,
1751
+ & pkg_root,
1752
+ & env_config,
1753
+ ) ) )
1738
1754
} ;
1739
1755
1740
1756
// Note that `false` == "not overridden"
1741
- ( Box :: new ( calculate) , false )
1757
+ Ok ( ( Box :: new ( calculate) , false ) )
1742
1758
}
1743
1759
1744
1760
/// Create a [`LocalFingerprint`] for an overridden build script.
@@ -1769,6 +1785,7 @@ fn local_fingerprints_deps(
1769
1785
deps : & BuildDeps ,
1770
1786
target_root : & Path ,
1771
1787
pkg_root : & Path ,
1788
+ env_config : & Arc < HashMap < String , OsString > > ,
1772
1789
) -> Vec < LocalFingerprint > {
1773
1790
debug ! ( "new local fingerprints deps {:?}" , pkg_root) ;
1774
1791
let mut local = Vec :: new ( ) ;
@@ -1793,7 +1810,7 @@ fn local_fingerprints_deps(
1793
1810
local. extend (
1794
1811
deps. rerun_if_env_changed
1795
1812
. iter ( )
1796
- . map ( LocalFingerprint :: from_env) ,
1813
+ . map ( |s| LocalFingerprint :: from_env ( s , env_config ) ) ,
1797
1814
) ;
1798
1815
1799
1816
local
0 commit comments