uv_distribution/metadata/
mod.rs

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