uv_types/
traits.rs

1use std::fmt::{Debug, Display, Formatter};
2use std::future::Future;
3use std::ops::Deref;
4use std::path::{Path, PathBuf};
5
6use anyhow::Result;
7use rustc_hash::FxHashSet;
8
9use uv_cache::Cache;
10use uv_configuration::{BuildKind, BuildOptions, BuildOutput, SourceStrategy};
11use uv_distribution_filename::DistFilename;
12use uv_distribution_types::{
13    CachedDist, ConfigSettings, DependencyMetadata, DistributionId, ExtraBuildRequires,
14    ExtraBuildVariables, IndexCapabilities, IndexLocations, InstalledDist, IsBuildBackendError,
15    PackageConfigSettings, Requirement, Resolution, SourceDist,
16};
17use uv_git::GitResolver;
18use uv_normalize::PackageName;
19use uv_python::{Interpreter, PythonEnvironment};
20use uv_workspace::WorkspaceCache;
21
22use crate::{BuildArena, BuildIsolation};
23
24///  Avoids cyclic crate dependencies between resolver, installer and builder.
25///
26/// To resolve the dependencies of a packages, we may need to build one or more source
27/// distributions. To building a source distribution, we need to create a virtual environment from
28/// the same base python as we use for the root resolution, resolve the build requirements
29/// (potentially which nested source distributions, recursing a level deeper), installing
30/// them and then build. The installer, the resolver and the source distribution builder are each in
31/// their own crate. To avoid circular crate dependencies, this type dispatches between the three
32/// crates with its three main methods ([`BuildContext::resolve`], [`BuildContext::install`] and
33/// [`BuildContext::setup_build`]).
34///
35/// The overall main crate structure looks like this:
36///
37/// ```text
38///                    ┌────────────────┐
39///                    │       uv       │
40///                    └───────▲────────┘
41///                            │
42///                            │
43///                    ┌───────┴────────┐
44///         ┌─────────►│  uv-dispatch   │◄─────────┐
45///         │          └───────▲────────┘          │
46///         │                  │                   │
47///         │                  │                   │
48/// ┌───────┴────────┐ ┌───────┴────────┐ ┌────────┴────────────────┐
49/// │  uv-resolver   │ │  uv-installer  │ │    uv-build-frontend    │
50/// └───────▲────────┘ └───────▲────────┘ └────────▲────────────────┘
51///         │                  │                   │
52///         └─────────────┐    │    ┌──────────────┘
53///                    ┌──┴────┴────┴───┐
54///                    │    uv-types    │
55///                    └────────────────┘
56/// ```
57///
58/// Put in a different way, the types here allow `uv-resolver` to depend on `uv-build` and
59/// `uv-build-frontend` to depend on `uv-resolver` without having actual crate dependencies between
60/// them.
61pub trait BuildContext {
62    type SourceDistBuilder: SourceBuildTrait;
63
64    // Note: this function is async deliberately, because downstream code may need to
65    // run async code to get the interpreter, to resolve the Python version.
66    /// Return a reference to the interpreter.
67    fn interpreter(&self) -> impl Future<Output = &Interpreter> + '_;
68
69    /// Return a reference to the cache.
70    fn cache(&self) -> &Cache;
71
72    /// Return a reference to the Git resolver.
73    fn git(&self) -> &GitResolver;
74
75    /// Return a reference to the build arena.
76    fn build_arena(&self) -> &BuildArena<Self::SourceDistBuilder>;
77
78    /// Return a reference to the discovered registry capabilities.
79    fn capabilities(&self) -> &IndexCapabilities;
80
81    /// Return a reference to any pre-defined static metadata.
82    fn dependency_metadata(&self) -> &DependencyMetadata;
83
84    /// Whether source distribution building or pre-built wheels is disabled.
85    ///
86    /// This [`BuildContext::setup_build`] calls will fail if builds are disabled.
87    /// This method exists to avoid fetching source distributions if we know we can't build them.
88    fn build_options(&self) -> &BuildOptions;
89
90    /// The isolation mode used for building source distributions.
91    fn build_isolation(&self) -> BuildIsolation<'_>;
92
93    /// The [`ConfigSettings`] used to build distributions.
94    fn config_settings(&self) -> &ConfigSettings;
95
96    /// The [`ConfigSettings`] used to build a specific package.
97    fn config_settings_package(&self) -> &PackageConfigSettings;
98
99    /// Whether to incorporate `tool.uv.sources` when resolving requirements.
100    fn sources(&self) -> SourceStrategy;
101
102    /// The index locations being searched.
103    fn locations(&self) -> &IndexLocations;
104
105    /// Workspace discovery caching.
106    fn workspace_cache(&self) -> &WorkspaceCache;
107
108    /// Get the extra build requirements.
109    fn extra_build_requires(&self) -> &ExtraBuildRequires;
110
111    /// Get the extra build variables.
112    fn extra_build_variables(&self) -> &ExtraBuildVariables;
113
114    /// Resolve the given requirements into a ready-to-install set of package versions.
115    fn resolve<'a>(
116        &'a self,
117        requirements: &'a [Requirement],
118        build_stack: &'a BuildStack,
119    ) -> impl Future<Output = Result<Resolution, impl IsBuildBackendError>> + 'a;
120
121    /// Install the given set of package versions into the virtual environment. The environment must
122    /// use the same base Python as [`BuildContext::interpreter`]
123    fn install<'a>(
124        &'a self,
125        resolution: &'a Resolution,
126        venv: &'a PythonEnvironment,
127        build_stack: &'a BuildStack,
128    ) -> impl Future<Output = Result<Vec<CachedDist>, impl IsBuildBackendError>> + 'a;
129
130    /// Set up a source distribution build by installing the required dependencies. A wrapper for
131    /// `uv_build::SourceBuild::setup`.
132    ///
133    /// For PEP 517 builds, this calls `get_requires_for_build_wheel`.
134    ///
135    /// `version_id` is for error reporting only.
136    /// `dist` is for safety checks and may be null for editable builds.
137    fn setup_build<'a>(
138        &'a self,
139        source: &'a Path,
140        subdirectory: Option<&'a Path>,
141        install_path: &'a Path,
142        version_id: Option<&'a str>,
143        dist: Option<&'a SourceDist>,
144        sources: SourceStrategy,
145        build_kind: BuildKind,
146        build_output: BuildOutput,
147        build_stack: BuildStack,
148    ) -> impl Future<Output = Result<Self::SourceDistBuilder, impl IsBuildBackendError>> + 'a;
149
150    /// Build by calling directly into the uv build backend without PEP 517, if possible.
151    ///
152    /// Checks if the source tree uses uv as build backend. If not, it returns `Ok(None)`, otherwise
153    /// it builds and returns the name of the built file.
154    ///
155    /// `version_id` is for error reporting only.
156    fn direct_build<'a>(
157        &'a self,
158        source: &'a Path,
159        subdirectory: Option<&'a Path>,
160        output_dir: &'a Path,
161        sources: SourceStrategy,
162        build_kind: BuildKind,
163        version_id: Option<&'a str>,
164    ) -> impl Future<Output = Result<Option<DistFilename>, impl IsBuildBackendError>> + 'a;
165}
166
167/// A wrapper for `uv_build::SourceBuild` to avoid cyclical crate dependencies.
168///
169/// You can either call only `wheel()` to build the wheel directly, call only `metadata()` to get
170/// the metadata without performing the actual or first call `metadata()` and then `wheel()`.
171pub trait SourceBuildTrait {
172    /// A wrapper for `uv_build::SourceBuild::get_metadata_without_build`.
173    ///
174    /// For PEP 517 builds, this calls `prepare_metadata_for_build_wheel`
175    ///
176    /// Returns the metadata directory if we're having a PEP 517 build and the
177    /// `prepare_metadata_for_build_wheel` hook exists
178    fn metadata(&mut self) -> impl Future<Output = Result<Option<PathBuf>, AnyErrorBuild>>;
179
180    /// A wrapper for `uv_build::SourceBuild::build`.
181    ///
182    /// For PEP 517 builds, this calls `build_wheel`.
183    ///
184    /// Returns the filename of the built wheel inside the given `wheel_dir`. The filename is a
185    /// string and not a `WheelFilename` because the on disk filename might not be normalized in the
186    /// same way as uv would.
187    fn wheel<'a>(
188        &'a self,
189        wheel_dir: &'a Path,
190    ) -> impl Future<Output = Result<String, AnyErrorBuild>> + 'a;
191}
192
193/// A wrapper for [`uv_installer::SitePackages`]
194pub trait InstalledPackagesProvider: Clone + Send + Sync + 'static {
195    fn iter(&self) -> impl Iterator<Item = &InstalledDist>;
196    fn get_packages(&self, name: &PackageName) -> Vec<&InstalledDist>;
197}
198
199/// An [`InstalledPackagesProvider`] with no packages in it.
200#[derive(Clone)]
201pub struct EmptyInstalledPackages;
202
203impl InstalledPackagesProvider for EmptyInstalledPackages {
204    fn iter(&self) -> impl Iterator<Item = &InstalledDist> {
205        std::iter::empty()
206    }
207
208    fn get_packages(&self, _name: &PackageName) -> Vec<&InstalledDist> {
209        Vec::new()
210    }
211}
212
213/// [`anyhow::Error`]-like wrapper type for [`BuildDispatch`] method return values, that also makes
214/// [`IsBuildBackendError`] work as [`thiserror`] `#[source]`.
215///
216/// The errors types have the same problem as [`BuildDispatch`] generally: The `uv-resolver`,
217/// `uv-installer` and `uv-build-frontend` error types all reference each other:
218/// Resolution and installation may need to build packages, while the build frontend needs to
219/// resolve and install for the PEP 517 build environment.
220///
221/// Usually, [`anyhow::Error`] is opaque error type of choice. In this case though, we error type
222/// that we can inspect on whether it's a build backend error with [`IsBuildBackendError`], and
223/// [`anyhow::Error`] does not allow attaching more traits. The next choice would be
224/// `Box<dyn std::error::Error + IsBuildFrontendError + Send + Sync + 'static>`, but [`thiserror`]
225/// complains about the internal `AsDynError` not being implemented when being used as `#[source]`.
226/// This struct is an otherwise transparent error wrapper that thiserror recognizes.
227pub struct AnyErrorBuild(Box<dyn IsBuildBackendError>);
228
229impl Debug for AnyErrorBuild {
230    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
231        Debug::fmt(&self.0, f)
232    }
233}
234
235impl Display for AnyErrorBuild {
236    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
237        Display::fmt(&self.0, f)
238    }
239}
240
241impl std::error::Error for AnyErrorBuild {
242    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
243        self.0.source()
244    }
245
246    #[allow(deprecated)]
247    fn description(&self) -> &str {
248        self.0.description()
249    }
250
251    #[allow(deprecated)]
252    fn cause(&self) -> Option<&dyn std::error::Error> {
253        self.0.cause()
254    }
255}
256
257impl<T: IsBuildBackendError> From<T> for AnyErrorBuild {
258    fn from(err: T) -> Self {
259        Self(Box::new(err))
260    }
261}
262
263impl Deref for AnyErrorBuild {
264    type Target = dyn IsBuildBackendError;
265
266    fn deref(&self) -> &Self::Target {
267        &*self.0
268    }
269}
270
271/// The stack of packages being built.
272#[derive(Debug, Clone, Default)]
273pub struct BuildStack(FxHashSet<DistributionId>);
274
275impl BuildStack {
276    /// Return an empty stack.
277    pub fn empty() -> Self {
278        Self(FxHashSet::default())
279    }
280
281    pub fn contains(&self, id: &DistributionId) -> bool {
282        self.0.contains(id)
283    }
284
285    /// Push a package onto the stack.
286    pub fn insert(&mut self, id: DistributionId) -> bool {
287        self.0.insert(id)
288    }
289}