@@ -328,7 +328,7 @@ use crate::core::{InternedString, Package};
328
328
use crate :: util;
329
329
use crate :: util:: errors:: { CargoResult , CargoResultExt } ;
330
330
use crate :: util:: paths;
331
- use crate :: util:: { internal, profile} ;
331
+ use crate :: util:: { internal, profile, ProcessBuilder } ;
332
332
333
333
use super :: custom_build:: BuildDeps ;
334
334
use super :: job:: {
@@ -1759,6 +1759,7 @@ pub fn translate_dep_info(
1759
1759
rustc_cwd : & Path ,
1760
1760
pkg_root : & Path ,
1761
1761
target_root : & Path ,
1762
+ rustc_cmd : & ProcessBuilder ,
1762
1763
allow_package : bool ,
1763
1764
) -> CargoResult < ( ) > {
1764
1765
let depinfo = parse_rustc_dep_info ( rustc_dep_info) ?;
@@ -1768,6 +1769,34 @@ pub fn translate_dep_info(
1768
1769
let mut on_disk_info = OnDiskDepInfo :: default ( ) ;
1769
1770
on_disk_info. env = depinfo. env ;
1770
1771
1772
+ // This is a bit of a tricky statement, but here we're *removing* the
1773
+ // dependency on environment variables that were defined specifically for
1774
+ // the command itself. Environment variables returend by `get_envs` includes
1775
+ // environment variables like:
1776
+ //
1777
+ // * `OUT_DIR` if applicable
1778
+ // * env vars added by a build script, if any
1779
+ //
1780
+ // The general idea here is that the dep info file tells us what, when
1781
+ // changed, should cause us to rebuild the crate. These environment
1782
+ // variables are synthesized by Cargo and/or the build script, and the
1783
+ // intention is that their values are tracked elsewhere for whether the
1784
+ // crate needs to be rebuilt.
1785
+ //
1786
+ // For example a build script says when it needs to be rerun and otherwise
1787
+ // it's assumed to produce the same output, so we're guaranteed that env
1788
+ // vars defined by the build script will always be the same unless the build
1789
+ // script itself reruns, in which case the crate will rerun anyway.
1790
+ //
1791
+ // For things like `OUT_DIR` it's a bit sketchy for now. Most of the time
1792
+ // that's used for code generation but this is technically buggy where if
1793
+ // you write a binary that does `println!("{}", env!("OUT_DIR"))` we won't
1794
+ // recompile that if you move the target directory. Hopefully that's not too
1795
+ // bad of an issue for now...
1796
+ on_disk_info
1797
+ . env
1798
+ . retain ( |( key, _) | !rustc_cmd. get_envs ( ) . contains_key ( key) ) ;
1799
+
1771
1800
for file in depinfo. files {
1772
1801
// The path may be absolute or relative, canonical or not. Make sure
1773
1802
// it is canonicalized so we are comparing the same kinds of paths.
0 commit comments