Skip to content

Commit feb5de8

Browse files
committed
feat(build-dir): Added the Cargo version to the inputs of workspace-path-hash
1 parent 5251455 commit feb5de8

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
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

+16-3
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)?;
@@ -58,9 +65,7 @@ pub fn version() -> VersionInfo {
5865
};
5966
}
6067

61-
// __CARGO_TEST_CARGO_VERSION is for testing purposes only.
62-
let version = std::env::var("__CARGO_TEST_CARGO_VERSION")
63-
.ok()
68+
let version = version_for_testing()
6469
// This is the version set in bootstrap, which we use to match rustc.
6570
.or_else(|| option_env_str!("CFG_RELEASE"))
6671
.unwrap_or_else(|| {
@@ -100,3 +105,11 @@ pub fn version() -> VersionInfo {
100105
description,
101106
}
102107
}
108+
109+
/// Provides a way for tests to inject an abitrary version for testing purposes.
110+
///
111+
// __CARGO_TEST_CARGO_VERSION should not be relied on outside of tests.
112+
#[allow(clippy::disallowed_methods)]
113+
fn version_for_testing() -> Option<String> {
114+
std::env::var("__CARGO_TEST_CARGO_VERSION").ok()
115+
}

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)