|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use anyhow::{Context, Result}; |
| 4 | +use futures::{StreamExt, TryStreamExt}; |
| 5 | + |
| 6 | +use distribution_types::{BuildableSource, Dist}; |
| 7 | +use pep508_rs::{Requirement, VersionOrUrl}; |
| 8 | +use uv_client::RegistryClient; |
| 9 | +use uv_distribution::{Reporter, SourceDistCachedBuilder}; |
| 10 | +use uv_types::{BuildContext, RequestedRequirements}; |
| 11 | + |
| 12 | +/// A resolver for resolving lookahead requirements from local dependencies. |
| 13 | +/// |
| 14 | +/// The resolver extends certain privileges to "first-party" requirements. For example, first-party |
| 15 | +/// requirements are allowed to contain direct URL references, local version specifiers, and more. |
| 16 | +/// |
| 17 | +/// We make an exception for transitive requirements of _local_ dependencies. For example, |
| 18 | +/// `pip install .` should treat the dependencies of `.` as if they were first-party dependencies. |
| 19 | +/// This matches our treatment of editable installs (`pip install -e .`). |
| 20 | +/// |
| 21 | +/// The lookahead resolver resolves requirements for local dependencies, so that the resolver can |
| 22 | +/// treat them as first-party dependencies for the purpose of analyzing their specifiers. |
| 23 | +pub struct LookaheadResolver<'a> { |
| 24 | + /// The requirements for the project. |
| 25 | + requirements: &'a [Requirement], |
| 26 | + /// The reporter to use when building source distributions. |
| 27 | + reporter: Option<Arc<dyn Reporter>>, |
| 28 | +} |
| 29 | + |
| 30 | +impl<'a> LookaheadResolver<'a> { |
| 31 | + /// Instantiate a new [`LookaheadResolver`] for a given set of `source_trees`. |
| 32 | + pub fn new(requirements: &'a [Requirement]) -> Self { |
| 33 | + Self { |
| 34 | + requirements, |
| 35 | + reporter: None, |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// Set the [`Reporter`] to use for this resolver. |
| 40 | + #[must_use] |
| 41 | + pub fn with_reporter(self, reporter: impl Reporter + 'static) -> Self { |
| 42 | + let reporter: Arc<dyn Reporter> = Arc::new(reporter); |
| 43 | + Self { |
| 44 | + reporter: Some(reporter), |
| 45 | + ..self |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// Resolve the requirements from the provided source trees. |
| 50 | + pub async fn resolve<T: BuildContext>( |
| 51 | + self, |
| 52 | + context: &T, |
| 53 | + client: &RegistryClient, |
| 54 | + ) -> Result<Vec<RequestedRequirements>> { |
| 55 | + let requirements: Vec<_> = futures::stream::iter(self.requirements.iter()) |
| 56 | + .map(|requirement| async { self.lookahead(requirement, context, client).await }) |
| 57 | + .buffered(50) |
| 58 | + .try_collect() |
| 59 | + .await?; |
| 60 | + Ok(requirements.into_iter().flatten().collect()) |
| 61 | + } |
| 62 | + |
| 63 | + /// Infer the package name for a given "unnamed" requirement. |
| 64 | + async fn lookahead<T: BuildContext>( |
| 65 | + &self, |
| 66 | + requirement: &Requirement, |
| 67 | + context: &T, |
| 68 | + client: &RegistryClient, |
| 69 | + ) -> Result<Option<RequestedRequirements>> { |
| 70 | + // Determine whether the requirement represents a local distribution. |
| 71 | + let Some(VersionOrUrl::Url(url)) = requirement.version_or_url.as_ref() else { |
| 72 | + return Ok(None); |
| 73 | + }; |
| 74 | + |
| 75 | + // Convert to a buildable distribution. |
| 76 | + let dist = Dist::from_url(requirement.name.clone(), url.clone())?; |
| 77 | + |
| 78 | + // Only support source trees (and not, e.g., wheels). |
| 79 | + let Dist::Source(source_dist) = &dist else { |
| 80 | + return Ok(None); |
| 81 | + }; |
| 82 | + if !source_dist.as_path().is_some_and(std::path::Path::is_dir) { |
| 83 | + return Ok(None); |
| 84 | + } |
| 85 | + |
| 86 | + // Run the PEP 517 build process to extract metadata from the source distribution. |
| 87 | + let builder = if let Some(reporter) = self.reporter.clone() { |
| 88 | + SourceDistCachedBuilder::new(context, client).with_reporter(reporter) |
| 89 | + } else { |
| 90 | + SourceDistCachedBuilder::new(context, client) |
| 91 | + }; |
| 92 | + |
| 93 | + let metadata = builder |
| 94 | + .download_and_build_metadata(&BuildableSource::Dist(source_dist)) |
| 95 | + .await |
| 96 | + .context("Failed to build source distribution")?; |
| 97 | + |
| 98 | + // Return the requirements from the metadata. |
| 99 | + Ok(Some(RequestedRequirements::new( |
| 100 | + requirement.extras.clone(), |
| 101 | + metadata.requires_dist, |
| 102 | + ))) |
| 103 | + } |
| 104 | +} |
0 commit comments