From ef95d79bfa67256fcb71797450759ae0cefe7f5b Mon Sep 17 00:00:00 2001 From: Ankit Saini <74284503+nkitsaini@users.noreply.github.com> Date: Thu, 27 Feb 2025 19:18:43 +0530 Subject: [PATCH] Fix version string truncation while generating cache_key (#11830) ## Summary Follow up for https://github.com/astral-sh/uv/pull/11738 I missed this while reviewing the truncation changes. `format!("{:.N}", value)` only truncates if the `fmt::Display` implementation supports it (by reading `f.precision()` in trait implementation). So in our case `format!("{:.N}", version.to_string())` will work but not `format!("{:.N}", version)` unless `Version` supports it. Since we only need it once, I am just truncating after the string is created. ## Test Plan --- crates/uv-distribution-filename/src/wheel.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/uv-distribution-filename/src/wheel.rs b/crates/uv-distribution-filename/src/wheel.rs index bc37bf707..4db9bea80 100644 --- a/crates/uv-distribution-filename/src/wheel.rs +++ b/crates/uv-distribution-filename/src/wheel.rs @@ -124,7 +124,10 @@ impl WheelFilename { // Truncate the version, but avoid trailing dots, plus signs, etc. to avoid ambiguity. let version_width = CACHE_KEY_MAX_LEN - 1 /* dash */ - 16 /* digest */; - let version = format!("{:.version_width$}", self.version); + let mut version = self.version.to_string(); + + // PANIC SAFETY: version strings can only contain ASCII characters. + version.truncate(version_width); let version = version.trim_end_matches(['.', '+']); format!("{version}-{digest}") @@ -499,8 +502,8 @@ mod tests { // Larger versions should get truncated. let filename = WheelFilename::from_str( - "example-1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9.0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" + "example-1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9.0.1.2.1.2.3.4.5.6.7.8.9.0.1.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl" ).unwrap(); - insta::assert_snapshot!(filename.cache_key(), @"1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9.0.1.2-80bf8598e9647cf7"); + insta::assert_snapshot!(filename.cache_key(), @"1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9.0.1.2.1.2-80bf8598e9647cf7"); } }