uv-resolver 0.0.101

This is an internal component crate of uv
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::future::Future;
use std::sync::Arc;
pub use uv_resolver_types::MetadataResponse;
pub(crate) use uv_resolver_types::MetadataUnavailable;
use uv_resolver_types::VariantsJsonMetadataError;

use uv_client::MetadataFormat;
use uv_configuration::BuildOptions;
use uv_distribution::{DistributionDatabase, Reporter};
use uv_distribution_types::{
    BuiltDist, Dist, IndexCapabilities, IndexLocations, IndexMetadata, IndexMetadataRef, IndexUrl,
    InstalledDist, MinimumLibcVersion, RequestedDist, RequiresPython, VariantsJsonContent,
    VerbatimFile,
};
use uv_normalize::PackageName;
use uv_platform_tags::Tags;
use uv_static::EnvVars;
use uv_types::{BuildContext, HashStrategy};

use crate::ExcludeNewer;
use crate::flat_index::{FlatDistributions, FlatIndex};
use crate::version_map::VersionMap;
use crate::yanks::AllowedYanks;

pub type PackageVersionsResult = Result<VersionsResponse, uv_client::Error>;
pub type WheelMetadataResult = Result<MetadataResponse, uv_distribution::Error>;

/// The outcome of an index-level variant metadata lookup.
///
/// A recoverable failure belongs in [`Self::Unavailable`] so the resolver can store it and replay
/// it when it revisits the package version. Fatal client errors are returned separately and abort
/// resolution.
#[derive(Debug)]
pub enum VariantsJsonResponse {
    /// The variant metadata was fetched successfully.
    Found(Vec<u8>),
    /// The package version cannot be used.
    Unavailable(MetadataUnavailable),
}

/// The response when requesting versions for a package
#[derive(Debug)]
pub enum VersionsResponse {
    /// The package was found in the registry with the included versions
    Found(Vec<VersionMap>),
    /// The package was not found in the registry
    NotFound,
    /// The package was not found in the local registry
    NoIndex,
    /// The package was not found in the cache and the network is not available.
    Offline,
}

pub trait ResolverProvider {
    /// Get the version map for a package.
    fn get_package_versions<'io>(
        &'io self,
        package_name: &'io PackageName,
        index: Option<&'io IndexMetadata>,
    ) -> impl Future<Output = PackageVersionsResult> + 'io;

    /// Get the metadata for a distribution.
    ///
    /// For a wheel, this is done by querying it (remote) metadata. For a source distribution, we
    /// (fetch and) build the source distribution and return the metadata from the built
    /// distribution.
    fn get_or_build_wheel_metadata<'io>(
        &'io self,
        dist: &'io Dist,
    ) -> impl Future<Output = WheelMetadataResult> + 'io;

    /// Get embedded variant metadata for a wheel being considered by the solver.
    fn get_wheel_variant_metadata<'io>(
        &'io self,
        wheel: &'io BuiltDist,
    ) -> impl Future<Output = Result<Option<VariantsJsonContent>, uv_distribution::Error>> + 'io;

    /// Get the metadata for an installed distribution.
    fn get_installed_metadata<'io>(
        &'io self,
        dist: &'io InstalledDist,
    ) -> impl Future<Output = WheelMetadataResult> + 'io;

    /// Fetch the index-level variant metadata for a package version.
    fn fetch_variants_json<'io>(
        &'io self,
        package_name: &'io PackageName,
        file: &'io VerbatimFile,
        index: &'io IndexUrl,
    ) -> impl Future<Output = Result<VariantsJsonResponse, uv_client::Error>> + 'io;

    /// Set the [`Reporter`] to use for this installer.
    #[must_use]
    fn with_reporter(self, reporter: Arc<dyn Reporter>) -> Self;
}

/// The main IO backend for the resolver, which does cached requests network requests using the
/// [`RegistryClient`] and [`DistributionDatabase`].
pub struct DefaultResolverProvider<'a, Context: BuildContext> {
    /// The [`DistributionDatabase`] used to build source distributions.
    fetcher: DistributionDatabase<'a, Context>,
    /// These are the entries from `--find-links` that act as overrides for index responses.
    flat_index: &'a FlatIndex,
    tags: Option<Tags>,
    requires_python: RequiresPython,
    allowed_yanks: AllowedYanks,
    hasher: &'a HashStrategy,
    exclude_newer: ExcludeNewer,
    available_version_cutoff: Option<jiff::Timestamp>,
    index_locations: &'a IndexLocations,
    build_options: &'a BuildOptions,
    capabilities: &'a IndexCapabilities,
    minimum_libc_version: Option<MinimumLibcVersion>,
}

