Skip to content

Commit 15d4960

Browse files
committed
Avoid colliding with older Cargo fingerprint changes
The fingerprint format Cargo stores changed recently in a way that older Cargos cannot understand. Unfortunately though older Cargos are colliding on some compilation units trying to read the new format and they're bailing out. This commit fixes this issue by ensuring that the location for fingerprint metadata is different in older Cargos and newer Cargos. Fingerprint metadata is always stored in a location with a hash in the file name. This hash typically includes the hash of rustc's version information itself, but for units which don't have a `Metadata` it's a much simpler hash which is much more likely to collide with other versions of Cargo. The fix in this commit is to extract the metadata version that we're hashing to a constant, and then also hash it for generating a filesystem location to house fingerprint data for a unit that has no `Metadata`. Closes #8472
1 parent 4f74d9b commit 15d4960

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Uni
1313
use crate::core::{Target, TargetKind, Workspace};
1414
use crate::util::{self, CargoResult, StableHasher};
1515

16+
/// This is a generic version number that can be changed to make
17+
/// backwards-incompatible changes to any file structures in the output
18+
/// directory. For example, the fingerprint files or the build-script
19+
/// output files. Normally cargo updates ship with rustc updates which will
20+
/// cause a new hash due to the rustc version changing, but this allows
21+
/// cargo to be extra careful to deal with different versions of cargo that
22+
/// use the same rustc version.
23+
const METADATA_VERSION: u8 = 2;
24+
1625
/// The `Metadata` is a hash used to make unique file names for each unit in a
1726
/// build. It is also use for symbol mangling.
1827
///
@@ -162,7 +171,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
162171
/// Used for the metadata when `metadata` returns `None`.
163172
pub fn target_short_hash(&self, unit: &Unit) -> String {
164173
let hashable = unit.pkg.package_id().stable_hash(self.ws.root());
165-
util::short_hash(&hashable)
174+
util::short_hash(&(METADATA_VERSION, hashable))
166175
}
167176

168177
/// Returns the directory where the artifacts for the given unit are
@@ -483,14 +492,7 @@ fn compute_metadata(
483492
}
484493
let mut hasher = StableHasher::new();
485494

486-
// This is a generic version number that can be changed to make
487-
// backwards-incompatible changes to any file structures in the output
488-
// directory. For example, the fingerprint files or the build-script
489-
// output files. Normally cargo updates ship with rustc updates which will
490-
// cause a new hash due to the rustc version changing, but this allows
491-
// cargo to be extra careful to deal with different versions of cargo that
492-
// use the same rustc version.
493-
2.hash(&mut hasher);
495+
METADATA_VERSION.hash(&mut hasher);
494496

495497
// Unique metadata per (name, source, version) triple. This'll allow us
496498
// to pull crates from anywhere without worrying about conflicts.

0 commit comments

Comments
 (0)