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
169 changes: 99 additions & 70 deletions crates/uv-resolver/src/lock/installable.rs

Large diffs are not rendered by default.

67 changes: 58 additions & 9 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
Expand Down Expand Up @@ -331,7 +332,7 @@ pub struct Lock {
/// this map, and that every dependency for every package has an ID
/// that exists in this map. That is, there are no dependencies that don't
/// have a corresponding locked package entry in the same lockfile.
by_id: FxHashMap<PackageId, usize>,
by_id: FxHashMap<PackageId, PackageIndex>,
/// The input requirements to the resolution.
manifest: ResolverManifest,
}
Expand Down Expand Up @@ -1200,8 +1201,8 @@ impl Lock {
// Check for duplicate package IDs and also build up the map for
// packages keyed by their ID.
let mut by_id = FxHashMap::default();
for (i, dist) in packages.iter().enumerate() {
if by_id.insert(dist.id.clone(), i).is_some() {
for (index, dist) in packages.iter().enumerate() {
if by_id.insert(dist.id.clone(), PackageIndex(index)).is_some() {
return Err(LockErrorKind::DuplicatePackage {
id: dist.id.clone(),
}
Expand Down Expand Up @@ -1239,15 +1240,21 @@ impl Lock {
// Check that every dependency has an entry in `by_id`. If any don't,
// it implies we somehow have a dependency with no corresponding locked
// package.
for dist in &packages {
for dependency in dist.all_dependencies() {
if !by_id.contains_key(&dependency.package_id) {
for dist in &mut packages {
for dependency in dist
.dependencies
.iter_mut()
.chain(dist.optional_dependencies.values_mut().flatten())
.chain(dist.dependency_groups.values_mut().flatten())
{
let Some(&index) = by_id.get(&dependency.package_id) else {
return Err(LockErrorKind::UnrecognizedDependency {
id: dist.id.clone(),
dependency: dependency.clone(),
}
.into());
}
};
dependency.index = index;
}

// Also check that our sources are consistent with whether we have
Expand Down Expand Up @@ -2065,7 +2072,7 @@ impl Lock {
fn find_by_id(&self, id: &PackageId) -> &Package {
let index = *self.by_id.get(id).expect("locked package for ID");

(self.packages.get(index).expect("valid index for package")) as _
(self.packages.get(index.0).expect("valid index for package")) as _
}

/// Return a [`SatisfiesResult`] if the given extras do not match the [`Package`] metadata.
Expand Down Expand Up @@ -6282,10 +6289,18 @@ impl TryFrom<WheelWire> for Wheel {
}
}

/// The position of a package in [`Lock::packages`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct PackageIndex(usize);

/// A single dependency of a package in a lockfile.
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Eq)]
pub struct Dependency {
package_id: PackageId,
/// The target's position in [`Lock::packages`], initialized by [`Lock::new`].
/// This cache is excluded from equality and ordering, since the position can differ between
/// locks that contain the same dependency.
index: PackageIndex,
extra: BTreeSet<ExtraName>,
/// A marker simplified from the PEP 508 marker in `complexified_marker`
/// by assuming `requires-python` and the PEP 508 portion of the parent package's reachability
Expand Down Expand Up @@ -6324,6 +6339,7 @@ impl Dependency {
let complexified_marker = simplified_marker.into_marker(requires_python);
Self {
package_id,
index: PackageIndex(0),
extra,
simplified_marker,
complexified_marker: UniversalMarker::from_combined(complexified_marker),
Expand All @@ -6341,6 +6357,38 @@ impl Dependency {
}
}

impl PartialEq for Dependency {
fn eq(&self, other: &Self) -> bool {
self.package_id == other.package_id
&& self.extra == other.extra
&& self.simplified_marker == other.simplified_marker
&& self.complexified_marker == other.complexified_marker
}
}

impl PartialOrd for Dependency {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Dependency {
fn cmp(&self, other: &Self) -> Ordering {
(
&self.package_id,
&self.extra,
&self.simplified_marker,
&self.complexified_marker,
)
.cmp(&(
&other.package_id,
&other.extra,
&other.simplified_marker,
&other.complexified_marker,
))
}
}

impl Display for Dependency {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match (self.extra.is_empty(), self.package_id.version.as_ref()) {
Expand Down Expand Up @@ -6395,6 +6443,7 @@ impl DependencyWire {
};
Ok(Dependency {
package_id: self.package_id.unwire(unambiguous_package_ids)?,
index: PackageIndex(0),
extra: self.extra,
simplified_marker,
complexified_marker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ Ok(
),
),
),
}: 0,
}: PackageIndex(
0,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ Ok(
),
),
),
}: 0,
}: PackageIndex(
0,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ Ok(
source: Path(
"file:///foo/bar",
),
}: 0,
}: PackageIndex(
0,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ Ok(
),
),
},
index: PackageIndex(
0,
),
extra: {},
simplified_marker: SimplifiedMarkerTree(
true,
Expand Down Expand Up @@ -184,7 +187,9 @@ Ok(
),
),
),
}: 0,
}: PackageIndex(
0,
),
PackageId {
name: PackageName(
"b",
Expand All @@ -199,7 +204,9 @@ Ok(
),
),
),
}: 1,
}: PackageIndex(
1,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ Ok(
),
),
},
index: PackageIndex(
0,
),
extra: {},
simplified_marker: SimplifiedMarkerTree(
true,
Expand Down Expand Up @@ -184,7 +187,9 @@ Ok(
),
),
),
}: 0,
}: PackageIndex(
0,
),
PackageId {
name: PackageName(
"b",
Expand All @@ -199,7 +204,9 @@ Ok(
),
),
),
}: 1,
}: PackageIndex(
1,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ Ok(
"path/to/a",
),
},
index: PackageIndex(
0,
),
extra: {},
simplified_marker: SimplifiedMarkerTree(
true,
Expand All @@ -194,7 +197,9 @@ Ok(
source: Editable(
"path/to/a",
),
}: 0,
}: PackageIndex(
0,
),
PackageId {
name: PackageName(
"a",
Expand All @@ -209,7 +214,9 @@ Ok(
),
),
),
}: 1,
}: PackageIndex(
1,
),
PackageId {
name: PackageName(
"b",
Expand All @@ -224,7 +231,9 @@ Ok(
),
),
),
}: 2,
}: PackageIndex(
2,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ Ok(
),
),
},
index: PackageIndex(
0,
),
extra: {},
simplified_marker: SimplifiedMarkerTree(
true,
Expand Down Expand Up @@ -184,7 +187,9 @@ Ok(
),
),
),
}: 0,
}: PackageIndex(
0,
),
PackageId {
name: PackageName(
"b",
Expand All @@ -199,7 +204,9 @@ Ok(
),
),
),
}: 1,
}: PackageIndex(
1,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ Ok(
),
},
),
}: 0,
}: PackageIndex(
0,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ Ok(
subdirectory: None,
},
),
}: 0,
}: PackageIndex(
0,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ Ok(
source: Directory(
"path/to/dir",
),
}: 0,
}: PackageIndex(
0,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ Ok(
source: Editable(
"path/to/dir",
),
}: 0,
}: PackageIndex(
0,
),
},
manifest: ResolverManifest {
members: {},
Expand Down
Loading