From 2fb8df376946c354ab3cfe0f7e362a51372f4700 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 14 Mar 2024 10:47:16 -0700 Subject: [PATCH] Avoid panicking on cannot-be-a-base URLs (#2461) `path_segments_mut` returns an `Err` for cannot-be-a-base URLs. These won't be valid when we try to fetch them anyway, but we need to avoid a panic. Closes https://github.com/astral-sh/uv/issues/2460. --- crates/cache-key/src/canonical_url.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/cache-key/src/canonical_url.rs b/crates/cache-key/src/canonical_url.rs index 36c15dae4..b45e33747 100644 --- a/crates/cache-key/src/canonical_url.rs +++ b/crates/cache-key/src/canonical_url.rs @@ -21,6 +21,11 @@ impl CanonicalUrl { pub fn new(url: &Url) -> Self { let mut url = url.clone(); + // If the URL cannot be a base, then it's not a valid URL anyway. + if url.cannot_be_a_base() { + return Self(url); + } + // Strip a trailing slash. if url.path().ends_with('/') { url.path_segments_mut().unwrap().pop_if_empty(); @@ -194,6 +199,12 @@ mod tests { )?, ); + // Two URLs that cannot be a base should be considered equal. + assert_eq!( + CanonicalUrl::parse("git+https:://github.com/pypa/sample-namespace-packages.git")?, + CanonicalUrl::parse("git+https:://github.com/pypa/sample-namespace-packages.git")?, + ); + Ok(()) }