impl<'a, Context: BuildContext> DefaultResolverProvider<'a, Context> {
    /// Reads the flat index entries and builds the provider.
    pub fn new(
        fetcher: DistributionDatabase<'a, Context>,
        flat_index: &'a FlatIndex,
        tags: Option<&'a Tags>,
        requires_python: &'a RequiresPython,
        allowed_yanks: AllowedYanks,
        hasher: &'a HashStrategy,
        exclude_newer: ExcludeNewer,
        index_locations: &'a IndexLocations,
        build_options: &'a BuildOptions,
        capabilities: &'a IndexCapabilities,
        minimum_libc_version: Option<MinimumLibcVersion>,
    ) -> Self {
        Self {
            fetcher,
            flat_index,
            tags: tags.cloned(),
            requires_python: requires_python.clone(),
            allowed_yanks,
            hasher,
            exclude_newer,
            available_version_cutoff: std::env::var(
                EnvVars::UV_INTERNAL__TEST_AVAILABLE_VERSION_CUTOFF,
            )
            .ok()
            .and_then(|value| value.parse().ok()),
            index_locations,
            build_options,
            capabilities,
            minimum_libc_version,
        }
    }

    fn effective_exclude_newer(
        &self,
        package_name: &PackageName,
        index: &uv_distribution_types::IndexUrl,
    ) -> Option<jiff::Timestamp> {
        self.exclude_newer.exclude_newer_package_for_index(
            package_name,
            self.index_locations.exclude_newer_for(index, package_name),
        )
    }

    /// Honor index-specific status codes when classifying metadata failures.
    fn classify_metadata_error(
        &self,
        error: uv_client::Error,
        index: Option<&IndexUrl>,
    ) -> Result<MetadataUnavailable, uv_client::Error> {
        if let uv_client::ErrorKind::WrappedReqwestError(_, http) = error.kind()
            && let Some(status) = http.status()
            && index.is_some_and(|index| {
                self.index_locations
                    .request_policy(index)
                    .ignores_error_code(status)
            })
        {
            return Ok(MetadataUnavailable::Network(status));
        }
        classify_metadata_error(error)
    }
}

/// Classify a metadata failure shared by index and direct URL requests.
pub(super) fn classify_metadata_error(
    error: uv_client::Error,
) -> Result<MetadataUnavailable, uv_client::Error> {
    let retries = error.retries();
    let duration = error.duration();
    match error.into_kind() {
        uv_client::ErrorKind::Offline(_) => Ok(MetadataUnavailable::Offline),
        uv_client::ErrorKind::MetadataParseError(_, _, error) => {
            Ok(MetadataUnavailable::InvalidMetadata(Arc::new(*error)))
        }
        uv_client::ErrorKind::Metadata(_, error) => Ok(classify_wheel_metadata_error(error)),
        kind => Err(uv_client::Error::new(kind, retries, duration)),
    }
}

/// Classify errors reading metadata from a wheel archive.
pub(super) fn classify_wheel_metadata_error(error: uv_metadata::Error) -> MetadataUnavailable {
    match error {
        uv_metadata::Error::InvalidVariantMetadata(source) => {
            MetadataUnavailable::InvalidVariantMetadata(Arc::new(VariantsJsonMetadataError::Wheel(
                source,
            )))
        }
        error => MetadataUnavailable::InvalidStructure(Arc::new(error)),
    }
}

