Skip to content

Commit d70ca21

Browse files
committed
Include package props with env vars into target metadata
Previously, when changing package properties with corresponding environment variables (such as authors, which has CARGO_PKG_AUTHORS), it didn't invalidate the build, even though there could have been a dependency on such variables in the source code. This commit includes such properties (there are 3 of them in total: authors, description and homepage) in the target metadata. Fixes #3696.
1 parent 3cac894 commit d70ca21

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/cargo/ops/cargo_rustc/compilation.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ impl<'cfg> Compilation<'cfg> {
146146
let cargo_exe = self.config.cargo_exe()?;
147147
cmd.env(::CARGO_ENV, cargo_exe);
148148

149+
// When adding new environment variables depending on
150+
// crate properties which might require rebuild upon change
151+
// consider adding the corresponding properties to the hash
152+
// in Context::target_metadata()
149153
cmd.env("CARGO_MANIFEST_DIR", pkg.root())
150154
.env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string())
151155
.env("CARGO_PKG_VERSION_MINOR", &pkg.version().minor.to_string())

src/cargo/ops/cargo_rustc/context.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
399399
// to pull crates from anywhere w/o worrying about conflicts
400400
unit.pkg.package_id().hash(&mut hasher);
401401

402+
// Add package properties which map to environment variables
403+
// exposed by Cargo
404+
let manifest_metadata = unit.pkg.manifest().metadata();
405+
manifest_metadata.authors.hash(&mut hasher);
406+
manifest_metadata.description.hash(&mut hasher);
407+
manifest_metadata.homepage.hash(&mut hasher);
408+
402409
// Also mix in enabled features to our metadata. This'll ensure that
403410
// when changing feature sets each lib is separately cached.
404411
self.resolve.features_sorted(unit.pkg.package_id()).hash(&mut hasher);

tests/freshness.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,44 @@ fn rebuild_if_build_artifacts_move_forward_in_time() {
680680
[FINISHED] [..]
681681
"));
682682
}
683+
684+
#[test]
685+
fn rebuild_if_environment_changes() {
686+
let p = project("env_change")
687+
.file("Cargo.toml", r#"
688+
[package]
689+
name = "env_change"
690+
description = "old desc"
691+
version = "0.0.1"
692+
authors = []
693+
"#)
694+
.file("src/main.rs", r#"
695+
fn main() {
696+
println!("{}", env!("CARGO_PKG_DESCRIPTION"));
697+
}
698+
"#);
699+
700+
assert_that(p.cargo_process("run"),
701+
execs().with_status(0)
702+
.with_stdout("old desc").with_stderr(&format!("\
703+
[COMPILING] env_change v0.0.1 ({dir})
704+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
705+
[RUNNING] `target[/]debug[/]env_change[EXE]`
706+
", dir = p.url())));
707+
708+
File::create(&p.root().join("Cargo.toml")).unwrap().write_all(br#"
709+
[package]
710+
name = "env_change"
711+
description = "new desc"
712+
version = "0.0.1"
713+
authors = []
714+
"#).unwrap();
715+
716+
assert_that(p.cargo("run"),
717+
execs().with_status(0)
718+
.with_stdout("new desc").with_stderr(&format!("\
719+
[COMPILING] env_change v0.0.1 ({dir})
720+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
721+
[RUNNING] `target[/]debug[/]env_change[EXE]`
722+
", dir = p.url())));
723+
}

0 commit comments

Comments
 (0)