File tree 3 files changed +30
-8
lines changed
3 files changed +30
-8
lines changed Original file line number Diff line number Diff line change @@ -676,7 +676,16 @@ impl GlobalContext {
676
676
. to_string( ) ,
677
677
) ,
678
678
( "{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
+ ) ) ;
680
689
format!( "{}{}{}" , & hash[ 0 ..2 ] , std:: path:: MAIN_SEPARATOR , & hash[ 2 ..] )
681
690
} ) ,
682
691
] ;
Original file line number Diff line number Diff line change @@ -35,6 +35,13 @@ pub struct VersionInfo {
35
35
pub description : Option < String > ,
36
36
}
37
37
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
+
38
45
impl fmt:: Display for VersionInfo {
39
46
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
40
47
write ! ( f, "{}" , self . version) ?;
@@ -58,9 +65,7 @@ pub fn version() -> VersionInfo {
58
65
} ;
59
66
}
60
67
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 ( )
64
69
// This is the version set in bootstrap, which we use to match rustc.
65
70
. or_else ( || option_env_str ! ( "CFG_RELEASE" ) )
66
71
. unwrap_or_else ( || {
@@ -100,3 +105,11 @@ pub fn version() -> VersionInfo {
100
105
description,
101
106
}
102
107
}
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
+ }
Original file line number Diff line number Diff line change @@ -610,7 +610,7 @@ fn template_workspace_path_hash() {
610
610
}
611
611
612
612
#[ 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 ( ) {
614
614
let p = project ( )
615
615
. file ( "src/main.rs" , r#"fn main() { println!("Hello, World!") }"# )
616
616
. file (
@@ -661,10 +661,10 @@ fn template_workspace_path_hash_should_not_change_between_cargo_versions() {
661
661
let second_run_build_dir = hash_dir. as_path ( ) . join ( "build-dir" ) ;
662
662
assert_build_dir_layout ( second_run_build_dir. clone ( ) , "debug" ) ;
663
663
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 ! (
666
666
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 "
668
668
) ;
669
669
}
670
670
You can’t perform that action at this time.
0 commit comments