impl<Context: BuildContext> ResolverProvider for DefaultResolverProvider<'_, Context> {
    /// Make a "Simple API" request for the package and convert the result to a [`VersionMap`].
    async fn get_package_versions<'io>(
        &'io self,
        package_name: &'io PackageName,
        index: Option<&'io IndexMetadata>,
    ) -> PackageVersionsResult {
        let result = self
            .fetcher
            .client()
            .manual(|client, semaphore| {
                client.simple_detail(
                    package_name,
                    index.map(IndexMetadataRef::from),
                    self.capabilities,
                    semaphore,
                )
            })
            .await;

        // If a package is pinned to an explicit index, ignore any `--find-links` entries.
        let flat_index = index.is_none().then_some(self.flat_index);
        let flat_distributions = flat_index
            .and_then(|flat_index| flat_index.get(package_name))
            .map(|entries| {
                FlatDistributions::from_entries(
                    entries.iter().cloned(),
                    self.tags.as_ref(),
                    self.hasher,
                    self.build_options,
                    self.minimum_libc_version,
                )
            });

        match result {
            Ok(results) => Ok(VersionsResponse::Found(
                results
                    .into_iter()
                    .map(|(index, metadata)| {
                        let included_version_cutoff =
                            self.effective_exclude_newer(package_name, index);
                        let available_version_cutoff = included_version_cutoff
                            .is_none()
                            .then_some(self.available_version_cutoff)
                            .flatten();

                        match metadata {
                            MetadataFormat::Simple(metadata) => VersionMap::from_simple_metadata(
                                metadata,
                                package_name,
                                index.clone(),
                                self.tags.clone(),
                                self.requires_python.clone(),
                                self.allowed_yanks.clone(),
                                self.hasher.clone(),
                                included_version_cutoff,
                                available_version_cutoff,
                                flat_distributions.clone(),
                                self.build_options,
                                self.minimum_libc_version,
                            ),
                            MetadataFormat::Flat(metadata) => VersionMap::from_flat_metadata(
                                metadata,
                                self.tags.as_ref(),
                                self.hasher,
                                self.build_options,
                                self.minimum_libc_version,
                            ),
                        }
                    })
                    .collect(),
            )),
            Err(err) => match err.kind() {
                uv_client::ErrorKind::RemotePackageNotFound(_) => {
                    if let Some(flat_index) = flat_distributions {
                        Ok(VersionsResponse::Found(vec![VersionMap::from(flat_index)]))
                    } else {
                        Ok(VersionsResponse::NotFound)
                    }
                }
                uv_client::ErrorKind::NoIndex(_) => {
                    if let Some(flat_index) = flat_distributions {
                        Ok(VersionsResponse::Found(vec![VersionMap::from(flat_index)]))
                    } else if flat_index.is_some_and(FlatIndex::offline) {
                        Ok(VersionsResponse::Offline)
                    } else {
                        Ok(VersionsResponse::NoIndex)
                    }
                }
                uv_client::ErrorKind::Offline(_) => {
                    if let Some(flat_index) = flat_distributions {
                        Ok(VersionsResponse::Found(vec![VersionMap::from(flat_index)]))
                    } else {
                        Ok(VersionsResponse::Offline)
                    }
                }
                _ => Err(err),
            },
        }
    }

    /// Fetch the metadata for a distribution, building it if necessary.
    async fn get_or_build_wheel_metadata<'io>(&'io self, dist: &'io Dist) -> WheelMetadataResult {
        match self
            .fetcher
            .get_or_build_wheel_metadata(dist, self.hasher.metadata_policy(dist))
            .await
        {
            Ok(metadata) => Ok(MetadataResponse::Found(metadata)),
            Err(err) => match err {
                uv_distribution::Error::Client(client) => {
                    // Both outcomes describe a failed request. A recoverable failure lets the
                    // resolver reject the candidate and backtrack; other client errors abort resolution.
                    match self.classify_metadata_error(client, dist.index()) {
                        Ok(reason) => Ok(MetadataResponse::Unavailable(reason)),
                        Err(error) => Err(error.into()),
                    }
                }
                uv_distribution::Error::WheelMetadataVersionMismatch { .. } => {
                    Ok(MetadataResponse::Unavailable(
                        MetadataUnavailable::InconsistentMetadata(Arc::new(err)),
                    ))
                }
                uv_distribution::Error::WheelMetadataNameMismatch { .. } => {
                    Ok(MetadataResponse::Unavailable(
                        MetadataUnavailable::InconsistentMetadata(Arc::new(err)),
                    ))
                }
                uv_distribution::Error::Metadata(err) => Ok(MetadataResponse::Unavailable(
                    MetadataUnavailable::InvalidMetadata(Arc::new(err)),
                )),
                uv_distribution::Error::WheelMetadata(_, err) => Ok(MetadataResponse::Unavailable(
                    classify_wheel_metadata_error(*err),
                )),
                uv_distribution::Error::RequiresPython(requires_python, version) => {
                    Ok(MetadataResponse::Unavailable(
                        MetadataUnavailable::RequiresPython(requires_python, version),
                    ))
                }
                err => Ok(MetadataResponse::Error(
                    Box::new(RequestedDist::Installable(dist.clone())),
                    Arc::new(err),
                )),
            },
        }
    }

    async fn get_wheel_variant_metadata(
        &self,
        wheel: &BuiltDist,
    ) -> Result<Option<VariantsJsonContent>, uv_distribution::Error> {
        self.fetcher
            .get_wheel_variant_metadata(wheel, self.hasher.archive_policy(wheel))
            .await
    }

    /// Return the metadata for an installed distribution.
    async fn get_installed_metadata<'io>(
        &'io self,
        dist: &'io InstalledDist,
    ) -> WheelMetadataResult {
        match self.fetcher.get_installed_metadata(dist).await {
            Ok(metadata) => Ok(MetadataResponse::Found(metadata)),
            Err(err) => Ok(MetadataResponse::Error(
                Box::new(RequestedDist::Installed(dist.clone())),
                Arc::new(err),
            )),
        }
    }

    async fn fetch_variants_json<'io>(
        &'io self,
        package_name: &'io PackageName,
        file: &'io VerbatimFile,
        index: &'io IndexUrl,
    ) -> Result<VariantsJsonResponse, uv_client::Error> {
        match self
            .fetcher
            .client()
            .managed(|client| client.fetch_variants_json(package_name, file, index))
            .await
        {
            Ok(contents) => Ok(VariantsJsonResponse::Found(contents)),
            Err(error) => match self.classify_metadata_error(error, Some(index)) {
                Ok(reason) => Ok(VariantsJsonResponse::Unavailable(reason)),
                Err(error) => Err(error),
            },
        }
    }

    /// Set the [`Reporter`] to use for this installer.
    fn with_reporter(self, reporter: Arc<dyn Reporter>) -> Self {
        Self {
            fetcher: self.fetcher.with_reporter(reporter),
            ..self
        }
    }
}