Skip to content

Commit 95491b1

Browse files
committed
feat(build-dir): Added the Cargo version to the inputs of workspace-path-hash
1 parent 6df7be3 commit 95491b1

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/cargo/util/context/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,16 @@ impl GlobalContext {
676676
.to_string(),
677677
),
678678
("{workspace-path-hash}", {
679-
let hash = crate::util::hex::short_hash(&workspace_manifest_path);
679+
// We include the version in the hash to prevent external tools from relying on
680+
// our hash implmentation. Note that we explictly do not include the prerelease
681+
// and build parts of the semver to avoid a new hash for every nightly version.
682+
let version = crate::version::version().semver();
683+
let hash = crate::util::hex::short_hash(&(
684+
version.major,
685+
version.minor,
686+
version.patch,
687+
workspace_manifest_path,
688+
));
680689
format!("{}{}{}", &hash[0..2], std::path::MAIN_SEPARATOR, &hash[2..])
681690
}),
682691
];

src/cargo/version.rs

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ pub struct VersionInfo {
3535
pub description: Option<String>,
3636
}
3737

38+
impl VersionInfo {
39+
/// The Cargo version as a [`semver::Version`].
40+
pub fn semver(&self) -> semver::Version {
41+
semver::Version::parse(&self.version).expect("Cargo version was not a valid semver version")
42+
}
43+
}
44+
3845
impl fmt::Display for VersionInfo {
3946
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4047
write!(f, "{}", self.version)?;

tests/testsuite/build_dir.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ fn template_workspace_path_hash() {
610610
}
611611

612612
#[cargo_test]
613-
fn template_workspace_path_hash_should_not_change_between_cargo_versions() {
613+
fn template_workspace_path_hash_should_change_between_cargo_versions() {
614614
let p = project()
615615
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
616616
.file(
@@ -661,10 +661,10 @@ fn template_workspace_path_hash_should_not_change_between_cargo_versions() {
661661
let second_run_build_dir = hash_dir.as_path().join("build-dir");
662662
assert_build_dir_layout(second_run_build_dir.clone(), "debug");
663663

664-
// Finally check that the build-dir is in the same location between both Cargo versions
665-
assert_eq!(
664+
// Finally check that the build-dir is in different location between both Cargo versions
665+
assert_ne!(
666666
first_run_build_dir, second_run_build_dir,
667-
"The workspace path hash generated between 2 Cargo versions did not match"
667+
"The workspace path hash generated between 2 Cargo versions matched when it should not"
668668
);
669669
}
670670

0 commit comments

Comments
 (0)