diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 37bf4008fdb..5192d2cadb8 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -146,6 +146,10 @@ impl<'cfg> Compilation<'cfg> { let cargo_exe = self.config.cargo_exe()?; cmd.env(::CARGO_ENV, cargo_exe); + // When adding new environment variables depending on + // crate properties which might require rebuild upon change + // consider adding the corresponding properties to the hash + // in Context::target_metadata() cmd.env("CARGO_MANIFEST_DIR", pkg.root()) .env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string()) .env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string()) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 756022dbc74..b5d01cd3efd 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -399,6 +399,13 @@ impl<'a, 'cfg> Context<'a, 'cfg> { // to pull crates from anywhere w/o worrying about conflicts unit.pkg.package_id().hash(&mut hasher); + // Add package properties which map to environment variables + // exposed by Cargo + let manifest_metadata = unit.pkg.manifest().metadata(); + manifest_metadata.authors.hash(&mut hasher); + manifest_metadata.description.hash(&mut hasher); + manifest_metadata.homepage.hash(&mut hasher); + // Also mix in enabled features to our metadata. This'll ensure that // when changing feature sets each lib is separately cached. self.resolve.features_sorted(unit.pkg.package_id()).hash(&mut hasher); diff --git a/tests/freshness.rs b/tests/freshness.rs index fc95cd88376..eb7532b3490 100644 --- a/tests/freshness.rs +++ b/tests/freshness.rs @@ -680,3 +680,44 @@ fn rebuild_if_build_artifacts_move_forward_in_time() { [FINISHED] [..] ")); } + +#[test] +fn rebuild_if_environment_changes() { + let p = project("env_change") + .file("Cargo.toml", r#" + [package] + name = "env_change" + description = "old desc" + version = "0.0.1" + authors = [] + "#) + .file("src/main.rs", r#" + fn main() { + println!("{}", env!("CARGO_PKG_DESCRIPTION")); + } + "#); + + assert_that(p.cargo_process("run"), + execs().with_status(0) + .with_stdout("old desc").with_stderr(&format!("\ +[COMPILING] env_change v0.0.1 ({dir}) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[RUNNING] `target[/]debug[/]env_change[EXE]` +", dir = p.url()))); + + File::create(&p.root().join("Cargo.toml")).unwrap().write_all(br#" + [package] + name = "env_change" + description = "new desc" + version = "0.0.1" + authors = [] + "#).unwrap(); + + assert_that(p.cargo("run"), + execs().with_status(0) + .with_stdout("new desc").with_stderr(&format!("\ +[COMPILING] env_change v0.0.1 ({dir}) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +[RUNNING] `target[/]debug[/]env_change[EXE]` +", dir = p.url()))); +}