@@ -328,7 +328,7 @@ use crate::core::{InternedString, Package};
328328use crate :: util;
329329use crate :: util:: errors:: { CargoResult , CargoResultExt } ;
330330use crate :: util:: paths;
331- use crate :: util:: { internal, profile} ;
331+ use crate :: util:: { internal, profile, ProcessBuilder } ;
332332
333333use super :: custom_build:: BuildDeps ;
334334use super :: job:: {
@@ -1759,6 +1759,7 @@ pub fn translate_dep_info(
17591759 rustc_cwd : & Path ,
17601760 pkg_root : & Path ,
17611761 target_root : & Path ,
1762+ rustc_cmd : & ProcessBuilder ,
17621763 allow_package : bool ,
17631764) -> CargoResult < ( ) > {
17641765 let depinfo = parse_rustc_dep_info ( rustc_dep_info) ?;
@@ -1768,6 +1769,34 @@ pub fn translate_dep_info(
17681769 let mut on_disk_info = OnDiskDepInfo :: default ( ) ;
17691770 on_disk_info. env = depinfo. env ;
17701771
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+
17711800 for file in depinfo. files {
17721801 // The path may be absolute or relative, canonical or not. Make sure
17731802 // it is canonicalized so we are comparing the same kinds of paths.
0 commit comments