diff --git a/crates/uv-resolver/src/pubgrub/package.rs b/crates/uv-resolver/src/pubgrub/package.rs index c54bee394..350960148 100644 --- a/crates/uv-resolver/src/pubgrub/package.rs +++ b/crates/uv-resolver/src/pubgrub/package.rs @@ -132,6 +132,18 @@ impl PubGrubPackage { } } + /// Returns the name of this PubGrub package, if it is not the root package or a Python version + /// constraint. + pub(crate) fn name_no_root(&self) -> Option<&PackageName> { + match &**self { + PubGrubPackageInner::Root(_) | PubGrubPackageInner::Python(_) => None, + PubGrubPackageInner::Package { name, .. } + | PubGrubPackageInner::Extra { name, .. } + | PubGrubPackageInner::Dev { name, .. } + | PubGrubPackageInner::Marker { name, .. } => Some(name), + } + } + /// Returns the marker expression associated with this PubGrub package, if /// it has one. pub(crate) fn marker(&self) -> Option<&MarkerTree> { diff --git a/crates/uv-resolver/src/pubgrub/priority.rs b/crates/uv-resolver/src/pubgrub/priority.rs index f105a36e3..d424622a4 100644 --- a/crates/uv-resolver/src/pubgrub/priority.rs +++ b/crates/uv-resolver/src/pubgrub/priority.rs @@ -31,53 +31,49 @@ impl PubGrubPriorities { urls: &ForkUrls, ) { let next = self.0.len(); - match &**package { - PubGrubPackageInner::Root(_) => {} - PubGrubPackageInner::Python(_) => {} + // The root package and Python constraints have no explicit priority, the root package is + // always first and the Python version (range) is fixed. + let Some(name) = package.name_no_root() else { + return; + }; - PubGrubPackageInner::Marker { name, .. } - | PubGrubPackageInner::Extra { name, .. } - | PubGrubPackageInner::Dev { name, .. } - | PubGrubPackageInner::Package { name, .. } => { - match self.0.entry(name.clone()) { - std::collections::hash_map::Entry::Occupied(mut entry) => { - // Preserve the original index. - let index = match entry.get() { - PubGrubPriority::Unspecified(Reverse(index)) => *index, - PubGrubPriority::Singleton(Reverse(index)) => *index, - PubGrubPriority::DirectUrl(Reverse(index)) => *index, - PubGrubPriority::Root => next, - }; + match self.0.entry(name.clone()) { + std::collections::hash_map::Entry::Occupied(mut entry) => { + // Preserve the original index. + let index = match entry.get() { + PubGrubPriority::Unspecified(Reverse(index)) => *index, + PubGrubPriority::Singleton(Reverse(index)) => *index, + PubGrubPriority::DirectUrl(Reverse(index)) => *index, + PubGrubPriority::Root => next, + }; - // Compute the priority. - let priority = if urls.get(name).is_some() { - PubGrubPriority::DirectUrl(Reverse(index)) - } else if version.as_singleton().is_some() { - PubGrubPriority::Singleton(Reverse(index)) - } else { - PubGrubPriority::Unspecified(Reverse(index)) - }; + // Compute the priority. + let priority = if urls.get(name).is_some() { + PubGrubPriority::DirectUrl(Reverse(index)) + } else if version.as_singleton().is_some() { + PubGrubPriority::Singleton(Reverse(index)) + } else { + PubGrubPriority::Unspecified(Reverse(index)) + }; - // Take the maximum of the new and existing priorities. - if priority > *entry.get() { - entry.insert(priority); - } - } - std::collections::hash_map::Entry::Vacant(entry) => { - // Compute the priority. - let priority = if urls.get(name).is_some() { - PubGrubPriority::DirectUrl(Reverse(next)) - } else if version.as_singleton().is_some() { - PubGrubPriority::Singleton(Reverse(next)) - } else { - PubGrubPriority::Unspecified(Reverse(next)) - }; - - // Insert the priority. - entry.insert(priority); - } + // Take the maximum of the new and existing priorities. + if priority > *entry.get() { + entry.insert(priority); } } + std::collections::hash_map::Entry::Vacant(entry) => { + // Compute the priority. + let priority = if urls.get(name).is_some() { + PubGrubPriority::DirectUrl(Reverse(next)) + } else if version.as_singleton().is_some() { + PubGrubPriority::Singleton(Reverse(next)) + } else { + PubGrubPriority::Unspecified(Reverse(next)) + }; + + // Insert the priority. + entry.insert(priority); + } } } diff --git a/crates/uv-resolver/src/resolver/mod.rs b/crates/uv-resolver/src/resolver/mod.rs index d6958f192..9d5c377a9 100644 --- a/crates/uv-resolver/src/resolver/mod.rs +++ b/crates/uv-resolver/src/resolver/mod.rs @@ -667,43 +667,31 @@ impl ResolverState, request_sink: &Sender, ) -> Result<(), ResolveError> { - match (&**package, url) { - (PubGrubPackageInner::Root(_), _) => {} - (PubGrubPackageInner::Python(_), _) => {} - ( - PubGrubPackageInner::Marker { name, .. } - | PubGrubPackageInner::Extra { name, .. } - | PubGrubPackageInner::Dev { name, .. } - | PubGrubPackageInner::Package { name, .. }, - None, - ) => { - // Verify that the package is allowed under the hash-checking policy. - if !self.hasher.allows_package(name) { - return Err(ResolveError::UnhashedPackage(name.clone())); - } + // Only request real package + let Some(name) = package.name_no_root() else { + return Ok(()); + }; - // Emit a request to fetch the metadata for this package. - if self.index.packages().register(name.clone()) { - request_sink.blocking_send(Request::Package(name.clone()))?; - } + if let Some(url) = url { + // Verify that the package is allowed under the hash-checking policy. + if !self.hasher.allows_url(&url.verbatim) { + return Err(ResolveError::UnhashedPackage(name.clone())); } - ( - PubGrubPackageInner::Marker { name, .. } - | PubGrubPackageInner::Extra { name, .. } - | PubGrubPackageInner::Dev { name, .. } - | PubGrubPackageInner::Package { name, .. }, - Some(url), - ) => { - // Verify that the package is allowed under the hash-checking policy. - if !self.hasher.allows_url(&url.verbatim) { - return Err(ResolveError::UnhashedPackage(name.clone())); - } - // Emit a request to fetch the metadata for this distribution. - let dist = Dist::from_url(name.clone(), url.clone())?; - if self.index.distributions().register(dist.version_id()) { - request_sink.blocking_send(Request::Dist(dist))?; - } + // Emit a request to fetch the metadata for this distribution. + let dist = Dist::from_url(name.clone(), url.clone())?; + if self.index.distributions().register(dist.version_id()) { + request_sink.blocking_send(Request::Dist(dist))?; + } + } else { + // Verify that the package is allowed under the hash-checking policy. + if !self.hasher.allows_package(name) { + return Err(ResolveError::UnhashedPackage(name.clone())); + } + + // Emit a request to fetch the metadata for this package. + if self.index.packages().register(name.clone()) { + request_sink.blocking_send(Request::Package(name.clone()))?; } } Ok(())