Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid enforcing platform compatibility when validating lockfile #7305

Merged
merged 1 commit into from
Sep 11, 2024
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
43 changes: 36 additions & 7 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl Lock {
dist.id.name.clone(),
ResolvedDist::Installable(dist.to_dist(
project.workspace().install_path(),
tags,
TagPolicy::Required(tags),
build_options,
)?),
);
Expand Down Expand Up @@ -1088,7 +1088,11 @@ impl Lock {
}

// Get the metadata for the distribution.
let dist = package.to_dist(workspace.install_path(), tags, build_options)?;
let dist = package.to_dist(
workspace.install_path(),
TagPolicy::Preferred(tags),
build_options,
)?;

let Ok(archive) = database
.get_or_build_wheel_metadata(&dist, HashPolicy::None)
Expand Down Expand Up @@ -1201,6 +1205,24 @@ impl Lock {
}
}

#[derive(Debug, Copy, Clone)]
enum TagPolicy<'tags> {
/// Exclusively consider wheels that match the specified platform tags.
Required(&'tags Tags),
/// Prefer wheels that match the specified platform tags, but fall back to incompatible wheels
/// if necessary.
Preferred(&'tags Tags),
}

impl<'tags> TagPolicy<'tags> {
/// Returns the platform tags to consider.
fn tags(&self) -> &'tags Tags {
match self {
TagPolicy::Required(tags) | TagPolicy::Preferred(tags) => tags,
}
}
}

/// The result of checking if a lockfile satisfies a set of requirements.
#[derive(Debug)]
pub enum SatisfiesResult<'lock> {
Expand Down Expand Up @@ -1595,14 +1617,14 @@ impl Package {
fn to_dist(
&self,
workspace_root: &Path,
tags: &Tags,
tag_policy: TagPolicy<'_>,
build_options: &BuildOptions,
) -> Result<Dist, LockError> {
let no_binary = build_options.no_binary_package(&self.id.name);
let no_build = build_options.no_build_package(&self.id.name);

if !no_binary {
if let Some(best_wheel_index) = self.find_best_wheel(tags) {
if let Some(best_wheel_index) = self.find_best_wheel(tag_policy) {
return match &self.id.source {
Source::Registry(source) => {
let wheels = self
Expand Down Expand Up @@ -2016,10 +2038,12 @@ impl Package {
Ok(table)
}

fn find_best_wheel(&self, tags: &Tags) -> Option<usize> {
fn find_best_wheel(&self, tag_policy: TagPolicy<'_>) -> Option<usize> {
let mut best: Option<(TagPriority, usize)> = None;
for (i, wheel) in self.wheels.iter().enumerate() {
let TagCompatibility::Compatible(priority) = wheel.filename.compatibility(tags) else {
let TagCompatibility::Compatible(priority) =
wheel.filename.compatibility(tag_policy.tags())
else {
continue;
};
match best {
Expand All @@ -2033,7 +2057,12 @@ impl Package {
}
}
}
best.map(|(_, i)| i)

let best = best.map(|(_, i)| i);
match tag_policy {
TagPolicy::Required(_) => best,
TagPolicy::Preferred(_) => best.or_else(|| self.wheels.first().map(|_| 0)),
}
}

/// Returns the [`PackageName`] of the package.
Expand Down
2 changes: 0 additions & 2 deletions crates/uv/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,6 @@ fn sync_wheel_url_source_error() -> Result<()> {
----- stdout -----

----- stderr -----
warning: Failed to validate existing lockfile: distribution cffi==1.17.1 @ direct+https://files.pythonhosted.org/packages/08/fd/cc2fedbd887223f9f5d170c96e57cbf655df9831a6546c1727ae13fa977a/cffi-1.17.1-cp310-cp310-macosx_11_0_arm64.whl can't be installed because it doesn't have a source distribution or wheel for the current platform
Resolved 3 packages in [TIME]
error: distribution cffi==1.17.1 @ direct+https://files.pythonhosted.org/packages/08/fd/cc2fedbd887223f9f5d170c96e57cbf655df9831a6546c1727ae13fa977a/cffi-1.17.1-cp310-cp310-macosx_11_0_arm64.whl can't be installed because it doesn't have a source distribution or wheel for the current platform
"###);
Expand Down Expand Up @@ -2243,7 +2242,6 @@ fn sync_wheel_path_source_error() -> Result<()> {
----- stdout -----

----- stderr -----
warning: Failed to validate existing lockfile: distribution cffi==1.17.1 @ path+cffi-1.17.1-cp310-cp310-macosx_11_0_arm64.whl can't be installed because it doesn't have a source distribution or wheel for the current platform
Resolved 3 packages in [TIME]
error: distribution cffi==1.17.1 @ path+cffi-1.17.1-cp310-cp310-macosx_11_0_arm64.whl can't be installed because it doesn't have a source distribution or wheel for the current platform
"###);
Expand Down
Loading