Skip to content

Commit b424f90

Browse files
committed
Don't use the full pathname of a custom target json in fingerprint hashes.
This makes it easier to get reproducible builds for custom targets.
1 parent 8fd9f82 commit b424f90

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

src/cargo/core/compiler/compile_kind.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,20 @@ impl CompileTarget {
182182
/// See [`CompileKind::fingerprint_hash`].
183183
pub fn fingerprint_hash(&self) -> u64 {
184184
let mut hasher = StableHasher::new();
185-
self.name.hash(&mut hasher);
186-
if self.name.ends_with(".json") {
187-
// This may have some performance concerns, since it is called
188-
// fairly often. If that ever seems worth fixing, consider
189-
// embedding this in `CompileTarget`.
190-
if let Ok(contents) = fs::read_to_string(self.name) {
185+
match self
186+
.name
187+
.ends_with(".json")
188+
.then(|| fs::read_to_string(self.name))
189+
{
190+
Some(Ok(contents)) => {
191+
// This may have some performance concerns, since it is called
192+
// fairly often. If that ever seems worth fixing, consider
193+
// embedding this in `CompileTarget`.
191194
contents.hash(&mut hasher);
192195
}
196+
_ => {
197+
self.name.hash(&mut hasher);
198+
}
193199
}
194200
hasher.finish()
195201
}

src/cargo/core/compiler/context/compilation_files.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,12 @@ fn compute_metadata(
587587
unit.mode.hash(&mut hasher);
588588
cx.lto[unit].hash(&mut hasher);
589589

590-
// Artifacts compiled for the host should have a different metadata
591-
// piece than those compiled for the target, so make sure we throw in
592-
// the unit's `kind` as well
593-
unit.kind.hash(&mut hasher);
590+
// Artifacts compiled for the host should have a different
591+
// metadata piece than those compiled for the target, so make sure
592+
// we throw in the unit's `kind` as well. Use `fingerprint_hash`
593+
// so that the StableHash doesn't change based on the pathnames
594+
// of the custom target JSON spec files.
595+
unit.kind.fingerprint_hash().hash(&mut hasher);
594596

595597
// Finally throw in the target name/kind. This ensures that concurrent
596598
// compiles of targets in the same crate don't collide.

tests/testsuite/custom_target.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,42 @@ fn changing_spec_relearns_crate_types() {
234234
)
235235
.run();
236236
}
237+
238+
#[cargo_test]
239+
fn custom_target_ignores_filepath() {
240+
// Changing the path of the .json file will not trigger a rebuild.
241+
if !is_nightly() {
242+
// Requires features no_core, lang_items
243+
return;
244+
}
245+
let p = project()
246+
.file(
247+
"src/lib.rs",
248+
&"
249+
__MINIMAL_LIB__
250+
251+
pub fn foo() -> u32 {
252+
42
253+
}
254+
"
255+
.replace("__MINIMAL_LIB__", MINIMAL_LIB),
256+
)
257+
.file("b/custom-target.json", SIMPLE_SPEC)
258+
.file("a/custom-target.json", SIMPLE_SPEC)
259+
.build();
260+
261+
// Should build the library the first time.
262+
p.cargo("build --lib --target a/custom-target.json")
263+
.with_stderr(
264+
"\
265+
[..]Compiling foo v0.0.1 ([..])
266+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
267+
",
268+
)
269+
.run();
270+
271+
// But not the second time, even though the path to the custom target is dfferent.
272+
p.cargo("build --lib --target b/custom-target.json")
273+
.with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
274+
.run();
275+
}

0 commit comments

Comments
 (0)