uv_distribution/metadata/
mod.rs

1use std::collections::BTreeMap;
2use std::path::{Path, PathBuf};
3
4use thiserror::Error;
5
6use uv_auth::CredentialsCache;
7use uv_configuration::NoSources;
8use uv_distribution_types::{GitSourceUrl, IndexLocations, Requirement};
9use uv_normalize::{ExtraName, GroupName, PackageName};
10use uv_pep440::{Version, VersionSpecifiers};
11use uv_pypi_types::{HashDigests, ResolutionMetadata};
12use uv_workspace::dependency_groups::DependencyGroupError;
13use uv_workspace::{WorkspaceCache, WorkspaceError};
14
15pub use crate::metadata::build_requires::{BuildRequires, LoweredExtraBuildDependencies};
16pub use crate::metadata::dependency_groups::SourcedDependencyGroups;
17pub use crate::metadata::lowering::LoweredRequirement;
18pub use crate::metadata::lowering::LoweringError;
19pub use crate::metadata::requires_dist::{FlatRequiresDist, RequiresDist};
20
21mod build_requires;
22mod dependency_groups;
23mod lowering;
24mod requires_dist;
25
26#[derive(Debug, Error)]
27pub enum MetadataError {
28    #[error(transparent)]
29    Workspace(#[from] WorkspaceError),
30    #[error(transparent)]
31    DependencyGroup(#[from] DependencyGroupError),
32    #[error("No pyproject.toml found at: {0}")]
33    MissingPyprojectToml(PathBuf),
34    #[error("Failed to parse entry: `{0}`")]
35    LoweringError(PackageName, #[source] Box<LoweringError>),
36    #[error("Failed to parse entry in group `{0}`: `{1}`")]
37    GroupLoweringError(GroupName, PackageName, #[source] Box<LoweringError>),
38    #[error(
39        "Source entry for `{0}` only applies to extra `{1}`, but the `{1}` extra does not exist. When an extra is present on a source (e.g., `extra = \"{1}\"`), the relevant package must be included in the `project.optional-dependencies` section for that extra (e.g., `project.optional-dependencies = {{ \"{1}\" = [\"{0}\"] }}`)."
40    )]
41    MissingSourceExtra(PackageName, ExtraName),
42    #[error(
43        "Source entry for `{0}` only applies to extra `{1}`, but `{0}` was not found under the `project.optional-dependencies` section for that extra. When an extra is present on a source (e.g., `extra = \"{1}\"`), the relevant package must be included in the `project.optional-dependencies` section for that extra (e.g., `project.optional-dependencies = {{ \"{1}\" = [\"{0}\"] }}`)."
44    )]
45    IncompleteSourceExtra(PackageName, ExtraName),
46    #[error(
47        "Source entry for `{0}` only applies to dependency group `{1}`, but the `{1}` group does not exist. When a group is present on a source (e.g., `group = \"{1}\"`), the relevant package must be included in the `dependency-groups` section for that extra (e.g., `dependency-groups = {{ \"{1}\" = [\"{0}\"] }}`)."
48    )]
49    MissingSourceGroup(PackageName, GroupName),
50    #[error(
51        "Source entry for `{0}` only applies to dependency group `{1}`, but `{0}` was not found under the `dependency-groups` section for that group. When a group is present on a source (e.g., `group = \"{1}\"`), the relevant package must be included in the `dependency-groups` section for that extra (e.g., `dependency-groups = {{ \"{1}\" = [\"{0}\"] }}`)."
52    )]
53    IncompleteSourceGroup(PackageName, GroupName),
54}
55
56#[derive(Debug, Clone)]
57pub struct Metadata {
58    // Mandatory fields
59    pub name: PackageName,
60    pub version: Version,
61    // Optional fields
62    pub requires_dist: Box<[Requirement]>,
63    pub requires_python: Option<VersionSpecifiers>,
64    pub provides_extra: Box<[ExtraName]>,
65    pub dependency_groups: BTreeMap<GroupName, Box<[Requirement]>>,
66    pub dynamic: bool,
67}
68
69impl Metadata {
70    /// Lower without considering `tool.uv` in `pyproject.toml`, used for index and other archive
71    /// dependencies.
72    pub fn from_metadata23(metadata: ResolutionMetadata) -> Self {
73        Self {
74            name: metadata.name,
75            version: metadata.version,
76            requires_dist: Box::into_iter(metadata.requires_dist)
77                .map(Requirement::from)
78                .collect(),
79            requires_python: metadata.requires_python,
80            provides_extra: metadata.provides_extra,
81            dependency_groups: BTreeMap::default(),
82            dynamic: metadata.dynamic,
83        }
84    }
85
86    /// Lower by considering `tool.uv` in `pyproject.toml` if present, used for Git and directory
87    /// dependencies.
88    pub async fn from_workspace(
89        metadata: ResolutionMetadata,
90        install_path: &Path,
91        git_source: Option<&GitWorkspaceMember<'_>>,
92        locations: &IndexLocations,
93        sources: NoSources,
94        cache: &WorkspaceCache,
95        credentials_cache: &CredentialsCache,
96    ) -> Result<Self, MetadataError> {
97        // Lower the requirements.
98        let requires_dist = uv_pypi_types::RequiresDist {
99            name: metadata.name,
100            requires_dist: metadata.requires_dist,
101            provides_extra: metadata.provides_extra,
102            dynamic: metadata.dynamic,
103        };
104        let RequiresDist {
105            name,
106            requires_dist,
107            provides_extra,
108            dependency_groups,
109            dynamic,
110        } = RequiresDist::from_project_maybe_workspace(
111            requires_dist,
112            install_path,
113            git_source,
114            locations,
115            sources,
116            cache,
117            credentials_cache,
118        )
119        .await?;
120
121        // Combine with the remaining metadata.
122        Ok(Self {
123            name,
124            version: metadata.version,
125            requires_dist,
126            requires_python: metadata.requires_python,
127            provides_extra,
128            dependency_groups,
129            dynamic,
130        })
131    }
132}
133
134/// The metadata associated with an archive.
135#[derive(Debug, Clone)]
136pub struct ArchiveMetadata {
137    /// The [`Metadata`] for the underlying distribution.
138    pub metadata: Metadata,
139    /// The hashes of the source or built archive.
140    pub hashes: HashDigests,
141}
142
143impl ArchiveMetadata {
144    /// Lower without considering `tool.uv` in `pyproject.toml`, used for index and other archive
145    /// dependencies.
146    pub fn from_metadata23(metadata: ResolutionMetadata) -> Self {
147        Self {
148            metadata: Metadata::from_metadata23(metadata),
149            hashes: HashDigests::empty(),
150        }
151    }
152}
153
154impl From<Metadata> for ArchiveMetadata {
155    fn from(metadata: Metadata) -> Self {
156        Self {
157            metadata,
158            hashes: HashDigests::empty(),
159        }
160    }
161}
162
163/// A workspace member from a checked-out Git repo.
164#[derive(Debug, Clone)]
165pub struct GitWorkspaceMember<'a> {
166    /// The root of the checkout, which may be the root of the workspace or may be above the
167    /// workspace root.
168    pub fetch_root: &'a Path,
169    pub git_source: &'a GitSourceUrl<'a>,
170}