Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Avoid cloning distribution metadata for lock serialization
  • Loading branch information
zaniebot authored and astral-automations-bot[bot] committed Oct 14, 2026
commit 10fdaf4efb19428a96d12b78bd3a335b136ca2c2
41 changes: 30 additions & 11 deletions crates/uv-lock/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2534,9 +2534,13 @@ impl Lock {
// Git declarations can introduce direct sources needed by offline freshness checks.
if metadata_free
&& matches!(package.id.source, Source::Git(..))
&& let Some(metadata) = dist.metadata_for_lock()
&& let Some(metadata) = dist.metadata()
{
package.metadata = PackageMetadata::from_distribution(metadata.as_ref(), root)?;
package.metadata = PackageMetadata::from_distribution(
metadata,
root,
dist.is_project_or_workspace_member,
)?;
}
let mut wheel_marker = dist.marker;
if let Some(supported_environments_marker) = supported_environments_marker {
Expand Down Expand Up @@ -6655,10 +6659,12 @@ impl Package {
let metadata = if id.source.is_immutable() {
PackageMetadata::default()
} else {
let metadata = annotated_dist
.metadata_for_lock()
.expect("metadata is present");
PackageMetadata::from_distribution(metadata.as_ref(), root)?
let metadata = annotated_dist.metadata().expect("metadata is present");
PackageMetadata::from_distribution(
metadata,
root,
annotated_dist.is_project_or_workspace_member,
)?
};
Ok(Self {
id,
Expand Down Expand Up @@ -7447,13 +7453,27 @@ struct PackageMetadata {
}

impl PackageMetadata {
fn from_distribution(metadata: &DistributionMetadata, root: &Path) -> Result<Self, LockError> {
fn from_distribution(
metadata: &DistributionMetadata,
root: &Path,
preserve_path_preference: bool,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an enum for this?

) -> Result<Self, LockError> {
let normalize = uv_preview::is_enabled(PreviewFeature::LockfileNormalization);
// We normally write dependency paths relative to the lockfile. For the current project and
// workspace members, preserve the user's choice of relative or absolute paths instead.
// Metadata from `tool.uv.dependency-metadata` already preserves that choice.
// Apply the path policy to the cloned requirements so shared metadata remains unchanged.
let relative_requirement = |requirement: &Requirement| {
let mut requirement = requirement.clone();
if preserve_path_preference {
requirement.set_force_relative(false);
}
requirement.relative_to(root)
};
let requires_dist = metadata
.requires_dist
.iter()
.cloned()
.map(|requirement| requirement.relative_to(root))
.map(&relative_requirement)
.collect::<Result<Vec<_>, _>>()
.map(|requirements| {
normalize_collection::<_, NormalizedRequirements>(requirements, normalize)
Expand All @@ -7465,8 +7485,7 @@ impl PackageMetadata {
.map(|(group, requirements)| {
let requirements = requirements
.iter()
.cloned()
.map(|requirement| requirement.relative_to(root))
.map(&relative_requirement)
.collect::<Result<Vec<_>, _>>()
.map(|requirements| {
normalize_collection::<_, NormalizedRequirements>(requirements, normalize)
Expand Down
23 changes: 4 additions & 19 deletions crates/uv-resolver-types/src/distribution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::fmt::Display;
use std::sync::Arc;

Expand Down Expand Up @@ -46,8 +45,8 @@ impl PackageNodeKind {
}

/// A pinned package with its resolved distribution and metadata. The [`ResolvedDist`] refers to a
/// specific distribution (e.g., a specific wheel), while the [`Metadata23`] refers to the metadata
/// for the package-version pair.
/// specific distribution (e.g., a specific wheel), while the [`Metadata`] describes
/// the package-version pair.
#[derive(Debug, Clone)]
pub struct AnnotatedDist {
pub dist: ResolvedDist,
Expand All @@ -56,7 +55,8 @@ pub struct AnnotatedDist {
pub kind: PackageNodeKind,
pub hashes: HashDigests,
pub metadata: Option<Arc<MetadataResponse>>,
pub is_workspace_member: bool,
/// Whether this distribution is the current project or a workspace member.
pub is_project_or_workspace_member: bool,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an enum for this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, we should create a separate pull adding this to the code quality review

/// The "full" marker for this distribution. It precisely describes all
/// marker environments for which this distribution _can_ be installed.
/// That is, when doing a traversal over all of the distributions in a
Expand All @@ -77,21 +77,6 @@ impl AnnotatedDist {
})
}

/// Returns the resolved metadata with local path preferences preserved for lock serialization.
pub fn metadata_for_lock(&self) -> Option<Cow<'_, Metadata>> {
let metadata = self.metadata()?;

// We normally write dependency paths relative to the lockfile. For the current project and
// workspace members, preserve the user's choice of relative or absolute paths instead.
// Metadata from `tool.uv.dependency-metadata` already preserves that choice.
// Only change this copy, not shared metadata.
if self.is_workspace_member {
Some(Cow::Owned(metadata.clone().with_force_relative(false)))
} else {
Some(Cow::Borrowed(metadata))
}
}

/// Returns `true` if the [`AnnotatedDist`] is a base package (i.e., not an extra or a
/// dependency group).
pub(crate) fn is_base(&self) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-resolver/src/resolution/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn add_version(
in_memory: &InMemoryIndex,
package: &ResolutionPackage,
selected: SelectedDistribution,
is_workspace_member: bool,
is_project_or_workspace_member: bool,
) -> NodeIndex {
let ResolutionPackage {
name,
Expand Down Expand Up @@ -295,7 +295,7 @@ fn add_version(
kind: kind.clone(),
hashes,
metadata,
is_workspace_member,
is_project_or_workspace_member,
marker: UniversalMarker::TRUE,
}))
}
Expand Down
⌂ 换网址