@@ -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 ( ) ;
@@ -803,13 +804,20 @@ impl LocalFingerprint {
803
804
/// Read the environment variable of the given env `key`, and creates a new
804
805
/// [`LocalFingerprint::RerunIfEnvChanged`] for it.
805
806
///
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 .
807
+ /// The `env_config` is used first to check if the env var is set in the config system
808
+ /// as some env needs to be overridden. If not, it will fallback to `std::env::var` .
808
809
#[ allow( clippy:: disallowed_methods) ]
809
- fn from_env < K : AsRef < str > > ( key : K ) -> LocalFingerprint {
810
+ fn from_env < K : AsRef < str > > (
811
+ key : K ,
812
+ env_config : & Arc < HashMap < String , OsString > > ,
813
+ ) -> LocalFingerprint {
810
814
let key = key. as_ref ( ) ;
811
815
let var = key. to_owned ( ) ;
812
- let val = env:: var ( key) . ok ( ) ;
816
+ let val = if let Some ( val) = env_config. get ( key) {
817
+ val. to_str ( ) . map ( ToOwned :: to_owned)
818
+ } else {
819
+ env:: var ( key) . ok ( )
820
+ } ;
813
821
LocalFingerprint :: RerunIfEnvChanged { var, val }
814
822
}
815
823
@@ -1577,7 +1585,7 @@ fn calculate_run_custom_build(
1577
1585
// the build script this means we'll be watching files and env vars.
1578
1586
// Otherwise if we haven't previously executed it we'll just start watching
1579
1587
// the whole crate.
1580
- let ( gen_local, overridden) = build_script_local_fingerprints ( build_runner, unit) ;
1588
+ let ( gen_local, overridden) = build_script_local_fingerprints ( build_runner, unit) ? ;
1581
1589
let deps = & build_runner. build_explicit_deps [ unit] ;
1582
1590
let local = ( gen_local) (
1583
1591
deps,
@@ -1671,7 +1679,7 @@ See https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-change
1671
1679
fn build_script_local_fingerprints (
1672
1680
build_runner : & mut BuildRunner < ' _ , ' _ > ,
1673
1681
unit : & Unit ,
1674
- ) -> (
1682
+ ) -> CargoResult < (
1675
1683
Box <
1676
1684
dyn FnOnce (
1677
1685
& BuildDeps ,
@@ -1680,20 +1688,20 @@ fn build_script_local_fingerprints(
1680
1688
+ Send ,
1681
1689
> ,
1682
1690
bool ,
1683
- ) {
1691
+ ) > {
1684
1692
assert ! ( unit. mode. is_run_custom_build( ) ) ;
1685
1693
// First up, if this build script is entirely overridden, then we just
1686
1694
// return the hash of what we overrode it with. This is the easy case!
1687
1695
if let Some ( fingerprint) = build_script_override_fingerprint ( build_runner, unit) {
1688
1696
debug ! ( "override local fingerprints deps {}" , unit. pkg) ;
1689
- return (
1697
+ return Ok ( (
1690
1698
Box :: new (
1691
1699
move |_: & BuildDeps , _: Option < & dyn Fn ( ) -> CargoResult < String > > | {
1692
1700
Ok ( Some ( vec ! [ fingerprint] ) )
1693
1701
} ,
1694
1702
) ,
1695
1703
true , // this is an overridden build script
1696
- ) ;
1704
+ ) ) ;
1697
1705
}
1698
1706
1699
1707
// ... Otherwise this is a "real" build script and we need to return a real
@@ -1705,6 +1713,7 @@ fn build_script_local_fingerprints(
1705
1713
// obvious.
1706
1714
let pkg_root = unit. pkg . root ( ) . to_path_buf ( ) ;
1707
1715
let target_dir = target_root ( build_runner) ;
1716
+ let env_config = Arc :: clone ( build_runner. bcx . gctx . env_config ( ) ?) ;
1708
1717
let calculate =
1709
1718
move |deps : & BuildDeps , pkg_fingerprint : Option < & dyn Fn ( ) -> CargoResult < String > > | {
1710
1719
if deps. rerun_if_changed . is_empty ( ) && deps. rerun_if_env_changed . is_empty ( ) {
@@ -1734,11 +1743,16 @@ fn build_script_local_fingerprints(
1734
1743
// Ok so now we're in "new mode" where we can have files listed as
1735
1744
// dependencies as well as env vars listed as dependencies. Process
1736
1745
// them all here.
1737
- Ok ( Some ( local_fingerprints_deps ( deps, & target_dir, & pkg_root) ) )
1746
+ Ok ( Some ( local_fingerprints_deps (
1747
+ deps,
1748
+ & target_dir,
1749
+ & pkg_root,
1750
+ & env_config,
1751
+ ) ) )
1738
1752
} ;
1739
1753
1740
1754
// Note that `false` == "not overridden"
1741
- ( Box :: new ( calculate) , false )
1755
+ Ok ( ( Box :: new ( calculate) , false ) )
1742
1756
}
1743
1757
1744
1758
/// Create a [`LocalFingerprint`] for an overridden build script.
@@ -1769,6 +1783,7 @@ fn local_fingerprints_deps(
1769
1783
deps : & BuildDeps ,
1770
1784
target_root : & Path ,
1771
1785
pkg_root : & Path ,
1786
+ env_config : & Arc < HashMap < String , OsString > > ,
1772
1787
) -> Vec < LocalFingerprint > {
1773
1788
debug ! ( "new local fingerprints deps {:?}" , pkg_root) ;
1774
1789
let mut local = Vec :: new ( ) ;
@@ -1793,7 +1808,7 @@ fn local_fingerprints_deps(
1793
1808
local. extend (
1794
1809
deps. rerun_if_env_changed
1795
1810
. iter ( )
1796
- . map ( LocalFingerprint :: from_env) ,
1811
+ . map ( |s| LocalFingerprint :: from_env ( s , env_config ) ) ,
1797
1812
) ;
1798
1813
1799
1814
local
0 commit comments