Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions crates/uv-requirements/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::sync::Arc;
use futures::{TryStreamExt, stream::FuturesOrdered};

use uv_distribution::{DistributionDatabase, Reporter};
use uv_distribution_types::DistributionMetadata;
use uv_distribution_types::Requirement;
use uv_distribution_types::{Identifier, Requirement};
use uv_resolver::{InMemoryIndex, MetadataResponse};
use uv_types::{BuildContext, HashStrategy};

Expand Down Expand Up @@ -78,7 +77,7 @@ impl<'a, Context: BuildContext> ExtrasResolver<'a, Context> {

// Fetch the metadata for the distribution.
let metadata = {
let id = dist.version_id();
let id = dist.distribution_id();
if let Some(archive) = index
.distributions()
.get(&id)
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-requirements/src/lookahead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tracing::trace;

use uv_configuration::{Constraints, Overrides};
use uv_distribution::{DistributionDatabase, Reporter};
use uv_distribution_types::{Dist, DistributionMetadata, Requirement, RequirementSource};
use uv_distribution_types::{Dist, Identifier, Requirement, RequirementSource};
use uv_resolver::{InMemoryIndex, MetadataResponse, ResolverEnvironment};
use uv_types::{BuildContext, HashStrategy, RequestedRequirements};

Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'a, Context: BuildContext> LookaheadResolver<'a, Context> {

// Fetch the metadata for the distribution.
let metadata = {
let id = dist.version_id();
let id = dist.distribution_id();
if self.index.distributions().register(id.clone()) {
// Run the PEP 517 build process to extract metadata from the source distribution.
let archive = self
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-requirements/src/source_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use uv_configuration::ExtrasSpecification;
use uv_distribution::{DistributionDatabase, FlatRequiresDist, Reporter, RequiresDist};
use uv_distribution_types::Requirement;
use uv_distribution_types::{
BuildableSource, DirectorySourceUrl, HashGeneration, HashPolicy, SourceUrl, VersionId,
BuildableSource, DirectorySourceUrl, HashGeneration, HashPolicy, Identifier, SourceUrl,
};
use uv_fs::Simplified;
use uv_normalize::{ExtraName, PackageName};
Expand Down Expand Up @@ -202,7 +202,7 @@ impl<'a, Context: BuildContext> SourceTreeResolver<'a, Context> {

// Fetch the metadata for the distribution.
let metadata = {
let id = VersionId::from_url(source.url());
let id = source.distribution_id();
if self.index.distributions().register(id.clone()) {
// Run the PEP 517 build process to extract metadata from the source distribution.
let source = BuildableSource::Url(source);
Expand Down
6 changes: 3 additions & 3 deletions crates/uv-requirements/src/unnamed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use url::Host;
use uv_distribution::{DistributionDatabase, Reporter};
use uv_distribution_filename::{DistExtension, SourceDistFilename, WheelFilename};
use uv_distribution_types::{
BuildableSource, DirectSourceUrl, DirectorySourceUrl, GitSourceUrl, PathSourceUrl,
RemoteSource, Requirement, SourceUrl, VersionId,
BuildableSource, DirectSourceUrl, DirectorySourceUrl, GitSourceUrl, Identifier, PathSourceUrl,
RemoteSource, Requirement, SourceUrl,
};
use uv_fs::Simplified;
use uv_normalize::PackageName;
Expand Down Expand Up @@ -276,7 +276,7 @@ impl<'a, Context: BuildContext> NamedRequirementsResolver<'a, Context> {

// Fetch the metadata for the distribution.
let name = {
let id = VersionId::from_url(source.url());
let id = source.distribution_id();
if let Some(archive) = index
.distributions()
.get(&id)
Expand Down
10 changes: 5 additions & 5 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ use uv_distribution_filename::{
};
use uv_distribution_types::{
BuiltDist, DependencyMetadata, DirectUrlBuiltDist, DirectUrlSourceDist, DirectorySourceDist,
Dist, DistributionMetadata, FileLocation, GitSourceDist, IndexLocations, IndexMetadata,
IndexUrl, Name, PathBuiltDist, PathSourceDist, RegistryBuiltDist, RegistryBuiltWheel,
RegistrySourceDist, RemoteSource, Requirement, RequirementSource, RequiresPython, ResolvedDist,
Dist, FileLocation, GitSourceDist, Identifier, IndexLocations, IndexMetadata, IndexUrl, Name,
PathBuiltDist, PathSourceDist, RegistryBuiltDist, RegistryBuiltWheel, RegistrySourceDist,
RemoteSource, Requirement, RequirementSource, RequiresPython, ResolvedDist,
SimplifiedMarkerTree, StaticMetadata, ToUrlError, UrlString,
};
use uv_fs::{PortablePath, PortablePathBuf, Simplified, relative_to};
Expand Down Expand Up @@ -1794,7 +1794,7 @@ impl Lock {
)?;

let metadata = {
let id = dist.version_id();
let id = dist.distribution_id();
if let Some(archive) =
index
.distributions()
Expand Down Expand Up @@ -1946,7 +1946,7 @@ impl Lock {
)?;

let metadata = {
let id = dist.version_id();
let id = dist.distribution_id();
if let Some(archive) =
index
.distributions()
Expand Down
53 changes: 46 additions & 7 deletions crates/uv-resolver/src/pins.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
use std::collections::hash_map::Entry;

use rustc_hash::FxHashMap;

use uv_distribution_types::{CompatibleDist, ResolvedDist};
use uv_distribution_types::{CompatibleDist, DistributionId, Identifier, ResolvedDist};
use uv_normalize::PackageName;

use crate::candidate_selector::Candidate;

#[derive(Clone, Debug)]
struct FilePin {
/// The concrete distribution chosen for installation and locking.
dist: ResolvedDist,
/// The concrete distribution whose metadata was used during resolution.
metadata_id: DistributionId,
}

/// A set of package versions pinned to specific files.
///
/// For example, given `Flask==3.0.0`, the [`FilePins`] would contain a mapping from `Flask` to
/// `3.0.0` to the specific wheel or source distribution archive that was pinned for that version.
/// `3.0.0` to the specific wheel or source distribution archive that was pinned for installation,
/// along with the concrete distribution whose metadata was used during resolution.
#[derive(Clone, Debug, Default)]
pub(crate) struct FilePins(FxHashMap<(PackageName, uv_pep440::Version), ResolvedDist>);
pub(crate) struct FilePins(FxHashMap<(PackageName, uv_pep440::Version), FilePin>);

// Inserts are common (every time we select a version) while reads are rare (converting the
// final resolution).
impl FilePins {
/// Pin a candidate package.
pub(crate) fn insert(&mut self, candidate: &Candidate, dist: &CompatibleDist) {
self.0
let pin = FilePin {
dist: dist.for_installation().to_owned(),
metadata_id: dist.for_resolution().distribution_id(),
};
match self
.0
.entry((candidate.name().clone(), candidate.version().clone()))
// Avoid the expensive clone when a version is selected again.
.or_insert_with(|| dist.for_installation().to_owned());
{
Entry::Occupied(mut entry) => {
if entry.get().dist.distribution_id() != pin.dist.distribution_id()
|| entry.get().metadata_id != pin.metadata_id
{
entry.insert(pin);
}
}
Entry::Vacant(entry) => {
entry.insert(pin);
}
}
}

/// Return the pinned file for the given package name and version, if it exists.
Expand All @@ -29,6 +55,19 @@ impl FilePins {
name: &PackageName,
version: &uv_pep440::Version,
) -> Option<&ResolvedDist> {
self.0.get(&(name.clone(), version.clone()))
self.0
.get(&(name.clone(), version.clone()))
.map(|pin| &pin.dist)
}

/// Return the distribution id whose metadata was used during resolution.
pub(crate) fn metadata_id(
&self,
name: &PackageName,
version: &uv_pep440::Version,
) -> Option<&DistributionId> {
self.0
.get(&(name.clone(), version.clone()))
.map(|pin| &pin.metadata_id)
}
}
38 changes: 0 additions & 38 deletions crates/uv-resolver/src/pubgrub/distribution.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/uv-resolver/src/pubgrub/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
pub(crate) use crate::pubgrub::dependencies::PubGrubDependency;
pub(crate) use crate::pubgrub::distribution::PubGrubDistribution;
pub use crate::pubgrub::package::{PubGrubPackage, PubGrubPackageInner, PubGrubPython};
pub(crate) use crate::pubgrub::priority::{PubGrubPriorities, PubGrubPriority, PubGrubTiebreaker};
pub(crate) use crate::pubgrub::report::PubGrubReportFormatter;

mod dependencies;
mod distribution;
mod package;
mod priority;
mod report;
51 changes: 23 additions & 28 deletions crates/uv-resolver/src/resolution/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use uv_configuration::{Constraints, Overrides};
use uv_distribution::Metadata;
use uv_distribution_types::{
Dist, DistributionMetadata, Edge, IndexUrl, Name, Node, Requirement, RequiresPython,
ResolutionDiagnostic, ResolvedDist, VersionId, VersionOrUrlRef,
Dist, DistributionId, Edge, Identifier, IndexUrl, Name, Node, Requirement, RequiresPython,
ResolutionDiagnostic, ResolvedDist,
};
use uv_git::GitResolver;
use uv_normalize::{ExtraName, GroupName, PackageName};
Expand Down Expand Up @@ -414,17 +414,18 @@ impl ResolverOutput {
git: &GitResolver,
) -> Result<(ResolvedDist, HashDigests, Option<Metadata>), ResolveError> {
Ok(if let Some(url) = url {
// Create the distribution.
// Create the locked distribution and recover the metadata using the original URL that
// was requested during resolution.
let dist = Dist::from_url(name.clone(), url_to_precise(url.clone(), git))?;

let version_id = VersionId::from_url(&url.verbatim);
let hashes_id = dist.distribution_id();
let metadata_id = Dist::from_url(name.clone(), url.clone())?.distribution_id();

// Extract the hashes.
let hashes = Self::get_hashes(
name,
index,
Some(url),
&version_id,
&hashes_id,
version,
preferences,
in_memory,
Expand All @@ -434,13 +435,13 @@ impl ResolverOutput {
let metadata = {
let response = in_memory
.distributions()
.get(&version_id)
.get(&metadata_id)
.unwrap_or_else(|| {
panic!("Every URL distribution should have metadata: {version_id:?}")
panic!("Every URL distribution should have metadata: {metadata_id:?}")
});

let MetadataResponse::Found(archive) = &*response else {
panic!("Every URL distribution should have metadata: {version_id:?}")
panic!("Every URL distribution should have metadata: {metadata_id:?}")
};

archive.metadata.clone()
Expand All @@ -459,8 +460,10 @@ impl ResolverOutput {
.get(name, version)
.expect("Every package should be pinned")
.clone();

let version_id = dist.version_id();
let hashes_id = dist.distribution_id();
let metadata_id = pins
.metadata_id(name, version)
.expect("Every package should have pinned metadata");

// Track yanks for any registry distributions.
match dist.yanked() {
Expand All @@ -484,7 +487,7 @@ impl ResolverOutput {
name,
index,
None,
&version_id,
&hashes_id,
version,
preferences,
in_memory,
Expand All @@ -494,7 +497,7 @@ impl ResolverOutput {
let metadata = {
in_memory
.distributions()
.get(&version_id)
.get(metadata_id)
.and_then(|response| {
if let MetadataResponse::Found(archive) = &*response {
Some(archive.metadata.clone())
Expand All @@ -508,13 +511,13 @@ impl ResolverOutput {
})
}

/// Identify the hashes for the [`VersionId`], preserving any hashes that were provided by the
/// lockfile.
/// Identify the hashes for a concrete distribution, preserving any hashes that were provided
/// by the lockfile.
fn get_hashes(
name: &PackageName,
index: Option<&IndexUrl>,
url: Option<&VerbatimParsedUrl>,
version_id: &VersionId,
metadata_id: &DistributionId,
version: &Version,
preferences: &Preferences,
in_memory: &InMemoryIndex,
Expand All @@ -527,7 +530,7 @@ impl ResolverOutput {
}

// 2. Look for hashes for the distribution (i.e., the specific wheel or source distribution).
if let Some(metadata_response) = in_memory.distributions().get(version_id) {
if let Some(metadata_response) = in_memory.distributions().get(metadata_id) {
if let MetadataResponse::Found(ref archive) = *metadata_response {
let mut digests = archive.hashes.clone();
digests.sort_unstable();
Expand Down Expand Up @@ -711,21 +714,13 @@ impl ResolverOutput {
let ResolutionGraphNode::Dist(dist) = &self.graph[i] else {
continue;
};
let version_id = match dist.version_or_url() {
VersionOrUrlRef::Version(version) => {
VersionId::from_registry(dist.name().clone(), version.clone())
}
VersionOrUrlRef::Url(verbatim_url) => VersionId::from_url(verbatim_url.raw()),
};
let metadata_id = dist.dist.distribution_id();
let res = index
.distributions()
.get(&version_id)
.get(&metadata_id)
.expect("every package in resolution graph has metadata");
let MetadataResponse::Found(archive, ..) = &*res else {
panic!(
"Every package should have metadata: {:?}",
dist.version_id()
)
panic!("Every package should have metadata: {metadata_id:?}")
};
for req in self
.constraints
Expand Down
6 changes: 2 additions & 4 deletions crates/uv-resolver/src/resolver/batch_prefetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use crate::resolver::Request;
use crate::{
InMemoryIndex, PythonRequirement, ResolveError, ResolverEnvironment, VersionsResponse,
};
use uv_distribution_types::{
CompatibleDist, DistributionMetadata, IndexCapabilities, IndexMetadata,
};
use uv_distribution_types::{CompatibleDist, Identifier, IndexCapabilities, IndexMetadata};
use uv_normalize::PackageName;
use uv_pep440::Version;
use uv_pep508::MarkerTree;
Expand Down Expand Up @@ -315,7 +313,7 @@ impl BatchPrefetcherRunner {
);
prefetch_count += 1;

if self.index.distributions().register(candidate.version_id()) {
if self.index.distributions().register(dist.distribution_id()) {
let request = Request::from(dist);
self.request_sink.blocking_send(request)?;
}
Expand Down
Loading
Loading