uv_resolver/lock/
mod.rs

1use std::borrow::Cow;
2use std::collections::{BTreeMap, BTreeSet, VecDeque};
3use std::error::Error;
4use std::fmt::{Debug, Display, Formatter};
5use std::io;
6use std::path::{Path, PathBuf};
7use std::str::FromStr;
8use std::sync::{Arc, LazyLock};
9
10use itertools::Itertools;
11use jiff::Timestamp;
12use owo_colors::OwoColorize;
13use petgraph::graph::NodeIndex;
14use petgraph::visit::EdgeRef;
15use rustc_hash::{FxHashMap, FxHashSet};
16use serde::Serializer;
17use toml_edit::{Array, ArrayOfTables, InlineTable, Item, Table, Value, value};
18use tracing::debug;
19use url::Url;
20
21use uv_cache_key::RepositoryUrl;
22use uv_configuration::{BuildOptions, Constraints, InstallTarget};
23use uv_distribution::{DistributionDatabase, FlatRequiresDist};
24use uv_distribution_filename::{
25    BuildTag, DistExtension, ExtensionError, SourceDistExtension, WheelFilename,
26};
27use uv_distribution_types::{
28    BuiltDist, DependencyMetadata, DirectUrlBuiltDist, DirectUrlSourceDist, DirectorySourceDist,
29    Dist, DistributionMetadata, FileLocation, GitSourceDist, IndexLocations, IndexMetadata,
30    IndexUrl, Name, PathBuiltDist, PathSourceDist, RegistryBuiltDist, RegistryBuiltWheel,
31    RegistrySourceDist, RemoteSource, Requirement, RequirementSource, RequiresPython, ResolvedDist,
32    SimplifiedMarkerTree, StaticMetadata, ToUrlError, UrlString,
33};
34use uv_fs::{PortablePath, PortablePathBuf, relative_to};
35use uv_git::{RepositoryReference, ResolvedRepositoryReference};
36use uv_git_types::{GitLfs, GitOid, GitReference, GitUrl, GitUrlParseError};
37use uv_normalize::{ExtraName, GroupName, PackageName};
38use uv_pep440::Version;
39use uv_pep508::{MarkerEnvironment, MarkerTree, VerbatimUrl, VerbatimUrlError, split_scheme};
40use uv_platform_tags::{
41    AbiTag, IncompatibleTag, LanguageTag, PlatformTag, TagCompatibility, TagPriority, Tags,
42};
43use uv_pypi_types::{
44    ConflictKind, Conflicts, HashAlgorithm, HashDigest, HashDigests, Hashes, ParsedArchiveUrl,
45    ParsedGitUrl, PyProjectToml,
46};
47use uv_redacted::{DisplaySafeUrl, DisplaySafeUrlError};
48use uv_small_str::SmallString;
49use uv_types::{BuildContext, HashStrategy};
50use uv_workspace::{Editability, WorkspaceMember};
51
52use crate::exclude_newer::ExcludeNewerSpan;
53use crate::fork_strategy::ForkStrategy;
54pub(crate) use crate::lock::export::PylockTomlPackage;
55pub use crate::lock::export::RequirementsTxtExport;
56pub use crate::lock::export::{PylockToml, PylockTomlErrorKind, cyclonedx_json};
57pub use crate::lock::installable::Installable;
58pub use crate::lock::map::PackageMap;
59pub use crate::lock::tree::TreeDisplay;
60use crate::resolution::{AnnotatedDist, ResolutionGraphNode};
61use crate::universal_marker::{ConflictMarker, UniversalMarker};
62use crate::{
63    ExcludeNewer, ExcludeNewerPackage, ExcludeNewerValue, InMemoryIndex, MetadataResponse,
64    PrereleaseMode, ResolutionMode, ResolverOutput,
65};
66
67mod export;
68mod installable;
69mod map;
70mod tree;
71
72/// The current version of the lockfile format.
73pub const VERSION: u32 = 1;
74
75/// The current revision of the lockfile format.
76const REVISION: u32 = 3;
77
78static LINUX_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
79    let pep508 = MarkerTree::from_str("os_name == 'posix' and sys_platform == 'linux'").unwrap();
80    UniversalMarker::new(pep508, ConflictMarker::TRUE)
81});
82static WINDOWS_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
83    let pep508 = MarkerTree::from_str("os_name == 'nt' and sys_platform == 'win32'").unwrap();
84    UniversalMarker::new(pep508, ConflictMarker::TRUE)
85});
86static MAC_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
87    let pep508 = MarkerTree::from_str("os_name == 'posix' and sys_platform == 'darwin'").unwrap();
88    UniversalMarker::new(pep508, ConflictMarker::TRUE)
89});
90static ANDROID_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
91    let pep508 = MarkerTree::from_str("sys_platform == 'android'").unwrap();
92    UniversalMarker::new(pep508, ConflictMarker::TRUE)
93});
94static ARM_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
95    let pep508 =
96        MarkerTree::from_str("platform_machine == 'aarch64' or platform_machine == 'arm64' or platform_machine == 'ARM64'")
97            .unwrap();
98    UniversalMarker::new(pep508, ConflictMarker::TRUE)
99});
100static X86_64_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
101    let pep508 =
102        MarkerTree::from_str("platform_machine == 'x86_64' or platform_machine == 'amd64' or platform_machine == 'AMD64'")
103            .unwrap();
104    UniversalMarker::new(pep508, ConflictMarker::TRUE)
105});
106static X86_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
107    let pep508 = MarkerTree::from_str(
108        "platform_machine == 'i686' or platform_machine == 'i386' or platform_machine == 'win32' or platform_machine == 'x86'",
109    )
110    .unwrap();
111    UniversalMarker::new(pep508, ConflictMarker::TRUE)
112});
113static LINUX_ARM_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
114    let mut marker = *LINUX_MARKERS;
115    marker.and(*ARM_MARKERS);
116    marker
117});
118static LINUX_X86_64_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
119    let mut marker = *LINUX_MARKERS;
120    marker.and(*X86_64_MARKERS);
121    marker
122});
123static LINUX_X86_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
124    let mut marker = *LINUX_MARKERS;
125    marker.and(*X86_MARKERS);
126    marker
127});
128static WINDOWS_ARM_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
129    let mut marker = *WINDOWS_MARKERS;
130    marker.and(*ARM_MARKERS);
131    marker
132});
133static WINDOWS_X86_64_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
134    let mut marker = *WINDOWS_MARKERS;
135    marker.and(*X86_64_MARKERS);
136    marker
137});
138static WINDOWS_X86_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
139    let mut marker = *WINDOWS_MARKERS;
140    marker.and(*X86_MARKERS);
141    marker
142});
143static MAC_ARM_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
144    let mut marker = *MAC_MARKERS;
145    marker.and(*ARM_MARKERS);
146    marker
147});
148static MAC_X86_64_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
149    let mut marker = *MAC_MARKERS;
150    marker.and(*X86_64_MARKERS);
151    marker
152});
153static MAC_X86_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
154    let mut marker = *MAC_MARKERS;
155    marker.and(*X86_MARKERS);
156    marker
157});
158static ANDROID_ARM_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
159    let mut marker = *ANDROID_MARKERS;
160    marker.and(*ARM_MARKERS);
161    marker
162});
163static ANDROID_X86_64_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
164    let mut marker = *ANDROID_MARKERS;
165    marker.and(*X86_64_MARKERS);
166    marker
167});
168static ANDROID_X86_MARKERS: LazyLock<UniversalMarker> = LazyLock::new(|| {
169    let mut marker = *ANDROID_MARKERS;
170    marker.and(*X86_MARKERS);
171    marker
172});
173
174#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize)]
175#[serde(try_from = "LockWire")]
176pub struct Lock {
177    /// The (major) version of the lockfile format.
178    ///
179    /// Changes to the major version indicate backwards- and forwards-incompatible changes to the
180    /// lockfile format. A given uv version only supports a single major version of the lockfile
181    /// format.
182    ///
183    /// In other words, a version of uv that supports version 2 of the lockfile format will not be
184    /// able to read lockfiles generated under version 1 or 3.
185    version: u32,
186    /// The revision of the lockfile format.
187    ///
188    /// Changes to the revision indicate backwards-compatible changes to the lockfile format.
189    /// In other words, versions of uv that only support revision 1 _will_ be able to read lockfiles
190    /// with a revision greater than 1 (though they may ignore newer fields).
191    revision: u32,
192    /// If this lockfile was built from a forking resolution with non-identical forks, store the
193    /// forks in the lockfile so we can recreate them in subsequent resolutions.
194    fork_markers: Vec<UniversalMarker>,
195    /// The conflicting groups/extras specified by the user.
196    conflicts: Conflicts,
197    /// The list of supported environments specified by the user.
198    supported_environments: Vec<MarkerTree>,
199    /// The list of required platforms specified by the user.
200    required_environments: Vec<MarkerTree>,
201    /// The range of supported Python versions.
202    requires_python: RequiresPython,
203    /// We discard the lockfile if these options don't match.
204    options: ResolverOptions,
205    /// The actual locked version and their metadata.
206    packages: Vec<Package>,
207    /// A map from package ID to index in `packages`.
208    ///
209    /// This can be used to quickly lookup the full package for any ID
210    /// in this lock. For example, the dependencies for each package are
211    /// listed as package IDs. This map can be used to find the full
212    /// package for each such dependency.
213    ///
214    /// It is guaranteed that every package in this lock has an entry in
215    /// this map, and that every dependency for every package has an ID
216    /// that exists in this map. That is, there are no dependencies that don't
217    /// have a corresponding locked package entry in the same lockfile.
218    by_id: FxHashMap<PackageId, usize>,
219    /// The input requirements to the resolution.
220    manifest: ResolverManifest,
221}
222
223impl Lock {
224    /// Initialize a [`Lock`] from a [`ResolverOutput`].
225    pub fn from_resolution(resolution: &ResolverOutput, root: &Path) -> Result<Self, LockError> {
226        let mut packages = BTreeMap::new();
227        let requires_python = resolution.requires_python.clone();
228
229        // Determine the set of packages included at multiple versions.
230        let mut seen = FxHashSet::default();
231        let mut duplicates = FxHashSet::default();
232        for node_index in resolution.graph.node_indices() {
233            let ResolutionGraphNode::Dist(dist) = &resolution.graph[node_index] else {
234                continue;
235            };
236            if !dist.is_base() {
237                continue;
238            }
239            if !seen.insert(dist.name()) {
240                duplicates.insert(dist.name());
241            }
242        }
243
244        // Lock all base packages.
245        for node_index in resolution.graph.node_indices() {
246            let ResolutionGraphNode::Dist(dist) = &resolution.graph[node_index] else {
247                continue;
248            };
249            if !dist.is_base() {
250                continue;
251            }
252
253            // If there are multiple distributions for the same package, include the markers of all
254            // forks that included the current distribution.
255            let fork_markers = if duplicates.contains(dist.name()) {
256                resolution
257                    .fork_markers
258                    .iter()
259                    .filter(|fork_markers| !fork_markers.is_disjoint(dist.marker))
260                    .copied()
261                    .collect()
262            } else {
263                vec![]
264            };
265
266            let mut package = Package::from_annotated_dist(dist, fork_markers, root)?;
267            Self::remove_unreachable_wheels(resolution, &requires_python, node_index, &mut package);
268
269            // Add all dependencies
270            for edge in resolution.graph.edges(node_index) {
271                let ResolutionGraphNode::Dist(dependency_dist) = &resolution.graph[edge.target()]
272                else {
273                    continue;
274                };
275                let marker = *edge.weight();
276                package.add_dependency(&requires_python, dependency_dist, marker, root)?;
277            }
278
279            let id = package.id.clone();
280            if let Some(locked_dist) = packages.insert(id, package) {
281                return Err(LockErrorKind::DuplicatePackage {
282                    id: locked_dist.id.clone(),
283                }
284                .into());
285            }
286        }
287
288        // Lock all extras and development dependencies.
289        for node_index in resolution.graph.node_indices() {
290            let ResolutionGraphNode::Dist(dist) = &resolution.graph[node_index] else {
291                continue;
292            };
293            if let Some(extra) = dist.extra.as_ref() {
294                let id = PackageId::from_annotated_dist(dist, root)?;
295                let Some(package) = packages.get_mut(&id) else {
296                    return Err(LockErrorKind::MissingExtraBase {
297                        id,
298                        extra: extra.clone(),
299                    }
300                    .into());
301                };
302                for edge in resolution.graph.edges(node_index) {
303                    let ResolutionGraphNode::Dist(dependency_dist) =
304                        &resolution.graph[edge.target()]
305                    else {
306                        continue;
307                    };
308                    let marker = *edge.weight();
309                    package.add_optional_dependency(
310                        &requires_python,
311                        extra.clone(),
312                        dependency_dist,
313                        marker,
314                        root,
315                    )?;
316                }
317            }
318            if let Some(group) = dist.group.as_ref() {
319                let id = PackageId::from_annotated_dist(dist, root)?;
320                let Some(package) = packages.get_mut(&id) else {
321                    return Err(LockErrorKind::MissingDevBase {
322                        id,
323                        group: group.clone(),
324                    }
325                    .into());
326                };
327                for edge in resolution.graph.edges(node_index) {
328                    let ResolutionGraphNode::Dist(dependency_dist) =
329                        &resolution.graph[edge.target()]
330                    else {
331                        continue;
332                    };
333                    let marker = *edge.weight();
334                    package.add_group_dependency(
335                        &requires_python,
336                        group.clone(),
337                        dependency_dist,
338                        marker,
339                        root,
340                    )?;
341                }
342            }
343        }
344
345        let packages = packages.into_values().collect();
346
347        let options = ResolverOptions {
348            resolution_mode: resolution.options.resolution_mode,
349            prerelease_mode: resolution.options.prerelease_mode,
350            fork_strategy: resolution.options.fork_strategy,
351            exclude_newer: resolution.options.exclude_newer.clone().into(),
352        };
353        let lock = Self::new(
354            VERSION,
355            REVISION,
356            packages,
357            requires_python,
358            options,
359            ResolverManifest::default(),
360            Conflicts::empty(),
361            vec![],
362            vec![],
363            resolution.fork_markers.clone(),
364        )?;
365        Ok(lock)
366    }
367
368    /// Remove wheels that can't be selected for installation due to environment markers.
369    ///
370    /// For example, a package included under `sys_platform == 'win32'` does not need Linux
371    /// wheels.
372    fn remove_unreachable_wheels(
373        graph: &ResolverOutput,
374        requires_python: &RequiresPython,
375        node_index: NodeIndex,
376        locked_dist: &mut Package,
377    ) {
378        // Remove wheels that don't match `requires-python` and can't be selected for installation.
379        locked_dist
380            .wheels
381            .retain(|wheel| requires_python.matches_wheel_tag(&wheel.filename));
382
383        // Filter by platform tags.
384        locked_dist.wheels.retain(|wheel| {
385            // Naively, we'd check whether `platform_system == 'Linux'` is disjoint, or
386            // `os_name == 'posix'` is disjoint, or `sys_platform == 'linux'` is disjoint (each on its
387            // own sufficient to exclude linux wheels), but due to
388            // `(A ∩ (B ∩ C) = ∅) => ((A ∩ B = ∅) or (A ∩ C = ∅))`
389            // a single disjointness check with the intersection is sufficient, so we have one
390            // constant per platform.
391            let platform_tags = wheel.filename.platform_tags();
392
393            if platform_tags.iter().all(PlatformTag::is_any) {
394                return true;
395            }
396
397            if platform_tags.iter().all(PlatformTag::is_linux) {
398                if platform_tags.iter().all(PlatformTag::is_arm) {
399                    if graph.graph[node_index]
400                        .marker()
401                        .is_disjoint(*LINUX_ARM_MARKERS)
402                    {
403                        return false;
404                    }
405                } else if platform_tags.iter().all(PlatformTag::is_x86_64) {
406                    if graph.graph[node_index]
407                        .marker()
408                        .is_disjoint(*LINUX_X86_64_MARKERS)
409                    {
410                        return false;
411                    }
412                } else if platform_tags.iter().all(PlatformTag::is_x86) {
413                    if graph.graph[node_index]
414                        .marker()
415                        .is_disjoint(*LINUX_X86_MARKERS)
416                    {
417                        return false;
418                    }
419                } else if graph.graph[node_index].marker().is_disjoint(*LINUX_MARKERS) {
420                    return false;
421                }
422            }
423
424            if platform_tags.iter().all(PlatformTag::is_windows) {
425                if platform_tags.iter().all(PlatformTag::is_arm) {
426                    if graph.graph[node_index]
427                        .marker()
428                        .is_disjoint(*WINDOWS_ARM_MARKERS)
429                    {
430                        return false;
431                    }
432                } else if platform_tags.iter().all(PlatformTag::is_x86_64) {
433                    if graph.graph[node_index]
434                        .marker()
435                        .is_disjoint(*WINDOWS_X86_64_MARKERS)
436                    {
437                        return false;
438                    }
439                } else if platform_tags.iter().all(PlatformTag::is_x86) {
440                    if graph.graph[node_index]
441                        .marker()
442                        .is_disjoint(*WINDOWS_X86_MARKERS)
443                    {
444                        return false;
445                    }
446                } else if graph.graph[node_index]
447                    .marker()
448                    .is_disjoint(*WINDOWS_MARKERS)
449                {
450                    return false;
451                }
452            }
453
454            if platform_tags.iter().all(PlatformTag::is_macos) {
455                if platform_tags.iter().all(PlatformTag::is_arm) {
456                    if graph.graph[node_index]
457                        .marker()
458                        .is_disjoint(*MAC_ARM_MARKERS)
459                    {
460                        return false;
461                    }
462                } else if platform_tags.iter().all(PlatformTag::is_x86_64) {
463                    if graph.graph[node_index]
464                        .marker()
465                        .is_disjoint(*MAC_X86_64_MARKERS)
466                    {
467                        return false;
468                    }
469                } else if platform_tags.iter().all(PlatformTag::is_x86) {
470                    if graph.graph[node_index]
471                        .marker()
472                        .is_disjoint(*MAC_X86_MARKERS)
473                    {
474                        return false;
475                    }
476                } else if graph.graph[node_index].marker().is_disjoint(*MAC_MARKERS) {
477                    return false;
478                }
479            }
480
481            if platform_tags.iter().all(PlatformTag::is_android) {
482                if platform_tags.iter().all(PlatformTag::is_arm) {
483                    if graph.graph[node_index]
484                        .marker()
485                        .is_disjoint(*ANDROID_ARM_MARKERS)
486                    {
487                        return false;
488                    }
489                } else if platform_tags.iter().all(PlatformTag::is_x86_64) {
490                    if graph.graph[node_index]
491                        .marker()
492                        .is_disjoint(*ANDROID_X86_64_MARKERS)
493                    {
494                        return false;
495                    }
496                } else if platform_tags.iter().all(PlatformTag::is_x86) {
497                    if graph.graph[node_index]
498                        .marker()
499                        .is_disjoint(*ANDROID_X86_MARKERS)
500                    {
501                        return false;
502                    }
503                } else if graph.graph[node_index]
504                    .marker()
505                    .is_disjoint(*ANDROID_MARKERS)
506                {
507                    return false;
508                }
509            }
510
511            if platform_tags.iter().all(PlatformTag::is_arm) {
512                if graph.graph[node_index].marker().is_disjoint(*ARM_MARKERS) {
513                    return false;
514                }
515            }
516
517            if platform_tags.iter().all(PlatformTag::is_x86_64) {
518                if graph.graph[node_index]
519                    .marker()
520                    .is_disjoint(*X86_64_MARKERS)
521                {
522                    return false;
523                }
524            }
525
526            if platform_tags.iter().all(PlatformTag::is_x86) {
527                if graph.graph[node_index].marker().is_disjoint(*X86_MARKERS) {
528                    return false;
529                }
530            }
531
532            true
533        });
534    }
535
536    /// Initialize a [`Lock`] from a list of [`Package`] entries.
537    fn new(
538        version: u32,
539        revision: u32,
540        mut packages: Vec<Package>,
541        requires_python: RequiresPython,
542        options: ResolverOptions,
543        manifest: ResolverManifest,
544        conflicts: Conflicts,
545        supported_environments: Vec<MarkerTree>,
546        required_environments: Vec<MarkerTree>,
547        fork_markers: Vec<UniversalMarker>,
548    ) -> Result<Self, LockError> {
549        // Put all dependencies for each package in a canonical order and
550        // check for duplicates.
551        for package in &mut packages {
552            package.dependencies.sort();
553            for windows in package.dependencies.windows(2) {
554                let (dep1, dep2) = (&windows[0], &windows[1]);
555                if dep1 == dep2 {
556                    return Err(LockErrorKind::DuplicateDependency {
557                        id: package.id.clone(),
558                        dependency: dep1.clone(),
559                    }
560                    .into());
561                }
562            }
563
564            // Perform the same validation for optional dependencies.
565            for (extra, dependencies) in &mut package.optional_dependencies {
566                dependencies.sort();
567                for windows in dependencies.windows(2) {
568                    let (dep1, dep2) = (&windows[0], &windows[1]);
569                    if dep1 == dep2 {
570                        return Err(LockErrorKind::DuplicateOptionalDependency {
571                            id: package.id.clone(),
572                            extra: extra.clone(),
573                            dependency: dep1.clone(),
574                        }
575                        .into());
576                    }
577                }
578            }
579
580            // Perform the same validation for dev dependencies.
581            for (group, dependencies) in &mut package.dependency_groups {
582                dependencies.sort();
583                for windows in dependencies.windows(2) {
584                    let (dep1, dep2) = (&windows[0], &windows[1]);
585                    if dep1 == dep2 {
586                        return Err(LockErrorKind::DuplicateDevDependency {
587                            id: package.id.clone(),
588                            group: group.clone(),
589                            dependency: dep1.clone(),
590                        }
591                        .into());
592                    }
593                }
594            }
595        }
596        packages.sort_by(|dist1, dist2| dist1.id.cmp(&dist2.id));
597
598        // Check for duplicate package IDs and also build up the map for
599        // packages keyed by their ID.
600        let mut by_id = FxHashMap::default();
601        for (i, dist) in packages.iter().enumerate() {
602            if by_id.insert(dist.id.clone(), i).is_some() {
603                return Err(LockErrorKind::DuplicatePackage {
604                    id: dist.id.clone(),
605                }
606                .into());
607            }
608        }
609
610        // Build up a map from ID to extras.
611        let mut extras_by_id = FxHashMap::default();
612        for dist in &packages {
613            for extra in dist.optional_dependencies.keys() {
614                extras_by_id
615                    .entry(dist.id.clone())
616                    .or_insert_with(FxHashSet::default)
617                    .insert(extra.clone());
618            }
619        }
620
621        // Remove any non-existent extras (e.g., extras that were requested but don't exist).
622        for dist in &mut packages {
623            for dep in dist
624                .dependencies
625                .iter_mut()
626                .chain(dist.optional_dependencies.values_mut().flatten())
627                .chain(dist.dependency_groups.values_mut().flatten())
628            {
629                dep.extra.retain(|extra| {
630                    extras_by_id
631                        .get(&dep.package_id)
632                        .is_some_and(|extras| extras.contains(extra))
633                });
634            }
635        }
636
637        // Check that every dependency has an entry in `by_id`. If any don't,
638        // it implies we somehow have a dependency with no corresponding locked
639        // package.
640        for dist in &packages {
641            for dep in &dist.dependencies {
642                if !by_id.contains_key(&dep.package_id) {
643                    return Err(LockErrorKind::UnrecognizedDependency {
644                        id: dist.id.clone(),
645                        dependency: dep.clone(),
646                    }
647                    .into());
648                }
649            }
650
651            // Perform the same validation for optional dependencies.
652            for dependencies in dist.optional_dependencies.values() {
653                for dep in dependencies {
654                    if !by_id.contains_key(&dep.package_id) {
655                        return Err(LockErrorKind::UnrecognizedDependency {
656                            id: dist.id.clone(),
657                            dependency: dep.clone(),
658                        }
659                        .into());
660                    }
661                }
662            }
663
664            // Perform the same validation for dev dependencies.
665            for dependencies in dist.dependency_groups.values() {
666                for dep in dependencies {
667                    if !by_id.contains_key(&dep.package_id) {
668                        return Err(LockErrorKind::UnrecognizedDependency {
669                            id: dist.id.clone(),
670                            dependency: dep.clone(),
671                        }
672                        .into());
673                    }
674                }
675            }
676
677            // Also check that our sources are consistent with whether we have
678            // hashes or not.
679            if let Some(requires_hash) = dist.id.source.requires_hash() {
680                for wheel in &dist.wheels {
681                    if requires_hash != wheel.hash.is_some() {
682                        return Err(LockErrorKind::Hash {
683                            id: dist.id.clone(),
684                            artifact_type: "wheel",
685                            expected: requires_hash,
686                        }
687                        .into());
688                    }
689                }
690            }
691        }
692        let lock = Self {
693            version,
694            revision,
695            fork_markers,
696            conflicts,
697            supported_environments,
698            required_environments,
699            requires_python,
700            options,
701            packages,
702            by_id,
703            manifest,
704        };
705        Ok(lock)
706    }
707
708    /// Record the requirements that were used to generate this lock.
709    #[must_use]
710    pub fn with_manifest(mut self, manifest: ResolverManifest) -> Self {
711        self.manifest = manifest;
712        self
713    }
714
715    /// Record the conflicting groups that were used to generate this lock.
716    #[must_use]
717    pub fn with_conflicts(mut self, conflicts: Conflicts) -> Self {
718        self.conflicts = conflicts;
719        self
720    }
721
722    /// Record the supported environments that were used to generate this lock.
723    #[must_use]
724    pub fn with_supported_environments(mut self, supported_environments: Vec<MarkerTree>) -> Self {
725        // We "complexify" the markers given, since the supported
726        // environments given might be coming directly from what's written in
727        // `pyproject.toml`, and those are assumed to be simplified (i.e.,
728        // they assume `requires-python` is true). But a `Lock` always uses
729        // non-simplified markers internally, so we need to re-complexify them
730        // here.
731        //
732        // The nice thing about complexifying is that it's a no-op if the
733        // markers given have already been complexified.
734        self.supported_environments = supported_environments
735            .into_iter()
736            .map(|marker| self.requires_python.complexify_markers(marker))
737            .collect();
738        self
739    }
740
741    /// Record the required platforms that were used to generate this lock.
742    #[must_use]
743    pub fn with_required_environments(mut self, required_environments: Vec<MarkerTree>) -> Self {
744        self.required_environments = required_environments
745            .into_iter()
746            .map(|marker| self.requires_python.complexify_markers(marker))
747            .collect();
748        self
749    }
750
751    /// Returns `true` if this [`Lock`] includes `provides-extra` metadata.
752    pub fn supports_provides_extra(&self) -> bool {
753        // `provides-extra` was added in Version 1 Revision 1.
754        (self.version(), self.revision()) >= (1, 1)
755    }
756
757    /// Returns `true` if this [`Lock`] includes entries for empty `dependency-group` metadata.
758    pub fn includes_empty_groups(&self) -> bool {
759        // Empty dependency groups are included as of https://github.com/astral-sh/uv/pull/8598,
760        // but Version 1 Revision 1 is the first revision published after that change.
761        (self.version(), self.revision()) >= (1, 1)
762    }
763
764    /// Returns the lockfile version.
765    pub fn version(&self) -> u32 {
766        self.version
767    }
768
769    /// Returns the lockfile revision.
770    pub fn revision(&self) -> u32 {
771        self.revision
772    }
773
774    /// Returns the number of packages in the lockfile.
775    pub fn len(&self) -> usize {
776        self.packages.len()
777    }
778
779    /// Returns `true` if the lockfile contains no packages.
780    pub fn is_empty(&self) -> bool {
781        self.packages.is_empty()
782    }
783
784    /// Returns the [`Package`] entries in this lock.
785    pub fn packages(&self) -> &[Package] {
786        &self.packages
787    }
788
789    /// Returns the supported Python version range for the lockfile, if present.
790    pub fn requires_python(&self) -> &RequiresPython {
791        &self.requires_python
792    }
793
794    /// Returns the resolution mode used to generate this lock.
795    pub fn resolution_mode(&self) -> ResolutionMode {
796        self.options.resolution_mode
797    }
798
799    /// Returns the pre-release mode used to generate this lock.
800    pub fn prerelease_mode(&self) -> PrereleaseMode {
801        self.options.prerelease_mode
802    }
803
804    /// Returns the multi-version mode used to generate this lock.
805    pub fn fork_strategy(&self) -> ForkStrategy {
806        self.options.fork_strategy
807    }
808
809    /// Returns the exclude newer setting used to generate this lock.
810    pub fn exclude_newer(&self) -> ExcludeNewer {
811        // TODO(zanieb): It'd be nice not to hide this clone here, but I am hesitant to introduce
812        // a whole new `ExcludeNewerRef` type just for this
813        self.options.exclude_newer.clone().into()
814    }
815
816    /// Returns the conflicting groups that were used to generate this lock.
817    pub fn conflicts(&self) -> &Conflicts {
818        &self.conflicts
819    }
820
821    /// Returns the supported environments that were used to generate this lock.
822    pub fn supported_environments(&self) -> &[MarkerTree] {
823        &self.supported_environments
824    }
825
826    /// Returns the required platforms that were used to generate this lock.
827    pub fn required_environments(&self) -> &[MarkerTree] {
828        &self.required_environments
829    }
830
831    /// Returns the workspace members that were used to generate this lock.
832    pub fn members(&self) -> &BTreeSet<PackageName> {
833        &self.manifest.members
834    }
835
836    /// Returns the dependency groups that were used to generate this lock.
837    pub fn requirements(&self) -> &BTreeSet<Requirement> {
838        &self.manifest.requirements
839    }
840
841    /// Returns the dependency groups that were used to generate this lock.
842    pub fn dependency_groups(&self) -> &BTreeMap<GroupName, BTreeSet<Requirement>> {
843        &self.manifest.dependency_groups
844    }
845
846    /// Returns the build constraints that were used to generate this lock.
847    pub fn build_constraints(&self, root: &Path) -> Constraints {
848        Constraints::from_requirements(
849            self.manifest
850                .build_constraints
851                .iter()
852                .cloned()
853                .map(|requirement| requirement.to_absolute(root)),
854        )
855    }
856
857    /// Return the workspace root used to generate this lock.
858    pub fn root(&self) -> Option<&Package> {
859        self.packages.iter().find(|package| {
860            let (Source::Editable(path) | Source::Virtual(path)) = &package.id.source else {
861                return false;
862            };
863            path.as_ref() == Path::new("")
864        })
865    }
866
867    /// Returns the supported environments that were used to generate this
868    /// lock.
869    ///
870    /// The markers returned here are "simplified" with respect to the lock
871    /// file's `requires-python` setting. This means these should only be used
872    /// for direct comparison purposes with the supported environments written
873    /// by a human in `pyproject.toml`. (Think of "supported environments" in
874    /// `pyproject.toml` as having an implicit `and python_full_version >=
875    /// '{requires-python-bound}'` attached to each one.)
876    pub fn simplified_supported_environments(&self) -> Vec<MarkerTree> {
877        self.supported_environments()
878            .iter()
879            .copied()
880            .map(|marker| self.simplify_environment(marker))
881            .collect()
882    }
883
884    /// Returns the required platforms that were used to generate this
885    /// lock.
886    pub fn simplified_required_environments(&self) -> Vec<MarkerTree> {
887        self.required_environments()
888            .iter()
889            .copied()
890            .map(|marker| self.simplify_environment(marker))
891            .collect()
892    }
893
894    /// Simplify the given marker environment with respect to the lockfile's
895    /// `requires-python` setting.
896    pub fn simplify_environment(&self, marker: MarkerTree) -> MarkerTree {
897        self.requires_python.simplify_markers(marker)
898    }
899
900    /// If this lockfile was built from a forking resolution with non-identical forks, return the
901    /// markers of those forks, otherwise `None`.
902    pub fn fork_markers(&self) -> &[UniversalMarker] {
903        self.fork_markers.as_slice()
904    }
905
906    /// Checks whether the fork markers cover the entire supported marker space.
907    ///
908    /// Returns the actually covered and the expected marker space on validation error.
909    pub fn check_marker_coverage(&self) -> Result<(), (MarkerTree, MarkerTree)> {
910        let fork_markers_union = if self.fork_markers().is_empty() {
911            self.requires_python.to_marker_tree()
912        } else {
913            let mut fork_markers_union = MarkerTree::FALSE;
914            for fork_marker in self.fork_markers() {
915                fork_markers_union.or(fork_marker.pep508());
916            }
917            fork_markers_union
918        };
919        let mut environments_union = if !self.supported_environments.is_empty() {
920            let mut environments_union = MarkerTree::FALSE;
921            for fork_marker in &self.supported_environments {
922                environments_union.or(*fork_marker);
923            }
924            environments_union
925        } else {
926            MarkerTree::TRUE
927        };
928        // When a user defines environments, they are implicitly constrained by requires-python.
929        environments_union.and(self.requires_python.to_marker_tree());
930        if fork_markers_union.negate().is_disjoint(environments_union) {
931            Ok(())
932        } else {
933            Err((fork_markers_union, environments_union))
934        }
935    }
936
937    /// Checks whether the new requires-python specification is disjoint with
938    /// the fork markers in this lock file.
939    ///
940    /// If they are disjoint, then the union of the fork markers along with the
941    /// given requires-python specification (converted to a marker tree) are
942    /// returned.
943    ///
944    /// When disjoint, the fork markers in the lock file should be dropped and
945    /// not used.
946    pub fn requires_python_coverage(
947        &self,
948        new_requires_python: &RequiresPython,
949    ) -> Result<(), (MarkerTree, MarkerTree)> {
950        let fork_markers_union = if self.fork_markers().is_empty() {
951            self.requires_python.to_marker_tree()
952        } else {
953            let mut fork_markers_union = MarkerTree::FALSE;
954            for fork_marker in self.fork_markers() {
955                fork_markers_union.or(fork_marker.pep508());
956            }
957            fork_markers_union
958        };
959        let new_requires_python = new_requires_python.to_marker_tree();
960        if fork_markers_union.is_disjoint(new_requires_python) {
961            Err((fork_markers_union, new_requires_python))
962        } else {
963            Ok(())
964        }
965    }
966
967    /// Returns the TOML representation of this lockfile.
968    pub fn to_toml(&self) -> Result<String, toml_edit::ser::Error> {
969        // Catch a lockfile where the union of fork markers doesn't cover the supported
970        // environments.
971        debug_assert!(self.check_marker_coverage().is_ok());
972
973        // We construct a TOML document manually instead of going through Serde to enable
974        // the use of inline tables.
975        let mut doc = toml_edit::DocumentMut::new();
976        doc.insert("version", value(i64::from(self.version)));
977
978        if self.revision > 0 {
979            doc.insert("revision", value(i64::from(self.revision)));
980        }
981
982        doc.insert("requires-python", value(self.requires_python.to_string()));
983
984        if !self.fork_markers.is_empty() {
985            let fork_markers = each_element_on_its_line_array(
986                simplified_universal_markers(&self.fork_markers, &self.requires_python).into_iter(),
987            );
988            if !fork_markers.is_empty() {
989                doc.insert("resolution-markers", value(fork_markers));
990            }
991        }
992
993        if !self.supported_environments.is_empty() {
994            let supported_environments = each_element_on_its_line_array(
995                self.supported_environments
996                    .iter()
997                    .copied()
998                    .map(|marker| SimplifiedMarkerTree::new(&self.requires_python, marker))
999                    .filter_map(SimplifiedMarkerTree::try_to_string),
1000            );
1001            doc.insert("supported-markers", value(supported_environments));
1002        }
1003
1004        if !self.required_environments.is_empty() {
1005            let required_environments = each_element_on_its_line_array(
1006                self.required_environments
1007                    .iter()
1008                    .copied()
1009                    .map(|marker| SimplifiedMarkerTree::new(&self.requires_python, marker))
1010                    .filter_map(SimplifiedMarkerTree::try_to_string),
1011            );
1012            doc.insert("required-markers", value(required_environments));
1013        }
1014
1015        if !self.conflicts.is_empty() {
1016            let mut list = Array::new();
1017            for set in self.conflicts.iter() {
1018                list.push(each_element_on_its_line_array(set.iter().map(|item| {
1019                    let mut table = InlineTable::new();
1020                    table.insert("package", Value::from(item.package().to_string()));
1021                    match item.kind() {
1022                        ConflictKind::Project => {}
1023                        ConflictKind::Extra(extra) => {
1024                            table.insert("extra", Value::from(extra.to_string()));
1025                        }
1026                        ConflictKind::Group(group) => {
1027                            table.insert("group", Value::from(group.to_string()));
1028                        }
1029                    }
1030                    table
1031                })));
1032            }
1033            doc.insert("conflicts", value(list));
1034        }
1035
1036        // Write the settings that were used to generate the resolution.
1037        // This enables us to invalidate the lockfile if the user changes
1038        // their settings.
1039        {
1040            let mut options_table = Table::new();
1041
1042            if self.options.resolution_mode != ResolutionMode::default() {
1043                options_table.insert(
1044                    "resolution-mode",
1045                    value(self.options.resolution_mode.to_string()),
1046                );
1047            }
1048            if self.options.prerelease_mode != PrereleaseMode::default() {
1049                options_table.insert(
1050                    "prerelease-mode",
1051                    value(self.options.prerelease_mode.to_string()),
1052                );
1053            }
1054            if self.options.fork_strategy != ForkStrategy::default() {
1055                options_table.insert(
1056                    "fork-strategy",
1057                    value(self.options.fork_strategy.to_string()),
1058                );
1059            }
1060            let exclude_newer = ExcludeNewer::from(self.options.exclude_newer.clone());
1061            if !exclude_newer.is_empty() {
1062                // Always serialize global exclude-newer as a string
1063                if let Some(global) = &exclude_newer.global {
1064                    options_table.insert("exclude-newer", value(global.to_string()));
1065                    // Serialize the original span if present
1066                    if let Some(span) = global.span() {
1067                        options_table.insert("exclude-newer-span", value(span.to_string()));
1068                    }
1069                }
1070
1071                // Serialize package-specific exclusions as a separate field
1072                if !exclude_newer.package.is_empty() {
1073                    let mut package_table = toml_edit::Table::new();
1074                    for (name, exclude_newer_value) in &exclude_newer.package {
1075                        if let Some(span) = exclude_newer_value.span() {
1076                            // Serialize as inline table with timestamp and span
1077                            let mut inline = toml_edit::InlineTable::new();
1078                            inline.insert(
1079                                "timestamp",
1080                                exclude_newer_value.timestamp().to_string().into(),
1081                            );
1082                            inline.insert("span", span.to_string().into());
1083                            package_table.insert(name.as_ref(), Item::Value(inline.into()));
1084                        } else {
1085                            // Serialize as simple string
1086                            package_table
1087                                .insert(name.as_ref(), value(exclude_newer_value.to_string()));
1088                        }
1089                    }
1090                    options_table.insert("exclude-newer-package", Item::Table(package_table));
1091                }
1092            }
1093
1094            if !options_table.is_empty() {
1095                doc.insert("options", Item::Table(options_table));
1096            }
1097        }
1098
1099        // Write the manifest that was used to generate the resolution.
1100        {
1101            let mut manifest_table = Table::new();
1102
1103            if !self.manifest.members.is_empty() {
1104                manifest_table.insert(
1105                    "members",
1106                    value(each_element_on_its_line_array(
1107                        self.manifest
1108                            .members
1109                            .iter()
1110                            .map(std::string::ToString::to_string),
1111                    )),
1112                );
1113            }
1114
1115            if !self.manifest.requirements.is_empty() {
1116                let requirements = self
1117                    .manifest
1118                    .requirements
1119                    .iter()
1120                    .map(|requirement| {
1121                        serde::Serialize::serialize(
1122                            &requirement,
1123                            toml_edit::ser::ValueSerializer::new(),
1124                        )
1125                    })
1126                    .collect::<Result<Vec<_>, _>>()?;
1127                let requirements = match requirements.as_slice() {
1128                    [] => Array::new(),
1129                    [requirement] => Array::from_iter([requirement]),
1130                    requirements => each_element_on_its_line_array(requirements.iter()),
1131                };
1132                manifest_table.insert("requirements", value(requirements));
1133            }
1134
1135            if !self.manifest.constraints.is_empty() {
1136                let constraints = self
1137                    .manifest
1138                    .constraints
1139                    .iter()
1140                    .map(|requirement| {
1141                        serde::Serialize::serialize(
1142                            &requirement,
1143                            toml_edit::ser::ValueSerializer::new(),
1144                        )
1145                    })
1146                    .collect::<Result<Vec<_>, _>>()?;
1147                let constraints = match constraints.as_slice() {
1148                    [] => Array::new(),
1149                    [requirement] => Array::from_iter([requirement]),
1150                    constraints => each_element_on_its_line_array(constraints.iter()),
1151                };
1152                manifest_table.insert("constraints", value(constraints));
1153            }
1154
1155            if !self.manifest.overrides.is_empty() {
1156                let overrides = self
1157                    .manifest
1158                    .overrides
1159                    .iter()
1160                    .map(|requirement| {
1161                        serde::Serialize::serialize(
1162                            &requirement,
1163                            toml_edit::ser::ValueSerializer::new(),
1164                        )
1165                    })
1166                    .collect::<Result<Vec<_>, _>>()?;
1167                let overrides = match overrides.as_slice() {
1168                    [] => Array::new(),
1169                    [requirement] => Array::from_iter([requirement]),
1170                    overrides => each_element_on_its_line_array(overrides.iter()),
1171                };
1172                manifest_table.insert("overrides", value(overrides));
1173            }
1174
1175            if !self.manifest.excludes.is_empty() {
1176                let excludes = self
1177                    .manifest
1178                    .excludes
1179                    .iter()
1180                    .map(|name| {
1181                        serde::Serialize::serialize(&name, toml_edit::ser::ValueSerializer::new())
1182                    })
1183                    .collect::<Result<Vec<_>, _>>()?;
1184                let excludes = match excludes.as_slice() {
1185                    [] => Array::new(),
1186                    [name] => Array::from_iter([name]),
1187                    excludes => each_element_on_its_line_array(excludes.iter()),
1188                };
1189                manifest_table.insert("excludes", value(excludes));
1190            }
1191
1192            if !self.manifest.build_constraints.is_empty() {
1193                let build_constraints = self
1194                    .manifest
1195                    .build_constraints
1196                    .iter()
1197                    .map(|requirement| {
1198                        serde::Serialize::serialize(
1199                            &requirement,
1200                            toml_edit::ser::ValueSerializer::new(),
1201                        )
1202                    })
1203                    .collect::<Result<Vec<_>, _>>()?;
1204                let build_constraints = match build_constraints.as_slice() {
1205                    [] => Array::new(),
1206                    [requirement] => Array::from_iter([requirement]),
1207                    build_constraints => each_element_on_its_line_array(build_constraints.iter()),
1208                };
1209                manifest_table.insert("build-constraints", value(build_constraints));
1210            }
1211
1212            if !self.manifest.dependency_groups.is_empty() {
1213                let mut dependency_groups = Table::new();
1214                for (extra, requirements) in &self.manifest.dependency_groups {
1215                    let requirements = requirements
1216                        .iter()
1217                        .map(|requirement| {
1218                            serde::Serialize::serialize(
1219                                &requirement,
1220                                toml_edit::ser::ValueSerializer::new(),
1221                            )
1222                        })
1223                        .collect::<Result<Vec<_>, _>>()?;
1224                    let requirements = match requirements.as_slice() {
1225                        [] => Array::new(),
1226                        [requirement] => Array::from_iter([requirement]),
1227                        requirements => each_element_on_its_line_array(requirements.iter()),
1228                    };
1229                    if !requirements.is_empty() {
1230                        dependency_groups.insert(extra.as_ref(), value(requirements));
1231                    }
1232                }
1233                if !dependency_groups.is_empty() {
1234                    manifest_table.insert("dependency-groups", Item::Table(dependency_groups));
1235                }
1236            }
1237
1238            if !self.manifest.dependency_metadata.is_empty() {
1239                let mut tables = ArrayOfTables::new();
1240                for metadata in &self.manifest.dependency_metadata {
1241                    let mut table = Table::new();
1242                    table.insert("name", value(metadata.name.to_string()));
1243                    if let Some(version) = metadata.version.as_ref() {
1244                        table.insert("version", value(version.to_string()));
1245                    }
1246                    if !metadata.requires_dist.is_empty() {
1247                        table.insert(
1248                            "requires-dist",
1249                            value(serde::Serialize::serialize(
1250                                &metadata.requires_dist,
1251                                toml_edit::ser::ValueSerializer::new(),
1252                            )?),
1253                        );
1254                    }
1255                    if let Some(requires_python) = metadata.requires_python.as_ref() {
1256                        table.insert("requires-python", value(requires_python.to_string()));
1257                    }
1258                    if !metadata.provides_extra.is_empty() {
1259                        table.insert(
1260                            "provides-extras",
1261                            value(serde::Serialize::serialize(
1262                                &metadata.provides_extra,
1263                                toml_edit::ser::ValueSerializer::new(),
1264                            )?),
1265                        );
1266                    }
1267                    tables.push(table);
1268                }
1269                manifest_table.insert("dependency-metadata", Item::ArrayOfTables(tables));
1270            }
1271
1272            if !manifest_table.is_empty() {
1273                doc.insert("manifest", Item::Table(manifest_table));
1274            }
1275        }
1276
1277        // Count the number of packages for each package name. When
1278        // there's only one package for a particular package name (the
1279        // overwhelmingly common case), we can omit some data (like source and
1280        // version) on dependency edges since it is strictly redundant.
1281        let mut dist_count_by_name: FxHashMap<PackageName, u64> = FxHashMap::default();
1282        for dist in &self.packages {
1283            *dist_count_by_name.entry(dist.id.name.clone()).or_default() += 1;
1284        }
1285
1286        let mut packages = ArrayOfTables::new();
1287        for dist in &self.packages {
1288            packages.push(dist.to_toml(&self.requires_python, &dist_count_by_name)?);
1289        }
1290
1291        doc.insert("package", Item::ArrayOfTables(packages));
1292        Ok(doc.to_string())
1293    }
1294
1295    /// Returns the package with the given name. If there are multiple
1296    /// matching packages, then an error is returned. If there are no
1297    /// matching packages, then `Ok(None)` is returned.
1298    pub fn find_by_name(&self, name: &PackageName) -> Result<Option<&Package>, String> {
1299        let mut found_dist = None;
1300        for dist in &self.packages {
1301            if &dist.id.name == name {
1302                if found_dist.is_some() {
1303                    return Err(format!("found multiple packages matching `{name}`"));
1304                }
1305                found_dist = Some(dist);
1306            }
1307        }
1308        Ok(found_dist)
1309    }
1310
1311    /// Returns the package with the given name.
1312    ///
1313    /// If there are multiple matching packages, returns the package that
1314    /// corresponds to the given marker tree.
1315    ///
1316    /// If there are multiple packages that are relevant to the current
1317    /// markers, then an error is returned.
1318    ///
1319    /// If there are no matching packages, then `Ok(None)` is returned.
1320    fn find_by_markers(
1321        &self,
1322        name: &PackageName,
1323        marker_env: &MarkerEnvironment,
1324    ) -> Result<Option<&Package>, String> {
1325        let mut found_dist = None;
1326        for dist in &self.packages {
1327            if &dist.id.name == name {
1328                if dist.fork_markers.is_empty()
1329                    || dist
1330                        .fork_markers
1331                        .iter()
1332                        .any(|marker| marker.evaluate_no_extras(marker_env))
1333                {
1334                    if found_dist.is_some() {
1335                        return Err(format!("found multiple packages matching `{name}`"));
1336                    }
1337                    found_dist = Some(dist);
1338                }
1339            }
1340        }
1341        Ok(found_dist)
1342    }
1343
1344    fn find_by_id(&self, id: &PackageId) -> &Package {
1345        let index = *self.by_id.get(id).expect("locked package for ID");
1346
1347        (self.packages.get(index).expect("valid index for package")) as _
1348    }
1349
1350    /// Return a [`SatisfiesResult`] if the given extras do not match the [`Package`] metadata.
1351    fn satisfies_provides_extra<'lock>(
1352        &self,
1353        provides_extra: Box<[ExtraName]>,
1354        package: &'lock Package,
1355    ) -> SatisfiesResult<'lock> {
1356        if !self.supports_provides_extra() {
1357            return SatisfiesResult::Satisfied;
1358        }
1359
1360        let expected: BTreeSet<_> = provides_extra.iter().collect();
1361        let actual: BTreeSet<_> = package.metadata.provides_extra.iter().collect();
1362
1363        if expected != actual {
1364            let expected = Box::into_iter(provides_extra).collect();
1365            return SatisfiesResult::MismatchedPackageProvidesExtra(
1366                &package.id.name,
1367                package.id.version.as_ref(),
1368                expected,
1369                actual,
1370            );
1371        }
1372
1373        SatisfiesResult::Satisfied
1374    }
1375
1376    /// Return a [`SatisfiesResult`] if the given requirements do not match the [`Package`] metadata.
1377    #[allow(clippy::unused_self)]
1378    fn satisfies_requires_dist<'lock>(
1379        &self,
1380        requires_dist: Box<[Requirement]>,
1381        dependency_groups: BTreeMap<GroupName, Box<[Requirement]>>,
1382        package: &'lock Package,
1383        root: &Path,
1384    ) -> Result<SatisfiesResult<'lock>, LockError> {
1385        // Special-case: if the version is dynamic, compare the flattened requirements.
1386        let flattened = if package.is_dynamic() {
1387            Some(
1388                FlatRequiresDist::from_requirements(requires_dist.clone(), &package.id.name)
1389                    .into_iter()
1390                    .map(|requirement| {
1391                        normalize_requirement(requirement, root, &self.requires_python)
1392                    })
1393                    .collect::<Result<BTreeSet<_>, _>>()?,
1394            )
1395        } else {
1396            None
1397        };
1398
1399        // Validate the `requires-dist` metadata.
1400        let expected: BTreeSet<_> = Box::into_iter(requires_dist)
1401            .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1402            .collect::<Result<_, _>>()?;
1403        let actual: BTreeSet<_> = package
1404            .metadata
1405            .requires_dist
1406            .iter()
1407            .cloned()
1408            .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1409            .collect::<Result<_, _>>()?;
1410
1411        if expected != actual && flattened.is_none_or(|expected| expected != actual) {
1412            return Ok(SatisfiesResult::MismatchedPackageRequirements(
1413                &package.id.name,
1414                package.id.version.as_ref(),
1415                expected,
1416                actual,
1417            ));
1418        }
1419
1420        // Validate the `dependency-groups` metadata.
1421        let expected: BTreeMap<GroupName, BTreeSet<Requirement>> = dependency_groups
1422            .into_iter()
1423            .filter(|(_, requirements)| self.includes_empty_groups() || !requirements.is_empty())
1424            .map(|(group, requirements)| {
1425                Ok::<_, LockError>((
1426                    group,
1427                    Box::into_iter(requirements)
1428                        .map(|requirement| {
1429                            normalize_requirement(requirement, root, &self.requires_python)
1430                        })
1431                        .collect::<Result<_, _>>()?,
1432                ))
1433            })
1434            .collect::<Result<_, _>>()?;
1435        let actual: BTreeMap<GroupName, BTreeSet<Requirement>> = package
1436            .metadata
1437            .dependency_groups
1438            .iter()
1439            .filter(|(_, requirements)| self.includes_empty_groups() || !requirements.is_empty())
1440            .map(|(group, requirements)| {
1441                Ok::<_, LockError>((
1442                    group.clone(),
1443                    requirements
1444                        .iter()
1445                        .cloned()
1446                        .map(|requirement| {
1447                            normalize_requirement(requirement, root, &self.requires_python)
1448                        })
1449                        .collect::<Result<_, _>>()?,
1450                ))
1451            })
1452            .collect::<Result<_, _>>()?;
1453
1454        if expected != actual {
1455            return Ok(SatisfiesResult::MismatchedPackageDependencyGroups(
1456                &package.id.name,
1457                package.id.version.as_ref(),
1458                expected,
1459                actual,
1460            ));
1461        }
1462
1463        Ok(SatisfiesResult::Satisfied)
1464    }
1465
1466    /// Check whether the lock matches the project structure, requirements and configuration.
1467    pub async fn satisfies<Context: BuildContext>(
1468        &self,
1469        root: &Path,
1470        packages: &BTreeMap<PackageName, WorkspaceMember>,
1471        members: &[PackageName],
1472        required_members: &BTreeMap<PackageName, Editability>,
1473        requirements: &[Requirement],
1474        constraints: &[Requirement],
1475        overrides: &[Requirement],
1476        excludes: &[PackageName],
1477        build_constraints: &[Requirement],
1478        dependency_groups: &BTreeMap<GroupName, Vec<Requirement>>,
1479        dependency_metadata: &DependencyMetadata,
1480        indexes: Option<&IndexLocations>,
1481        tags: &Tags,
1482        markers: &MarkerEnvironment,
1483        hasher: &HashStrategy,
1484        index: &InMemoryIndex,
1485        database: &DistributionDatabase<'_, Context>,
1486    ) -> Result<SatisfiesResult<'_>, LockError> {
1487        let mut queue: VecDeque<&Package> = VecDeque::new();
1488        let mut seen = FxHashSet::default();
1489
1490        // Validate that the lockfile was generated with the same root members.
1491        {
1492            let expected = members.iter().cloned().collect::<BTreeSet<_>>();
1493            let actual = &self.manifest.members;
1494            if expected != *actual {
1495                return Ok(SatisfiesResult::MismatchedMembers(expected, actual));
1496            }
1497        }
1498
1499        // Validate that the member sources have not changed (e.g., that they've switched from
1500        // virtual to non-virtual or vice versa).
1501        for (name, member) in packages {
1502            let source = self.find_by_name(name).ok().flatten();
1503
1504            // Determine whether the member was required by any other member.
1505            let value = required_members.get(name);
1506            let is_required_member = value.is_some();
1507            let editability = value.copied().flatten();
1508
1509            // Verify that the member is virtual (or not).
1510            let expected_virtual = !member.pyproject_toml().is_package(!is_required_member);
1511            let actual_virtual =
1512                source.map(|package| matches!(package.id.source, Source::Virtual(..)));
1513            if actual_virtual != Some(expected_virtual) {
1514                return Ok(SatisfiesResult::MismatchedVirtual(
1515                    name.clone(),
1516                    expected_virtual,
1517                ));
1518            }
1519
1520            // Verify that the member is editable (or not).
1521            let expected_editable = if expected_virtual {
1522                false
1523            } else {
1524                editability.unwrap_or(true)
1525            };
1526            let actual_editable =
1527                source.map(|package| matches!(package.id.source, Source::Editable(..)));
1528            if actual_editable != Some(expected_editable) {
1529                return Ok(SatisfiesResult::MismatchedEditable(
1530                    name.clone(),
1531                    expected_editable,
1532                ));
1533            }
1534        }
1535
1536        // Validate that the lockfile was generated with the same requirements.
1537        {
1538            let expected: BTreeSet<_> = requirements
1539                .iter()
1540                .cloned()
1541                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1542                .collect::<Result<_, _>>()?;
1543            let actual: BTreeSet<_> = self
1544                .manifest
1545                .requirements
1546                .iter()
1547                .cloned()
1548                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1549                .collect::<Result<_, _>>()?;
1550            if expected != actual {
1551                return Ok(SatisfiesResult::MismatchedRequirements(expected, actual));
1552            }
1553        }
1554
1555        // Validate that the lockfile was generated with the same constraints.
1556        {
1557            let expected: BTreeSet<_> = constraints
1558                .iter()
1559                .cloned()
1560                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1561                .collect::<Result<_, _>>()?;
1562            let actual: BTreeSet<_> = self
1563                .manifest
1564                .constraints
1565                .iter()
1566                .cloned()
1567                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1568                .collect::<Result<_, _>>()?;
1569            if expected != actual {
1570                return Ok(SatisfiesResult::MismatchedConstraints(expected, actual));
1571            }
1572        }
1573
1574        // Validate that the lockfile was generated with the same overrides.
1575        {
1576            let expected: BTreeSet<_> = overrides
1577                .iter()
1578                .cloned()
1579                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1580                .collect::<Result<_, _>>()?;
1581            let actual: BTreeSet<_> = self
1582                .manifest
1583                .overrides
1584                .iter()
1585                .cloned()
1586                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1587                .collect::<Result<_, _>>()?;
1588            if expected != actual {
1589                return Ok(SatisfiesResult::MismatchedOverrides(expected, actual));
1590            }
1591        }
1592
1593        // Validate that the lockfile was generated with the same excludes.
1594        {
1595            let expected: BTreeSet<_> = excludes.iter().cloned().collect();
1596            let actual: BTreeSet<_> = self.manifest.excludes.iter().cloned().collect();
1597            if expected != actual {
1598                return Ok(SatisfiesResult::MismatchedExcludes(expected, actual));
1599            }
1600        }
1601
1602        // Validate that the lockfile was generated with the same build constraints.
1603        {
1604            let expected: BTreeSet<_> = build_constraints
1605                .iter()
1606                .cloned()
1607                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1608                .collect::<Result<_, _>>()?;
1609            let actual: BTreeSet<_> = self
1610                .manifest
1611                .build_constraints
1612                .iter()
1613                .cloned()
1614                .map(|requirement| normalize_requirement(requirement, root, &self.requires_python))
1615                .collect::<Result<_, _>>()?;
1616            if expected != actual {
1617                return Ok(SatisfiesResult::MismatchedBuildConstraints(
1618                    expected, actual,
1619                ));
1620            }
1621        }
1622
1623        // Validate that the lockfile was generated with the dependency groups.
1624        {
1625            let expected: BTreeMap<GroupName, BTreeSet<Requirement>> = dependency_groups
1626                .iter()
1627                .filter(|(_, requirements)| !requirements.is_empty())
1628                .map(|(group, requirements)| {
1629                    Ok::<_, LockError>((
1630                        group.clone(),
1631                        requirements
1632                            .iter()
1633                            .cloned()
1634                            .map(|requirement| {
1635                                normalize_requirement(requirement, root, &self.requires_python)
1636                            })
1637                            .collect::<Result<_, _>>()?,
1638                    ))
1639                })
1640                .collect::<Result<_, _>>()?;
1641            let actual: BTreeMap<GroupName, BTreeSet<Requirement>> = self
1642                .manifest
1643                .dependency_groups
1644                .iter()
1645                .filter(|(_, requirements)| !requirements.is_empty())
1646                .map(|(group, requirements)| {
1647                    Ok::<_, LockError>((
1648                        group.clone(),
1649                        requirements
1650                            .iter()
1651                            .cloned()
1652                            .map(|requirement| {
1653                                normalize_requirement(requirement, root, &self.requires_python)
1654                            })
1655                            .collect::<Result<_, _>>()?,
1656                    ))
1657                })
1658                .collect::<Result<_, _>>()?;
1659            if expected != actual {
1660                return Ok(SatisfiesResult::MismatchedDependencyGroups(
1661                    expected, actual,
1662                ));
1663            }
1664        }
1665
1666        // Validate that the lockfile was generated with the same static metadata.
1667        {
1668            let expected = dependency_metadata
1669                .values()
1670                .cloned()
1671                .collect::<BTreeSet<_>>();
1672            let actual = &self.manifest.dependency_metadata;
1673            if expected != *actual {
1674                return Ok(SatisfiesResult::MismatchedStaticMetadata(expected, actual));
1675            }
1676        }
1677
1678        // Collect the set of available indexes (both `--index-url` and `--find-links` entries).
1679        let mut remotes = indexes.map(|locations| {
1680            locations
1681                .allowed_indexes()
1682                .into_iter()
1683                .filter_map(|index| match index.url() {
1684                    IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
1685                        Some(UrlString::from(index.url().without_credentials().as_ref()))
1686                    }
1687                    IndexUrl::Path(_) => None,
1688                })
1689                .collect::<BTreeSet<_>>()
1690        });
1691
1692        let mut locals = indexes.map(|locations| {
1693            locations
1694                .allowed_indexes()
1695                .into_iter()
1696                .filter_map(|index| match index.url() {
1697                    IndexUrl::Pypi(_) | IndexUrl::Url(_) => None,
1698                    IndexUrl::Path(url) => {
1699                        let path = url.to_file_path().ok()?;
1700                        let path = relative_to(&path, root)
1701                            .or_else(|_| std::path::absolute(path))
1702                            .ok()?
1703                            .into_boxed_path();
1704                        Some(path)
1705                    }
1706                })
1707                .collect::<BTreeSet<_>>()
1708        });
1709
1710        // Add the workspace packages to the queue.
1711        for root_name in packages.keys() {
1712            let root = self
1713                .find_by_name(root_name)
1714                .expect("found too many packages matching root");
1715
1716            let Some(root) = root else {
1717                // The package is not in the lockfile, so it can't be satisfied.
1718                return Ok(SatisfiesResult::MissingRoot(root_name.clone()));
1719            };
1720
1721            // Add the base package.
1722            queue.push_back(root);
1723        }
1724
1725        while let Some(package) = queue.pop_front() {
1726            // If the lockfile references an index that was not provided, we can't validate it.
1727            if let Source::Registry(index) = &package.id.source {
1728                match index {
1729                    RegistrySource::Url(url) => {
1730                        if remotes
1731                            .as_ref()
1732                            .is_some_and(|remotes| !remotes.contains(url))
1733                        {
1734                            let name = &package.id.name;
1735                            let version = &package
1736                                .id
1737                                .version
1738                                .as_ref()
1739                                .expect("version for registry source");
1740                            return Ok(SatisfiesResult::MissingRemoteIndex(name, version, url));
1741                        }
1742                    }
1743                    RegistrySource::Path(path) => {
1744                        if locals.as_ref().is_some_and(|locals| !locals.contains(path)) {
1745                            let name = &package.id.name;
1746                            let version = &package
1747                                .id
1748                                .version
1749                                .as_ref()
1750                                .expect("version for registry source");
1751                            return Ok(SatisfiesResult::MissingLocalIndex(name, version, path));
1752                        }
1753                    }
1754                }
1755            }
1756
1757            // If the package is immutable, we don't need to validate it (or its dependencies).
1758            if package.id.source.is_immutable() {
1759                continue;
1760            }
1761
1762            if let Some(version) = package.id.version.as_ref() {
1763                // For a non-dynamic package, fetch the metadata from the distribution database.
1764                let dist = package.to_dist(
1765                    root,
1766                    TagPolicy::Preferred(tags),
1767                    &BuildOptions::default(),
1768                    markers,
1769                )?;
1770
1771                let metadata = {
1772                    let id = dist.version_id();
1773                    if let Some(archive) =
1774                        index
1775                            .distributions()
1776                            .get(&id)
1777                            .as_deref()
1778                            .and_then(|response| {
1779                                if let MetadataResponse::Found(archive, ..) = response {
1780                                    Some(archive)
1781                                } else {
1782                                    None
1783                                }
1784                            })
1785                    {
1786                        // If the metadata is already in the index, return it.
1787                        archive.metadata.clone()
1788                    } else {
1789                        // Run the PEP 517 build process to extract metadata from the source distribution.
1790                        let archive = database
1791                            .get_or_build_wheel_metadata(&dist, hasher.get(&dist))
1792                            .await
1793                            .map_err(|err| LockErrorKind::Resolution {
1794                                id: package.id.clone(),
1795                                err,
1796                            })?;
1797
1798                        let metadata = archive.metadata.clone();
1799
1800                        // Insert the metadata into the index.
1801                        index
1802                            .distributions()
1803                            .done(id, Arc::new(MetadataResponse::Found(archive)));
1804
1805                        metadata
1806                    }
1807                };
1808
1809                // If this is a local package, validate that it hasn't become dynamic (in which
1810                // case, we'd expect the version to be omitted).
1811                if package.id.source.is_source_tree() {
1812                    if metadata.dynamic {
1813                        return Ok(SatisfiesResult::MismatchedDynamic(&package.id.name, false));
1814                    }
1815                }
1816
1817                // Validate the `version` metadata.
1818                if metadata.version != *version {
1819                    return Ok(SatisfiesResult::MismatchedVersion(
1820                        &package.id.name,
1821                        version.clone(),
1822                        Some(metadata.version.clone()),
1823                    ));
1824                }
1825
1826                // Validate the `provides-extras` metadata.
1827                match self.satisfies_provides_extra(metadata.provides_extra, package) {
1828                    SatisfiesResult::Satisfied => {}
1829                    result => return Ok(result),
1830                }
1831
1832                // Validate that the requirements are unchanged.
1833                match self.satisfies_requires_dist(
1834                    metadata.requires_dist,
1835                    metadata.dependency_groups,
1836                    package,
1837                    root,
1838                )? {
1839                    SatisfiesResult::Satisfied => {}
1840                    result => return Ok(result),
1841                }
1842            } else if let Some(source_tree) = package.id.source.as_source_tree() {
1843                // For dynamic packages, we don't need the version. We only need to know that the
1844                // package is still dynamic, and that the requirements are unchanged.
1845                //
1846                // If the distribution is a source tree, attempt to extract the requirements from the
1847                // `pyproject.toml` directly. The distribution database will do this too, but we can be
1848                // even more aggressive here since we _only_ need the requirements. So, for example,
1849                // even if the version is dynamic, we can still extract the requirements without
1850                // performing a build, unlike in the database where we typically construct a "complete"
1851                // metadata object.
1852                let parent = root.join(source_tree);
1853                let path = parent.join("pyproject.toml");
1854                let metadata =
1855                    match fs_err::tokio::read_to_string(&path).await {
1856                        Ok(contents) => {
1857                            let pyproject_toml = toml::from_str::<PyProjectToml>(&contents)
1858                                .map_err(|err| LockErrorKind::InvalidPyprojectToml {
1859                                    path: path.clone(),
1860                                    err,
1861                                })?;
1862                            database
1863                                .requires_dist(&parent, &pyproject_toml)
1864                                .await
1865                                .map_err(|err| LockErrorKind::Resolution {
1866                                    id: package.id.clone(),
1867                                    err,
1868                                })?
1869                        }
1870                        Err(err) if err.kind() == io::ErrorKind::NotFound => None,
1871                        Err(err) => {
1872                            return Err(LockErrorKind::UnreadablePyprojectToml { path, err }.into());
1873                        }
1874                    };
1875
1876                let satisfied = metadata.is_some_and(|metadata| {
1877                    // Validate that the package is still dynamic.
1878                    if !metadata.dynamic {
1879                        debug!("Static `requires-dist` for `{}` is out-of-date; falling back to distribution database", package.id);
1880                        return false;
1881                    }
1882
1883                    // Validate that the extras are unchanged.
1884                    if let SatisfiesResult::Satisfied = self.satisfies_provides_extra(metadata.provides_extra, package, ) {
1885                        debug!("Static `provides-extra` for `{}` is up-to-date", package.id);
1886                    } else {
1887                        debug!("Static `provides-extra` for `{}` is out-of-date; falling back to distribution database", package.id);
1888                        return false;
1889                    }
1890
1891                    // Validate that the requirements are unchanged.
1892                    match self.satisfies_requires_dist(metadata.requires_dist, metadata.dependency_groups, package, root) {
1893                        Ok(SatisfiesResult::Satisfied) => {
1894                            debug!("Static `requires-dist` for `{}` is up-to-date", package.id);
1895                        },
1896                        Ok(..) => {
1897                            debug!("Static `requires-dist` for `{}` is out-of-date; falling back to distribution database", package.id);
1898                            return false;
1899                        },
1900                        Err(..) => {
1901                            debug!("Static `requires-dist` for `{}` is invalid; falling back to distribution database", package.id);
1902                            return false;
1903                        },
1904                    }
1905
1906                    true
1907                });
1908
1909                // If the `requires-dist` metadata matches the requirements, we're done; otherwise,
1910                // fetch the "full" metadata, which may involve invoking the build system. In some
1911                // cases, build backends return metadata that does _not_ match the `pyproject.toml`
1912                // exactly. For example, `hatchling` will flatten any recursive (or self-referential)
1913                // extras, while `setuptools` will not.
1914                if !satisfied {
1915                    let dist = package.to_dist(
1916                        root,
1917                        TagPolicy::Preferred(tags),
1918                        &BuildOptions::default(),
1919                        markers,
1920                    )?;
1921
1922                    let metadata = {
1923                        let id = dist.version_id();
1924                        if let Some(archive) =
1925                            index
1926                                .distributions()
1927                                .get(&id)
1928                                .as_deref()
1929                                .and_then(|response| {
1930                                    if let MetadataResponse::Found(archive, ..) = response {
1931                                        Some(archive)
1932                                    } else {
1933                                        None
1934                                    }
1935                                })
1936                        {
1937                            // If the metadata is already in the index, return it.
1938                            archive.metadata.clone()
1939                        } else {
1940                            // Run the PEP 517 build process to extract metadata from the source distribution.
1941                            let archive = database
1942                                .get_or_build_wheel_metadata(&dist, hasher.get(&dist))
1943                                .await
1944                                .map_err(|err| LockErrorKind::Resolution {
1945                                    id: package.id.clone(),
1946                                    err,
1947                                })?;
1948
1949                            let metadata = archive.metadata.clone();
1950
1951                            // Insert the metadata into the index.
1952                            index
1953                                .distributions()
1954                                .done(id, Arc::new(MetadataResponse::Found(archive)));
1955
1956                            metadata
1957                        }
1958                    };
1959
1960                    // Validate that the package is still dynamic.
1961                    if !metadata.dynamic {
1962                        return Ok(SatisfiesResult::MismatchedDynamic(&package.id.name, true));
1963                    }
1964
1965                    // Validate that the extras are unchanged.
1966                    match self.satisfies_provides_extra(metadata.provides_extra, package) {
1967                        SatisfiesResult::Satisfied => {}
1968                        result => return Ok(result),
1969                    }
1970
1971                    // Validate that the requirements are unchanged.
1972                    match self.satisfies_requires_dist(
1973                        metadata.requires_dist,
1974                        metadata.dependency_groups,
1975                        package,
1976                        root,
1977                    )? {
1978                        SatisfiesResult::Satisfied => {}
1979                        result => return Ok(result),
1980                    }
1981                }
1982            } else {
1983                return Ok(SatisfiesResult::MissingVersion(&package.id.name));
1984            }
1985
1986            // Add any explicit indexes to the list of known locals or remotes. These indexes may
1987            // not be available as top-level configuration (i.e., if they're defined within a
1988            // workspace member), but we already validated that the dependencies are up-to-date, so
1989            // we can consider them "available".
1990            for requirement in package
1991                .metadata
1992                .requires_dist
1993                .iter()
1994                .chain(package.metadata.dependency_groups.values().flatten())
1995            {
1996                if let RequirementSource::Registry {
1997                    index: Some(index), ..
1998                } = &requirement.source
1999                {
2000                    match &index.url {
2001                        IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
2002                            if let Some(remotes) = remotes.as_mut() {
2003                                remotes.insert(UrlString::from(
2004                                    index.url().without_credentials().as_ref(),
2005                                ));
2006                            }
2007                        }
2008                        IndexUrl::Path(url) => {
2009                            if let Some(locals) = locals.as_mut() {
2010                                if let Some(path) = url.to_file_path().ok().and_then(|path| {
2011                                    relative_to(&path, root)
2012                                        .or_else(|_| std::path::absolute(path))
2013                                        .ok()
2014                                }) {
2015                                    locals.insert(path.into_boxed_path());
2016                                }
2017                            }
2018                        }
2019                    }
2020                }
2021            }
2022
2023            // Recurse.
2024            for dep in &package.dependencies {
2025                if seen.insert(&dep.package_id) {
2026                    let dep_dist = self.find_by_id(&dep.package_id);
2027                    queue.push_back(dep_dist);
2028                }
2029            }
2030
2031            for dependencies in package.optional_dependencies.values() {
2032                for dep in dependencies {
2033                    if seen.insert(&dep.package_id) {
2034                        let dep_dist = self.find_by_id(&dep.package_id);
2035                        queue.push_back(dep_dist);
2036                    }
2037                }
2038            }
2039
2040            for dependencies in package.dependency_groups.values() {
2041                for dep in dependencies {
2042                    if seen.insert(&dep.package_id) {
2043                        let dep_dist = self.find_by_id(&dep.package_id);
2044                        queue.push_back(dep_dist);
2045                    }
2046                }
2047            }
2048        }
2049
2050        Ok(SatisfiesResult::Satisfied)
2051    }
2052}
2053
2054#[derive(Debug, Copy, Clone)]
2055enum TagPolicy<'tags> {
2056    /// Exclusively consider wheels that match the specified platform tags.
2057    Required(&'tags Tags),
2058    /// Prefer wheels that match the specified platform tags, but fall back to incompatible wheels
2059    /// if necessary.
2060    Preferred(&'tags Tags),
2061}
2062
2063impl<'tags> TagPolicy<'tags> {
2064    /// Returns the platform tags to consider.
2065    fn tags(&self) -> &'tags Tags {
2066        match self {
2067            Self::Required(tags) | Self::Preferred(tags) => tags,
2068        }
2069    }
2070}
2071
2072/// The result of checking if a lockfile satisfies a set of requirements.
2073#[derive(Debug)]
2074pub enum SatisfiesResult<'lock> {
2075    /// The lockfile satisfies the requirements.
2076    Satisfied,
2077    /// The lockfile uses a different set of workspace members.
2078    MismatchedMembers(BTreeSet<PackageName>, &'lock BTreeSet<PackageName>),
2079    /// A workspace member switched from virtual to non-virtual or vice versa.
2080    MismatchedVirtual(PackageName, bool),
2081    /// A workspace member switched from editable to non-editable or vice versa.
2082    MismatchedEditable(PackageName, bool),
2083    /// A source tree switched from dynamic to non-dynamic or vice versa.
2084    MismatchedDynamic(&'lock PackageName, bool),
2085    /// The lockfile uses a different set of version for its workspace members.
2086    MismatchedVersion(&'lock PackageName, Version, Option<Version>),
2087    /// The lockfile uses a different set of requirements.
2088    MismatchedRequirements(BTreeSet<Requirement>, BTreeSet<Requirement>),
2089    /// The lockfile uses a different set of constraints.
2090    MismatchedConstraints(BTreeSet<Requirement>, BTreeSet<Requirement>),
2091    /// The lockfile uses a different set of overrides.
2092    MismatchedOverrides(BTreeSet<Requirement>, BTreeSet<Requirement>),
2093    /// The lockfile uses a different set of excludes.
2094    MismatchedExcludes(BTreeSet<PackageName>, BTreeSet<PackageName>),
2095    /// The lockfile uses a different set of build constraints.
2096    MismatchedBuildConstraints(BTreeSet<Requirement>, BTreeSet<Requirement>),
2097    /// The lockfile uses a different set of dependency groups.
2098    MismatchedDependencyGroups(
2099        BTreeMap<GroupName, BTreeSet<Requirement>>,
2100        BTreeMap<GroupName, BTreeSet<Requirement>>,
2101    ),
2102    /// The lockfile uses different static metadata.
2103    MismatchedStaticMetadata(BTreeSet<StaticMetadata>, &'lock BTreeSet<StaticMetadata>),
2104    /// The lockfile is missing a workspace member.
2105    MissingRoot(PackageName),
2106    /// The lockfile referenced a remote index that was not provided
2107    MissingRemoteIndex(&'lock PackageName, &'lock Version, &'lock UrlString),
2108    /// The lockfile referenced a local index that was not provided
2109    MissingLocalIndex(&'lock PackageName, &'lock Version, &'lock Path),
2110    /// A package in the lockfile contains different `requires-dist` metadata than expected.
2111    MismatchedPackageRequirements(
2112        &'lock PackageName,
2113        Option<&'lock Version>,
2114        BTreeSet<Requirement>,
2115        BTreeSet<Requirement>,
2116    ),
2117    /// A package in the lockfile contains different `provides-extra` metadata than expected.
2118    MismatchedPackageProvidesExtra(
2119        &'lock PackageName,
2120        Option<&'lock Version>,
2121        BTreeSet<ExtraName>,
2122        BTreeSet<&'lock ExtraName>,
2123    ),
2124    /// A package in the lockfile contains different `dependency-groups` metadata than expected.
2125    MismatchedPackageDependencyGroups(
2126        &'lock PackageName,
2127        Option<&'lock Version>,
2128        BTreeMap<GroupName, BTreeSet<Requirement>>,
2129        BTreeMap<GroupName, BTreeSet<Requirement>>,
2130    ),
2131    /// The lockfile is missing a version.
2132    MissingVersion(&'lock PackageName),
2133}
2134
2135/// We discard the lockfile if these options match.
2136#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq, Eq)]
2137#[serde(rename_all = "kebab-case")]
2138struct ResolverOptions {
2139    /// The [`ResolutionMode`] used to generate this lock.
2140    #[serde(default)]
2141    resolution_mode: ResolutionMode,
2142    /// The [`PrereleaseMode`] used to generate this lock.
2143    #[serde(default)]
2144    prerelease_mode: PrereleaseMode,
2145    /// The [`ForkStrategy`] used to generate this lock.
2146    #[serde(default)]
2147    fork_strategy: ForkStrategy,
2148    /// The [`ExcludeNewer`] setting used to generate this lock.
2149    #[serde(flatten)]
2150    exclude_newer: ExcludeNewerWire,
2151}
2152
2153#[allow(clippy::struct_field_names)]
2154#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq, Eq)]
2155#[serde(rename_all = "kebab-case")]
2156struct ExcludeNewerWire {
2157    exclude_newer: Option<Timestamp>,
2158    exclude_newer_span: Option<ExcludeNewerSpan>,
2159    #[serde(default, skip_serializing_if = "ExcludeNewerPackage::is_empty")]
2160    exclude_newer_package: ExcludeNewerPackage,
2161}
2162
2163impl From<ExcludeNewerWire> for ExcludeNewer {
2164    fn from(wire: ExcludeNewerWire) -> Self {
2165        Self {
2166            global: wire
2167                .exclude_newer
2168                .map(|timestamp| ExcludeNewerValue::new(timestamp, wire.exclude_newer_span)),
2169            package: wire.exclude_newer_package,
2170        }
2171    }
2172}
2173
2174impl From<ExcludeNewer> for ExcludeNewerWire {
2175    fn from(exclude_newer: ExcludeNewer) -> Self {
2176        let (timestamp, span) = exclude_newer
2177            .global
2178            .map(ExcludeNewerValue::into_parts)
2179            .map_or((None, None), |(t, s)| (Some(t), s));
2180        Self {
2181            exclude_newer: timestamp,
2182            exclude_newer_span: span,
2183            exclude_newer_package: exclude_newer.package,
2184        }
2185    }
2186}
2187
2188#[derive(Clone, Debug, Default, serde::Deserialize, PartialEq, Eq)]
2189#[serde(rename_all = "kebab-case")]
2190pub struct ResolverManifest {
2191    /// The workspace members included in the lockfile.
2192    #[serde(default)]
2193    members: BTreeSet<PackageName>,
2194    /// The requirements provided to the resolver, exclusive of the workspace members.
2195    ///
2196    /// These are requirements that are attached to the project, but not to any of its
2197    /// workspace members. For example, the requirements in a PEP 723 script would be included here.
2198    #[serde(default)]
2199    requirements: BTreeSet<Requirement>,
2200    /// The dependency groups provided to the resolver, exclusive of the workspace members.
2201    ///
2202    /// These are dependency groups that are attached to the project, but not to any of its
2203    /// workspace members. For example, the dependency groups in a `pyproject.toml` without a
2204    /// `[project]` table would be included here.
2205    #[serde(default)]
2206    dependency_groups: BTreeMap<GroupName, BTreeSet<Requirement>>,
2207    /// The constraints provided to the resolver.
2208    #[serde(default)]
2209    constraints: BTreeSet<Requirement>,
2210    /// The overrides provided to the resolver.
2211    #[serde(default)]
2212    overrides: BTreeSet<Requirement>,
2213    /// The excludes provided to the resolver.
2214    #[serde(default)]
2215    excludes: BTreeSet<PackageName>,
2216    /// The build constraints provided to the resolver.
2217    #[serde(default)]
2218    build_constraints: BTreeSet<Requirement>,
2219    /// The static metadata provided to the resolver.
2220    #[serde(default)]
2221    dependency_metadata: BTreeSet<StaticMetadata>,
2222}
2223
2224impl ResolverManifest {
2225    /// Initialize a [`ResolverManifest`] with the given members, requirements, constraints, and
2226    /// overrides.
2227    pub fn new(
2228        members: impl IntoIterator<Item = PackageName>,
2229        requirements: impl IntoIterator<Item = Requirement>,
2230        constraints: impl IntoIterator<Item = Requirement>,
2231        overrides: impl IntoIterator<Item = Requirement>,
2232        excludes: impl IntoIterator<Item = PackageName>,
2233        build_constraints: impl IntoIterator<Item = Requirement>,
2234        dependency_groups: impl IntoIterator<Item = (GroupName, Vec<Requirement>)>,
2235        dependency_metadata: impl IntoIterator<Item = StaticMetadata>,
2236    ) -> Self {
2237        Self {
2238            members: members.into_iter().collect(),
2239            requirements: requirements.into_iter().collect(),
2240            constraints: constraints.into_iter().collect(),
2241            overrides: overrides.into_iter().collect(),
2242            excludes: excludes.into_iter().collect(),
2243            build_constraints: build_constraints.into_iter().collect(),
2244            dependency_groups: dependency_groups
2245                .into_iter()
2246                .map(|(group, requirements)| (group, requirements.into_iter().collect()))
2247                .collect(),
2248            dependency_metadata: dependency_metadata.into_iter().collect(),
2249        }
2250    }
2251
2252    /// Convert the manifest to a relative form using the given workspace.
2253    pub fn relative_to(self, root: &Path) -> Result<Self, io::Error> {
2254        Ok(Self {
2255            members: self.members,
2256            requirements: self
2257                .requirements
2258                .into_iter()
2259                .map(|requirement| requirement.relative_to(root))
2260                .collect::<Result<BTreeSet<_>, _>>()?,
2261            constraints: self
2262                .constraints
2263                .into_iter()
2264                .map(|requirement| requirement.relative_to(root))
2265                .collect::<Result<BTreeSet<_>, _>>()?,
2266            overrides: self
2267                .overrides
2268                .into_iter()
2269                .map(|requirement| requirement.relative_to(root))
2270                .collect::<Result<BTreeSet<_>, _>>()?,
2271            excludes: self.excludes,
2272            build_constraints: self
2273                .build_constraints
2274                .into_iter()
2275                .map(|requirement| requirement.relative_to(root))
2276                .collect::<Result<BTreeSet<_>, _>>()?,
2277            dependency_groups: self
2278                .dependency_groups
2279                .into_iter()
2280                .map(|(group, requirements)| {
2281                    Ok::<_, io::Error>((
2282                        group,
2283                        requirements
2284                            .into_iter()
2285                            .map(|requirement| requirement.relative_to(root))
2286                            .collect::<Result<BTreeSet<_>, _>>()?,
2287                    ))
2288                })
2289                .collect::<Result<BTreeMap<_, _>, _>>()?,
2290            dependency_metadata: self.dependency_metadata,
2291        })
2292    }
2293}
2294
2295#[derive(Clone, Debug, serde::Deserialize)]
2296#[serde(rename_all = "kebab-case")]
2297struct LockWire {
2298    version: u32,
2299    revision: Option<u32>,
2300    requires_python: RequiresPython,
2301    /// If this lockfile was built from a forking resolution with non-identical forks, store the
2302    /// forks in the lockfile so we can recreate them in subsequent resolutions.
2303    #[serde(rename = "resolution-markers", default)]
2304    fork_markers: Vec<SimplifiedMarkerTree>,
2305    #[serde(rename = "supported-markers", default)]
2306    supported_environments: Vec<SimplifiedMarkerTree>,
2307    #[serde(rename = "required-markers", default)]
2308    required_environments: Vec<SimplifiedMarkerTree>,
2309    #[serde(rename = "conflicts", default)]
2310    conflicts: Option<Conflicts>,
2311    /// We discard the lockfile if these options match.
2312    #[serde(default)]
2313    options: ResolverOptions,
2314    #[serde(default)]
2315    manifest: ResolverManifest,
2316    #[serde(rename = "package", alias = "distribution", default)]
2317    packages: Vec<PackageWire>,
2318}
2319
2320impl TryFrom<LockWire> for Lock {
2321    type Error = LockError;
2322
2323    fn try_from(wire: LockWire) -> Result<Self, LockError> {
2324        // Count the number of sources for each package name. When
2325        // there's only one source for a particular package name (the
2326        // overwhelmingly common case), we can omit some data (like source and
2327        // version) on dependency edges since it is strictly redundant.
2328        let mut unambiguous_package_ids: FxHashMap<PackageName, PackageId> = FxHashMap::default();
2329        let mut ambiguous = FxHashSet::default();
2330        for dist in &wire.packages {
2331            if ambiguous.contains(&dist.id.name) {
2332                continue;
2333            }
2334            if let Some(id) = unambiguous_package_ids.remove(&dist.id.name) {
2335                ambiguous.insert(id.name);
2336                continue;
2337            }
2338            unambiguous_package_ids.insert(dist.id.name.clone(), dist.id.clone());
2339        }
2340
2341        let packages = wire
2342            .packages
2343            .into_iter()
2344            .map(|dist| dist.unwire(&wire.requires_python, &unambiguous_package_ids))
2345            .collect::<Result<Vec<_>, _>>()?;
2346        let supported_environments = wire
2347            .supported_environments
2348            .into_iter()
2349            .map(|simplified_marker| simplified_marker.into_marker(&wire.requires_python))
2350            .collect();
2351        let required_environments = wire
2352            .required_environments
2353            .into_iter()
2354            .map(|simplified_marker| simplified_marker.into_marker(&wire.requires_python))
2355            .collect();
2356        let fork_markers = wire
2357            .fork_markers
2358            .into_iter()
2359            .map(|simplified_marker| simplified_marker.into_marker(&wire.requires_python))
2360            .map(UniversalMarker::from_combined)
2361            .collect();
2362        let lock = Self::new(
2363            wire.version,
2364            wire.revision.unwrap_or(0),
2365            packages,
2366            wire.requires_python,
2367            wire.options,
2368            wire.manifest,
2369            wire.conflicts.unwrap_or_else(Conflicts::empty),
2370            supported_environments,
2371            required_environments,
2372            fork_markers,
2373        )?;
2374
2375        Ok(lock)
2376    }
2377}
2378
2379/// Like [`Lock`], but limited to the version field. Used for error reporting: by limiting parsing
2380/// to the version field, we can verify compatibility for lockfiles that may otherwise be
2381/// unparsable.
2382#[derive(Clone, Debug, serde::Deserialize)]
2383#[serde(rename_all = "kebab-case")]
2384pub struct LockVersion {
2385    version: u32,
2386}
2387
2388impl LockVersion {
2389    /// Returns the lockfile version.
2390    pub fn version(&self) -> u32 {
2391        self.version
2392    }
2393}
2394
2395#[derive(Clone, Debug, PartialEq, Eq)]
2396pub struct Package {
2397    pub(crate) id: PackageId,
2398    sdist: Option<SourceDist>,
2399    wheels: Vec<Wheel>,
2400    /// If there are multiple versions or sources for the same package name, we add the markers of
2401    /// the fork(s) that contained this version or source, so we can set the correct preferences in
2402    /// the next resolution.
2403    ///
2404    /// Named `resolution-markers` in `uv.lock`.
2405    fork_markers: Vec<UniversalMarker>,
2406    /// The resolved dependencies of the package.
2407    dependencies: Vec<Dependency>,
2408    /// The resolved optional dependencies of the package.
2409    optional_dependencies: BTreeMap<ExtraName, Vec<Dependency>>,
2410    /// The resolved PEP 735 dependency groups of the package.
2411    dependency_groups: BTreeMap<GroupName, Vec<Dependency>>,
2412    /// The exact requirements from the package metadata.
2413    metadata: PackageMetadata,
2414}
2415
2416impl Package {
2417    fn from_annotated_dist(
2418        annotated_dist: &AnnotatedDist,
2419        fork_markers: Vec<UniversalMarker>,
2420        root: &Path,
2421    ) -> Result<Self, LockError> {
2422        let id = PackageId::from_annotated_dist(annotated_dist, root)?;
2423        let sdist = SourceDist::from_annotated_dist(&id, annotated_dist)?;
2424        let wheels = Wheel::from_annotated_dist(annotated_dist)?;
2425        let requires_dist = if id.source.is_immutable() {
2426            BTreeSet::default()
2427        } else {
2428            annotated_dist
2429                .metadata
2430                .as_ref()
2431                .expect("metadata is present")
2432                .requires_dist
2433                .iter()
2434                .cloned()
2435                .map(|requirement| requirement.relative_to(root))
2436                .collect::<Result<_, _>>()
2437                .map_err(LockErrorKind::RequirementRelativePath)?
2438        };
2439        let provides_extra = if id.source.is_immutable() {
2440            Box::default()
2441        } else {
2442            annotated_dist
2443                .metadata
2444                .as_ref()
2445                .expect("metadata is present")
2446                .provides_extra
2447                .clone()
2448        };
2449        let dependency_groups = if id.source.is_immutable() {
2450            BTreeMap::default()
2451        } else {
2452            annotated_dist
2453                .metadata
2454                .as_ref()
2455                .expect("metadata is present")
2456                .dependency_groups
2457                .iter()
2458                .map(|(group, requirements)| {
2459                    let requirements = requirements
2460                        .iter()
2461                        .cloned()
2462                        .map(|requirement| requirement.relative_to(root))
2463                        .collect::<Result<_, _>>()
2464                        .map_err(LockErrorKind::RequirementRelativePath)?;
2465                    Ok::<_, LockError>((group.clone(), requirements))
2466                })
2467                .collect::<Result<_, _>>()?
2468        };
2469        Ok(Self {
2470            id,
2471            sdist,
2472            wheels,
2473            fork_markers,
2474            dependencies: vec![],
2475            optional_dependencies: BTreeMap::default(),
2476            dependency_groups: BTreeMap::default(),
2477            metadata: PackageMetadata {
2478                requires_dist,
2479                provides_extra,
2480                dependency_groups,
2481            },
2482        })
2483    }
2484
2485    /// Add the [`AnnotatedDist`] as a dependency of the [`Package`].
2486    fn add_dependency(
2487        &mut self,
2488        requires_python: &RequiresPython,
2489        annotated_dist: &AnnotatedDist,
2490        marker: UniversalMarker,
2491        root: &Path,
2492    ) -> Result<(), LockError> {
2493        let new_dep =
2494            Dependency::from_annotated_dist(requires_python, annotated_dist, marker, root)?;
2495        for existing_dep in &mut self.dependencies {
2496            if existing_dep.package_id == new_dep.package_id
2497                // It's important that we do a comparison on
2498                // *simplified* markers here. In particular, when
2499                // we write markers out to the lock file, we use
2500                // "simplified" markers, or markers that are simplified
2501                // *given* that `requires-python` is satisfied. So if
2502                // we don't do equality based on what the simplified
2503                // marker is, we might wind up not merging dependencies
2504                // that ought to be merged and thus writing out extra
2505                // entries.
2506                //
2507                // For example, if `requires-python = '>=3.8'` and we
2508                // have `foo==1` and
2509                // `foo==1 ; python_version >= '3.8'` dependencies,
2510                // then they don't have equivalent complexified
2511                // markers, but their simplified markers are identical.
2512                //
2513                // NOTE: It does seem like perhaps this should
2514                // be implemented semantically/algebraically on
2515                // `MarkerTree` itself, but it wasn't totally clear
2516                // how to do that. I think `pep508` would need to
2517                // grow a concept of "requires python" and provide an
2518                // operation specifically for that.
2519                && existing_dep.simplified_marker == new_dep.simplified_marker
2520            {
2521                existing_dep.extra.extend(new_dep.extra);
2522                return Ok(());
2523            }
2524        }
2525
2526        self.dependencies.push(new_dep);
2527        Ok(())
2528    }
2529
2530    /// Add the [`AnnotatedDist`] as an optional dependency of the [`Package`].
2531    fn add_optional_dependency(
2532        &mut self,
2533        requires_python: &RequiresPython,
2534        extra: ExtraName,
2535        annotated_dist: &AnnotatedDist,
2536        marker: UniversalMarker,
2537        root: &Path,
2538    ) -> Result<(), LockError> {
2539        let dep = Dependency::from_annotated_dist(requires_python, annotated_dist, marker, root)?;
2540        let optional_deps = self.optional_dependencies.entry(extra).or_default();
2541        for existing_dep in &mut *optional_deps {
2542            if existing_dep.package_id == dep.package_id
2543                // See note in add_dependency for why we use
2544                // simplified markers here.
2545                && existing_dep.simplified_marker == dep.simplified_marker
2546            {
2547                existing_dep.extra.extend(dep.extra);
2548                return Ok(());
2549            }
2550        }
2551
2552        optional_deps.push(dep);
2553        Ok(())
2554    }
2555
2556    /// Add the [`AnnotatedDist`] to a dependency group of the [`Package`].
2557    fn add_group_dependency(
2558        &mut self,
2559        requires_python: &RequiresPython,
2560        group: GroupName,
2561        annotated_dist: &AnnotatedDist,
2562        marker: UniversalMarker,
2563        root: &Path,
2564    ) -> Result<(), LockError> {
2565        let dep = Dependency::from_annotated_dist(requires_python, annotated_dist, marker, root)?;
2566        let deps = self.dependency_groups.entry(group).or_default();
2567        for existing_dep in &mut *deps {
2568            if existing_dep.package_id == dep.package_id
2569                // See note in add_dependency for why we use
2570                // simplified markers here.
2571                && existing_dep.simplified_marker == dep.simplified_marker
2572            {
2573                existing_dep.extra.extend(dep.extra);
2574                return Ok(());
2575            }
2576        }
2577
2578        deps.push(dep);
2579        Ok(())
2580    }
2581
2582    /// Convert the [`Package`] to a [`Dist`] that can be used in installation.
2583    fn to_dist(
2584        &self,
2585        workspace_root: &Path,
2586        tag_policy: TagPolicy<'_>,
2587        build_options: &BuildOptions,
2588        markers: &MarkerEnvironment,
2589    ) -> Result<Dist, LockError> {
2590        let no_binary = build_options.no_binary_package(&self.id.name);
2591        let no_build = build_options.no_build_package(&self.id.name);
2592
2593        if !no_binary {
2594            if let Some(best_wheel_index) = self.find_best_wheel(tag_policy) {
2595                return match &self.id.source {
2596                    Source::Registry(source) => {
2597                        let wheels = self
2598                            .wheels
2599                            .iter()
2600                            .map(|wheel| wheel.to_registry_wheel(source, workspace_root))
2601                            .collect::<Result<_, LockError>>()?;
2602                        let reg_built_dist = RegistryBuiltDist {
2603                            wheels,
2604                            best_wheel_index,
2605                            sdist: None,
2606                        };
2607                        Ok(Dist::Built(BuiltDist::Registry(reg_built_dist)))
2608                    }
2609                    Source::Path(path) => {
2610                        let filename: WheelFilename =
2611                            self.wheels[best_wheel_index].filename.clone();
2612                        let install_path = absolute_path(workspace_root, path)?;
2613                        let path_dist = PathBuiltDist {
2614                            filename,
2615                            url: verbatim_url(&install_path, &self.id)?,
2616                            install_path: absolute_path(workspace_root, path)?.into_boxed_path(),
2617                        };
2618                        let built_dist = BuiltDist::Path(path_dist);
2619                        Ok(Dist::Built(built_dist))
2620                    }
2621                    Source::Direct(url, direct) => {
2622                        let filename: WheelFilename =
2623                            self.wheels[best_wheel_index].filename.clone();
2624                        let url = DisplaySafeUrl::from(ParsedArchiveUrl {
2625                            url: url.to_url().map_err(LockErrorKind::InvalidUrl)?,
2626                            subdirectory: direct.subdirectory.clone(),
2627                            ext: DistExtension::Wheel,
2628                        });
2629                        let direct_dist = DirectUrlBuiltDist {
2630                            filename,
2631                            location: Box::new(url.clone()),
2632                            url: VerbatimUrl::from_url(url),
2633                        };
2634                        let built_dist = BuiltDist::DirectUrl(direct_dist);
2635                        Ok(Dist::Built(built_dist))
2636                    }
2637                    Source::Git(_, _) => Err(LockErrorKind::InvalidWheelSource {
2638                        id: self.id.clone(),
2639                        source_type: "Git",
2640                    }
2641                    .into()),
2642                    Source::Directory(_) => Err(LockErrorKind::InvalidWheelSource {
2643                        id: self.id.clone(),
2644                        source_type: "directory",
2645                    }
2646                    .into()),
2647                    Source::Editable(_) => Err(LockErrorKind::InvalidWheelSource {
2648                        id: self.id.clone(),
2649                        source_type: "editable",
2650                    }
2651                    .into()),
2652                    Source::Virtual(_) => Err(LockErrorKind::InvalidWheelSource {
2653                        id: self.id.clone(),
2654                        source_type: "virtual",
2655                    }
2656                    .into()),
2657                };
2658            }
2659        }
2660
2661        if let Some(sdist) = self.to_source_dist(workspace_root)? {
2662            // Even with `--no-build`, allow virtual packages. (In the future, we may want to allow
2663            // any local source tree, or at least editable source trees, which we allow in
2664            // `uv pip`.)
2665            if !no_build || sdist.is_virtual() {
2666                return Ok(Dist::Source(sdist));
2667            }
2668        }
2669
2670        match (no_binary, no_build) {
2671            (true, true) => Err(LockErrorKind::NoBinaryNoBuild {
2672                id: self.id.clone(),
2673            }
2674            .into()),
2675            (true, false) if self.id.source.is_wheel() => Err(LockErrorKind::NoBinaryWheelOnly {
2676                id: self.id.clone(),
2677            }
2678            .into()),
2679            (true, false) => Err(LockErrorKind::NoBinary {
2680                id: self.id.clone(),
2681            }
2682            .into()),
2683            (false, true) => Err(LockErrorKind::NoBuild {
2684                id: self.id.clone(),
2685            }
2686            .into()),
2687            (false, false) if self.id.source.is_wheel() => Err(LockError {
2688                kind: Box::new(LockErrorKind::IncompatibleWheelOnly {
2689                    id: self.id.clone(),
2690                }),
2691                hint: self.tag_hint(tag_policy, markers),
2692            }),
2693            (false, false) => Err(LockError {
2694                kind: Box::new(LockErrorKind::NeitherSourceDistNorWheel {
2695                    id: self.id.clone(),
2696                }),
2697                hint: self.tag_hint(tag_policy, markers),
2698            }),
2699        }
2700    }
2701
2702    /// Generate a [`WheelTagHint`] based on wheel-tag incompatibilities.
2703    fn tag_hint(
2704        &self,
2705        tag_policy: TagPolicy<'_>,
2706        markers: &MarkerEnvironment,
2707    ) -> Option<WheelTagHint> {
2708        let filenames = self
2709            .wheels
2710            .iter()
2711            .map(|wheel| &wheel.filename)
2712            .collect::<Vec<_>>();
2713        WheelTagHint::from_wheels(
2714            &self.id.name,
2715            self.id.version.as_ref(),
2716            &filenames,
2717            tag_policy.tags(),
2718            markers,
2719        )
2720    }
2721
2722    /// Convert the source of this [`Package`] to a [`SourceDist`] that can be used in installation.
2723    ///
2724    /// Returns `Ok(None)` if the source cannot be converted because `self.sdist` is `None`. This is required
2725    /// for registry sources.
2726    fn to_source_dist(
2727        &self,
2728        workspace_root: &Path,
2729    ) -> Result<Option<uv_distribution_types::SourceDist>, LockError> {
2730        let sdist = match &self.id.source {
2731            Source::Path(path) => {
2732                // A direct path source can also be a wheel, so validate the extension.
2733                let DistExtension::Source(ext) = DistExtension::from_path(path).map_err(|err| {
2734                    LockErrorKind::MissingExtension {
2735                        id: self.id.clone(),
2736                        err,
2737                    }
2738                })?
2739                else {
2740                    return Ok(None);
2741                };
2742                let install_path = absolute_path(workspace_root, path)?;
2743                let path_dist = PathSourceDist {
2744                    name: self.id.name.clone(),
2745                    version: self.id.version.clone(),
2746                    url: verbatim_url(&install_path, &self.id)?,
2747                    install_path: install_path.into_boxed_path(),
2748                    ext,
2749                };
2750                uv_distribution_types::SourceDist::Path(path_dist)
2751            }
2752            Source::Directory(path) => {
2753                let install_path = absolute_path(workspace_root, path)?;
2754                let dir_dist = DirectorySourceDist {
2755                    name: self.id.name.clone(),
2756                    url: verbatim_url(&install_path, &self.id)?,
2757                    install_path: install_path.into_boxed_path(),
2758                    editable: Some(false),
2759                    r#virtual: Some(false),
2760                };
2761                uv_distribution_types::SourceDist::Directory(dir_dist)
2762            }
2763            Source::Editable(path) => {
2764                let install_path = absolute_path(workspace_root, path)?;
2765                let dir_dist = DirectorySourceDist {
2766                    name: self.id.name.clone(),
2767                    url: verbatim_url(&install_path, &self.id)?,
2768                    install_path: install_path.into_boxed_path(),
2769                    editable: Some(true),
2770                    r#virtual: Some(false),
2771                };
2772                uv_distribution_types::SourceDist::Directory(dir_dist)
2773            }
2774            Source::Virtual(path) => {
2775                let install_path = absolute_path(workspace_root, path)?;
2776                let dir_dist = DirectorySourceDist {
2777                    name: self.id.name.clone(),
2778                    url: verbatim_url(&install_path, &self.id)?,
2779                    install_path: install_path.into_boxed_path(),
2780                    editable: Some(false),
2781                    r#virtual: Some(true),
2782                };
2783                uv_distribution_types::SourceDist::Directory(dir_dist)
2784            }
2785            Source::Git(url, git) => {
2786                // Remove the fragment and query from the URL; they're already present in the
2787                // `GitSource`.
2788                let mut url = url.to_url().map_err(LockErrorKind::InvalidUrl)?;
2789                url.set_fragment(None);
2790                url.set_query(None);
2791
2792                // Reconstruct the `GitUrl` from the `GitSource`.
2793                let git_url = GitUrl::from_commit(
2794                    url,
2795                    GitReference::from(git.kind.clone()),
2796                    git.precise,
2797                    git.lfs,
2798                )?;
2799
2800                // Reconstruct the PEP 508-compatible URL from the `GitSource`.
2801                let url = DisplaySafeUrl::from(ParsedGitUrl {
2802                    url: git_url.clone(),
2803                    subdirectory: git.subdirectory.clone(),
2804                });
2805
2806                let git_dist = GitSourceDist {
2807                    name: self.id.name.clone(),
2808                    url: VerbatimUrl::from_url(url),
2809                    git: Box::new(git_url),
2810                    subdirectory: git.subdirectory.clone(),
2811                };
2812                uv_distribution_types::SourceDist::Git(git_dist)
2813            }
2814            Source::Direct(url, direct) => {
2815                // A direct URL source can also be a wheel, so validate the extension.
2816                let DistExtension::Source(ext) =
2817                    DistExtension::from_path(url.base_str()).map_err(|err| {
2818                        LockErrorKind::MissingExtension {
2819                            id: self.id.clone(),
2820                            err,
2821                        }
2822                    })?
2823                else {
2824                    return Ok(None);
2825                };
2826                let location = url.to_url().map_err(LockErrorKind::InvalidUrl)?;
2827                let url = DisplaySafeUrl::from(ParsedArchiveUrl {
2828                    url: location.clone(),
2829                    subdirectory: direct.subdirectory.clone(),
2830                    ext: DistExtension::Source(ext),
2831                });
2832                let direct_dist = DirectUrlSourceDist {
2833                    name: self.id.name.clone(),
2834                    location: Box::new(location),
2835                    subdirectory: direct.subdirectory.clone(),
2836                    ext,
2837                    url: VerbatimUrl::from_url(url),
2838                };
2839                uv_distribution_types::SourceDist::DirectUrl(direct_dist)
2840            }
2841            Source::Registry(RegistrySource::Url(url)) => {
2842                let Some(ref sdist) = self.sdist else {
2843                    return Ok(None);
2844                };
2845
2846                let name = &self.id.name;
2847                let version = self
2848                    .id
2849                    .version
2850                    .as_ref()
2851                    .expect("version for registry source");
2852
2853                let file_url = sdist.url().ok_or_else(|| LockErrorKind::MissingUrl {
2854                    name: name.clone(),
2855                    version: version.clone(),
2856                })?;
2857                let filename = sdist
2858                    .filename()
2859                    .ok_or_else(|| LockErrorKind::MissingFilename {
2860                        id: self.id.clone(),
2861                    })?;
2862                let ext = SourceDistExtension::from_path(filename.as_ref()).map_err(|err| {
2863                    LockErrorKind::MissingExtension {
2864                        id: self.id.clone(),
2865                        err,
2866                    }
2867                })?;
2868                let file = Box::new(uv_distribution_types::File {
2869                    dist_info_metadata: false,
2870                    filename: SmallString::from(filename),
2871                    hashes: sdist.hash().map_or(HashDigests::empty(), |hash| {
2872                        HashDigests::from(hash.0.clone())
2873                    }),
2874                    requires_python: None,
2875                    size: sdist.size(),
2876                    upload_time_utc_ms: sdist.upload_time().map(Timestamp::as_millisecond),
2877                    url: FileLocation::AbsoluteUrl(file_url.clone()),
2878                    yanked: None,
2879                    zstd: None,
2880                });
2881
2882                let index = IndexUrl::from(VerbatimUrl::from_url(
2883                    url.to_url().map_err(LockErrorKind::InvalidUrl)?,
2884                ));
2885
2886                let reg_dist = RegistrySourceDist {
2887                    name: name.clone(),
2888                    version: version.clone(),
2889                    file,
2890                    ext,
2891                    index,
2892                    wheels: vec![],
2893                };
2894                uv_distribution_types::SourceDist::Registry(reg_dist)
2895            }
2896            Source::Registry(RegistrySource::Path(path)) => {
2897                let Some(ref sdist) = self.sdist else {
2898                    return Ok(None);
2899                };
2900
2901                let name = &self.id.name;
2902                let version = self
2903                    .id
2904                    .version
2905                    .as_ref()
2906                    .expect("version for registry source");
2907
2908                let file_url = match sdist {
2909                    SourceDist::Url { url: file_url, .. } => {
2910                        FileLocation::AbsoluteUrl(file_url.clone())
2911                    }
2912                    SourceDist::Path {
2913                        path: file_path, ..
2914                    } => {
2915                        let file_path = workspace_root.join(path).join(file_path);
2916                        let file_url =
2917                            DisplaySafeUrl::from_file_path(&file_path).map_err(|()| {
2918                                LockErrorKind::PathToUrl {
2919                                    path: file_path.into_boxed_path(),
2920                                }
2921                            })?;
2922                        FileLocation::AbsoluteUrl(UrlString::from(file_url))
2923                    }
2924                    SourceDist::Metadata { .. } => {
2925                        return Err(LockErrorKind::MissingPath {
2926                            name: name.clone(),
2927                            version: version.clone(),
2928                        }
2929                        .into());
2930                    }
2931                };
2932                let filename = sdist
2933                    .filename()
2934                    .ok_or_else(|| LockErrorKind::MissingFilename {
2935                        id: self.id.clone(),
2936                    })?;
2937                let ext = SourceDistExtension::from_path(filename.as_ref()).map_err(|err| {
2938                    LockErrorKind::MissingExtension {
2939                        id: self.id.clone(),
2940                        err,
2941                    }
2942                })?;
2943                let file = Box::new(uv_distribution_types::File {
2944                    dist_info_metadata: false,
2945                    filename: SmallString::from(filename),
2946                    hashes: sdist.hash().map_or(HashDigests::empty(), |hash| {
2947                        HashDigests::from(hash.0.clone())
2948                    }),
2949                    requires_python: None,
2950                    size: sdist.size(),
2951                    upload_time_utc_ms: sdist.upload_time().map(Timestamp::as_millisecond),
2952                    url: file_url,
2953                    yanked: None,
2954                    zstd: None,
2955                });
2956
2957                let index = IndexUrl::from(
2958                    VerbatimUrl::from_absolute_path(workspace_root.join(path))
2959                        .map_err(LockErrorKind::RegistryVerbatimUrl)?,
2960                );
2961
2962                let reg_dist = RegistrySourceDist {
2963                    name: name.clone(),
2964                    version: version.clone(),
2965                    file,
2966                    ext,
2967                    index,
2968                    wheels: vec![],
2969                };
2970                uv_distribution_types::SourceDist::Registry(reg_dist)
2971            }
2972        };
2973
2974        Ok(Some(sdist))
2975    }
2976
2977    fn to_toml(
2978        &self,
2979        requires_python: &RequiresPython,
2980        dist_count_by_name: &FxHashMap<PackageName, u64>,
2981    ) -> Result<Table, toml_edit::ser::Error> {
2982        let mut table = Table::new();
2983
2984        self.id.to_toml(None, &mut table);
2985
2986        if !self.fork_markers.is_empty() {
2987            let fork_markers = each_element_on_its_line_array(
2988                simplified_universal_markers(&self.fork_markers, requires_python).into_iter(),
2989            );
2990            if !fork_markers.is_empty() {
2991                table.insert("resolution-markers", value(fork_markers));
2992            }
2993        }
2994
2995        if !self.dependencies.is_empty() {
2996            let deps = each_element_on_its_line_array(self.dependencies.iter().map(|dep| {
2997                dep.to_toml(requires_python, dist_count_by_name)
2998                    .into_inline_table()
2999            }));
3000            table.insert("dependencies", value(deps));
3001        }
3002
3003        if !self.optional_dependencies.is_empty() {
3004            let mut optional_deps = Table::new();
3005            for (extra, deps) in &self.optional_dependencies {
3006                let deps = each_element_on_its_line_array(deps.iter().map(|dep| {
3007                    dep.to_toml(requires_python, dist_count_by_name)
3008                        .into_inline_table()
3009                }));
3010                if !deps.is_empty() {
3011                    optional_deps.insert(extra.as_ref(), value(deps));
3012                }
3013            }
3014            if !optional_deps.is_empty() {
3015                table.insert("optional-dependencies", Item::Table(optional_deps));
3016            }
3017        }
3018
3019        if !self.dependency_groups.is_empty() {
3020            let mut dependency_groups = Table::new();
3021            for (extra, deps) in &self.dependency_groups {
3022                let deps = each_element_on_its_line_array(deps.iter().map(|dep| {
3023                    dep.to_toml(requires_python, dist_count_by_name)
3024                        .into_inline_table()
3025                }));
3026                if !deps.is_empty() {
3027                    dependency_groups.insert(extra.as_ref(), value(deps));
3028                }
3029            }
3030            if !dependency_groups.is_empty() {
3031                table.insert("dev-dependencies", Item::Table(dependency_groups));
3032            }
3033        }
3034
3035        if let Some(ref sdist) = self.sdist {
3036            table.insert("sdist", value(sdist.to_toml()?));
3037        }
3038
3039        if !self.wheels.is_empty() {
3040            let wheels = each_element_on_its_line_array(
3041                self.wheels
3042                    .iter()
3043                    .map(Wheel::to_toml)
3044                    .collect::<Result<Vec<_>, _>>()?
3045                    .into_iter(),
3046            );
3047            table.insert("wheels", value(wheels));
3048        }
3049
3050        // Write the package metadata, if non-empty.
3051        {
3052            let mut metadata_table = Table::new();
3053
3054            if !self.metadata.requires_dist.is_empty() {
3055                let requires_dist = self
3056                    .metadata
3057                    .requires_dist
3058                    .iter()
3059                    .map(|requirement| {
3060                        serde::Serialize::serialize(
3061                            &requirement,
3062                            toml_edit::ser::ValueSerializer::new(),
3063                        )
3064                    })
3065                    .collect::<Result<Vec<_>, _>>()?;
3066                let requires_dist = match requires_dist.as_slice() {
3067                    [] => Array::new(),
3068                    [requirement] => Array::from_iter([requirement]),
3069                    requires_dist => each_element_on_its_line_array(requires_dist.iter()),
3070                };
3071                metadata_table.insert("requires-dist", value(requires_dist));
3072            }
3073
3074            if !self.metadata.dependency_groups.is_empty() {
3075                let mut dependency_groups = Table::new();
3076                for (extra, deps) in &self.metadata.dependency_groups {
3077                    let deps = deps
3078                        .iter()
3079                        .map(|requirement| {
3080                            serde::Serialize::serialize(
3081                                &requirement,
3082                                toml_edit::ser::ValueSerializer::new(),
3083                            )
3084                        })
3085                        .collect::<Result<Vec<_>, _>>()?;
3086                    let deps = match deps.as_slice() {
3087                        [] => Array::new(),
3088                        [requirement] => Array::from_iter([requirement]),
3089                        deps => each_element_on_its_line_array(deps.iter()),
3090                    };
3091                    dependency_groups.insert(extra.as_ref(), value(deps));
3092                }
3093                if !dependency_groups.is_empty() {
3094                    metadata_table.insert("requires-dev", Item::Table(dependency_groups));
3095                }
3096            }
3097
3098            if !self.metadata.provides_extra.is_empty() {
3099                let provides_extras = self
3100                    .metadata
3101                    .provides_extra
3102                    .iter()
3103                    .map(|extra| {
3104                        serde::Serialize::serialize(&extra, toml_edit::ser::ValueSerializer::new())
3105                    })
3106                    .collect::<Result<Vec<_>, _>>()?;
3107                // This is just a list of names, so linebreaking it is excessive.
3108                let provides_extras = Array::from_iter(provides_extras);
3109                metadata_table.insert("provides-extras", value(provides_extras));
3110            }
3111
3112            if !metadata_table.is_empty() {
3113                table.insert("metadata", Item::Table(metadata_table));
3114            }
3115        }
3116
3117        Ok(table)
3118    }
3119
3120    fn find_best_wheel(&self, tag_policy: TagPolicy<'_>) -> Option<usize> {
3121        type WheelPriority<'lock> = (TagPriority, Option<&'lock BuildTag>);
3122
3123        let mut best: Option<(WheelPriority, usize)> = None;
3124        for (i, wheel) in self.wheels.iter().enumerate() {
3125            let TagCompatibility::Compatible(tag_priority) =
3126                wheel.filename.compatibility(tag_policy.tags())
3127            else {
3128                continue;
3129            };
3130            let build_tag = wheel.filename.build_tag();
3131            let wheel_priority = (tag_priority, build_tag);
3132            match best {
3133                None => {
3134                    best = Some((wheel_priority, i));
3135                }
3136                Some((best_priority, _)) => {
3137                    if wheel_priority > best_priority {
3138                        best = Some((wheel_priority, i));
3139                    }
3140                }
3141            }
3142        }
3143
3144        let best = best.map(|(_, i)| i);
3145        match tag_policy {
3146            TagPolicy::Required(_) => best,
3147            TagPolicy::Preferred(_) => best.or_else(|| self.wheels.first().map(|_| 0)),
3148        }
3149    }
3150
3151    /// Returns the [`PackageName`] of the package.
3152    pub fn name(&self) -> &PackageName {
3153        &self.id.name
3154    }
3155
3156    /// Returns the [`Version`] of the package.
3157    pub fn version(&self) -> Option<&Version> {
3158        self.id.version.as_ref()
3159    }
3160
3161    /// Returns the Git SHA of the package, if it is a Git source.
3162    pub fn git_sha(&self) -> Option<&GitOid> {
3163        match &self.id.source {
3164            Source::Git(_, git) => Some(&git.precise),
3165            _ => None,
3166        }
3167    }
3168
3169    /// Return the fork markers for this package, if any.
3170    pub fn fork_markers(&self) -> &[UniversalMarker] {
3171        self.fork_markers.as_slice()
3172    }
3173
3174    /// Returns the [`IndexUrl`] for the package, if it is a registry source.
3175    pub fn index(&self, root: &Path) -> Result<Option<IndexUrl>, LockError> {
3176        match &self.id.source {
3177            Source::Registry(RegistrySource::Url(url)) => {
3178                let index = IndexUrl::from(VerbatimUrl::from_url(
3179                    url.to_url().map_err(LockErrorKind::InvalidUrl)?,
3180                ));
3181                Ok(Some(index))
3182            }
3183            Source::Registry(RegistrySource::Path(path)) => {
3184                let index = IndexUrl::from(
3185                    VerbatimUrl::from_absolute_path(root.join(path))
3186                        .map_err(LockErrorKind::RegistryVerbatimUrl)?,
3187                );
3188                Ok(Some(index))
3189            }
3190            _ => Ok(None),
3191        }
3192    }
3193
3194    /// Returns all the hashes associated with this [`Package`].
3195    fn hashes(&self) -> HashDigests {
3196        let mut hashes = Vec::with_capacity(
3197            usize::from(self.sdist.as_ref().and_then(|sdist| sdist.hash()).is_some())
3198                + self
3199                    .wheels
3200                    .iter()
3201                    .map(|wheel| usize::from(wheel.hash.is_some()))
3202                    .sum::<usize>(),
3203        );
3204        if let Some(ref sdist) = self.sdist {
3205            if let Some(hash) = sdist.hash() {
3206                hashes.push(hash.0.clone());
3207            }
3208        }
3209        for wheel in &self.wheels {
3210            hashes.extend(wheel.hash.as_ref().map(|h| h.0.clone()));
3211            if let Some(zstd) = wheel.zstd.as_ref() {
3212                hashes.extend(zstd.hash.as_ref().map(|h| h.0.clone()));
3213            }
3214        }
3215        HashDigests::from(hashes)
3216    }
3217
3218    /// Returns the [`ResolvedRepositoryReference`] for the package, if it is a Git source.
3219    pub fn as_git_ref(&self) -> Result<Option<ResolvedRepositoryReference>, LockError> {
3220        match &self.id.source {
3221            Source::Git(url, git) => Ok(Some(ResolvedRepositoryReference {
3222                reference: RepositoryReference {
3223                    url: RepositoryUrl::new(&url.to_url().map_err(LockErrorKind::InvalidUrl)?),
3224                    reference: GitReference::from(git.kind.clone()),
3225                },
3226                sha: git.precise,
3227            })),
3228            _ => Ok(None),
3229        }
3230    }
3231
3232    /// Returns `true` if the package is a dynamic source tree.
3233    fn is_dynamic(&self) -> bool {
3234        self.id.version.is_none()
3235    }
3236
3237    /// Returns the extras the package provides, if any.
3238    pub fn provides_extras(&self) -> &[ExtraName] {
3239        &self.metadata.provides_extra
3240    }
3241
3242    /// Returns the dependency groups the package provides, if any.
3243    pub fn dependency_groups(&self) -> &BTreeMap<GroupName, BTreeSet<Requirement>> {
3244        &self.metadata.dependency_groups
3245    }
3246
3247    /// Returns the dependencies of the package.
3248    pub fn dependencies(&self) -> &[Dependency] {
3249        &self.dependencies
3250    }
3251
3252    /// Returns the optional dependencies of the package.
3253    pub fn optional_dependencies(&self) -> &BTreeMap<ExtraName, Vec<Dependency>> {
3254        &self.optional_dependencies
3255    }
3256
3257    /// Returns the resolved PEP 735 dependency groups of the package.
3258    pub fn resolved_dependency_groups(&self) -> &BTreeMap<GroupName, Vec<Dependency>> {
3259        &self.dependency_groups
3260    }
3261
3262    /// Returns an [`InstallTarget`] view for filtering decisions.
3263    pub fn as_install_target(&self) -> InstallTarget<'_> {
3264        InstallTarget {
3265            name: self.name(),
3266            is_local: self.id.source.is_local(),
3267        }
3268    }
3269}
3270
3271/// Attempts to construct a `VerbatimUrl` from the given normalized `Path`.
3272fn verbatim_url(path: &Path, id: &PackageId) -> Result<VerbatimUrl, LockError> {
3273    let url =
3274        VerbatimUrl::from_normalized_path(path).map_err(|err| LockErrorKind::VerbatimUrl {
3275            id: id.clone(),
3276            err,
3277        })?;
3278    Ok(url)
3279}
3280
3281/// Attempts to construct an absolute path from the given `Path`.
3282fn absolute_path(workspace_root: &Path, path: &Path) -> Result<PathBuf, LockError> {
3283    let path = uv_fs::normalize_absolute_path(&workspace_root.join(path))
3284        .map_err(LockErrorKind::AbsolutePath)?;
3285    Ok(path)
3286}
3287
3288#[derive(Clone, Debug, serde::Deserialize)]
3289#[serde(rename_all = "kebab-case")]
3290struct PackageWire {
3291    #[serde(flatten)]
3292    id: PackageId,
3293    #[serde(default)]
3294    metadata: PackageMetadata,
3295    #[serde(default)]
3296    sdist: Option<SourceDist>,
3297    #[serde(default)]
3298    wheels: Vec<Wheel>,
3299    #[serde(default, rename = "resolution-markers")]
3300    fork_markers: Vec<SimplifiedMarkerTree>,
3301    #[serde(default)]
3302    dependencies: Vec<DependencyWire>,
3303    #[serde(default)]
3304    optional_dependencies: BTreeMap<ExtraName, Vec<DependencyWire>>,
3305    #[serde(default, rename = "dev-dependencies", alias = "dependency-groups")]
3306    dependency_groups: BTreeMap<GroupName, Vec<DependencyWire>>,
3307}
3308
3309#[derive(Clone, Default, Debug, Eq, PartialEq, serde::Deserialize)]
3310#[serde(rename_all = "kebab-case")]
3311struct PackageMetadata {
3312    #[serde(default)]
3313    requires_dist: BTreeSet<Requirement>,
3314    #[serde(default, rename = "provides-extras")]
3315    provides_extra: Box<[ExtraName]>,
3316    #[serde(default, rename = "requires-dev", alias = "dependency-groups")]
3317    dependency_groups: BTreeMap<GroupName, BTreeSet<Requirement>>,
3318}
3319
3320impl PackageWire {
3321    fn unwire(
3322        self,
3323        requires_python: &RequiresPython,
3324        unambiguous_package_ids: &FxHashMap<PackageName, PackageId>,
3325    ) -> Result<Package, LockError> {
3326        // Consistency check
3327        if !uv_flags::contains(uv_flags::EnvironmentFlags::SKIP_WHEEL_FILENAME_CHECK) {
3328            if let Some(version) = &self.id.version {
3329                for wheel in &self.wheels {
3330                    if *version != wheel.filename.version
3331                        && *version != wheel.filename.version.clone().without_local()
3332                    {
3333                        return Err(LockError::from(LockErrorKind::InconsistentVersions {
3334                            name: self.id.name,
3335                            version: version.clone(),
3336                            wheel: wheel.clone(),
3337                        }));
3338                    }
3339                }
3340                // We can't check the source dist version since it does not need to contain the version
3341                // in the filename.
3342            }
3343        }
3344
3345        let unwire_deps = |deps: Vec<DependencyWire>| -> Result<Vec<Dependency>, LockError> {
3346            deps.into_iter()
3347                .map(|dep| dep.unwire(requires_python, unambiguous_package_ids))
3348                .collect()
3349        };
3350
3351        Ok(Package {
3352            id: self.id,
3353            metadata: self.metadata,
3354            sdist: self.sdist,
3355            wheels: self.wheels,
3356            fork_markers: self
3357                .fork_markers
3358                .into_iter()
3359                .map(|simplified_marker| simplified_marker.into_marker(requires_python))
3360                .map(UniversalMarker::from_combined)
3361                .collect(),
3362            dependencies: unwire_deps(self.dependencies)?,
3363            optional_dependencies: self
3364                .optional_dependencies
3365                .into_iter()
3366                .map(|(extra, deps)| Ok((extra, unwire_deps(deps)?)))
3367                .collect::<Result<_, LockError>>()?,
3368            dependency_groups: self
3369                .dependency_groups
3370                .into_iter()
3371                .map(|(group, deps)| Ok((group, unwire_deps(deps)?)))
3372                .collect::<Result<_, LockError>>()?,
3373        })
3374    }
3375}
3376
3377/// Inside the lockfile, we match a dependency entry to a package entry through a key made up
3378/// of the name, the version and the source url.
3379#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, serde::Deserialize)]
3380#[serde(rename_all = "kebab-case")]
3381pub(crate) struct PackageId {
3382    pub(crate) name: PackageName,
3383    pub(crate) version: Option<Version>,
3384    source: Source,
3385}
3386
3387impl PackageId {
3388    fn from_annotated_dist(annotated_dist: &AnnotatedDist, root: &Path) -> Result<Self, LockError> {
3389        // Identify the source of the package.
3390        let source = Source::from_resolved_dist(&annotated_dist.dist, root)?;
3391        // Omit versions for dynamic source trees.
3392        let version = if source.is_source_tree()
3393            && annotated_dist
3394                .metadata
3395                .as_ref()
3396                .is_some_and(|metadata| metadata.dynamic)
3397        {
3398            None
3399        } else {
3400            Some(annotated_dist.version.clone())
3401        };
3402        let name = annotated_dist.name.clone();
3403        Ok(Self {
3404            name,
3405            version,
3406            source,
3407        })
3408    }
3409
3410    /// Writes this package ID inline into the table given.
3411    ///
3412    /// When a map is given, and if the package name in this ID is unambiguous
3413    /// (i.e., it has a count of 1 in the map), then the `version` and `source`
3414    /// fields are omitted. In all other cases, including when a map is not
3415    /// given, the `version` and `source` fields are written.
3416    fn to_toml(&self, dist_count_by_name: Option<&FxHashMap<PackageName, u64>>, table: &mut Table) {
3417        let count = dist_count_by_name.and_then(|map| map.get(&self.name).copied());
3418        table.insert("name", value(self.name.to_string()));
3419        if count.map(|count| count > 1).unwrap_or(true) {
3420            if let Some(version) = &self.version {
3421                table.insert("version", value(version.to_string()));
3422            }
3423            self.source.to_toml(table);
3424        }
3425    }
3426}
3427
3428impl Display for PackageId {
3429    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3430        if let Some(version) = &self.version {
3431            write!(f, "{}=={} @ {}", self.name, version, self.source)
3432        } else {
3433            write!(f, "{} @ {}", self.name, self.source)
3434        }
3435    }
3436}
3437
3438#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, serde::Deserialize)]
3439#[serde(rename_all = "kebab-case")]
3440struct PackageIdForDependency {
3441    name: PackageName,
3442    version: Option<Version>,
3443    source: Option<Source>,
3444}
3445
3446impl PackageIdForDependency {
3447    fn unwire(
3448        self,
3449        unambiguous_package_ids: &FxHashMap<PackageName, PackageId>,
3450    ) -> Result<PackageId, LockError> {
3451        let unambiguous_package_id = unambiguous_package_ids.get(&self.name);
3452        let source = self.source.map(Ok::<_, LockError>).unwrap_or_else(|| {
3453            let Some(package_id) = unambiguous_package_id else {
3454                return Err(LockErrorKind::MissingDependencySource {
3455                    name: self.name.clone(),
3456                }
3457                .into());
3458            };
3459            Ok(package_id.source.clone())
3460        })?;
3461        let version = if let Some(version) = self.version {
3462            Some(version)
3463        } else {
3464            if let Some(package_id) = unambiguous_package_id {
3465                package_id.version.clone()
3466            } else {
3467                // If the package is a source tree, assume that the missing `self.version` field is
3468                // indicative of a dynamic version.
3469                if source.is_source_tree() {
3470                    None
3471                } else {
3472                    return Err(LockErrorKind::MissingDependencyVersion {
3473                        name: self.name.clone(),
3474                    }
3475                    .into());
3476                }
3477            }
3478        };
3479        Ok(PackageId {
3480            name: self.name,
3481            version,
3482            source,
3483        })
3484    }
3485}
3486
3487impl From<PackageId> for PackageIdForDependency {
3488    fn from(id: PackageId) -> Self {
3489        Self {
3490            name: id.name,
3491            version: id.version,
3492            source: Some(id.source),
3493        }
3494    }
3495}
3496
3497/// A unique identifier to differentiate between different sources for the same version of a
3498/// package.
3499///
3500/// NOTE: Care should be taken when adding variants to this enum. Namely, new
3501/// variants should be added without changing the relative ordering of other
3502/// variants. Otherwise, this could cause the lockfile to have a different
3503/// canonical ordering of sources.
3504#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, serde::Deserialize)]
3505#[serde(try_from = "SourceWire")]
3506enum Source {
3507    /// A registry or `--find-links` index.
3508    Registry(RegistrySource),
3509    /// A Git repository.
3510    Git(UrlString, GitSource),
3511    /// A direct HTTP(S) URL.
3512    Direct(UrlString, DirectSource),
3513    /// A path to a local source or built archive.
3514    Path(Box<Path>),
3515    /// A path to a local directory.
3516    Directory(Box<Path>),
3517    /// A path to a local directory that should be installed as editable.
3518    Editable(Box<Path>),
3519    /// A path to a local directory that should not be built or installed.
3520    Virtual(Box<Path>),
3521}
3522
3523impl Source {
3524    fn from_resolved_dist(resolved_dist: &ResolvedDist, root: &Path) -> Result<Self, LockError> {
3525        match *resolved_dist {
3526            // We pass empty installed packages for locking.
3527            ResolvedDist::Installed { .. } => unreachable!(),
3528            ResolvedDist::Installable { ref dist, .. } => Self::from_dist(dist, root),
3529        }
3530    }
3531
3532    fn from_dist(dist: &Dist, root: &Path) -> Result<Self, LockError> {
3533        match *dist {
3534            Dist::Built(ref built_dist) => Self::from_built_dist(built_dist, root),
3535            Dist::Source(ref source_dist) => Self::from_source_dist(source_dist, root),
3536        }
3537    }
3538
3539    fn from_built_dist(built_dist: &BuiltDist, root: &Path) -> Result<Self, LockError> {
3540        match *built_dist {
3541            BuiltDist::Registry(ref reg_dist) => Self::from_registry_built_dist(reg_dist, root),
3542            BuiltDist::DirectUrl(ref direct_dist) => Ok(Self::from_direct_built_dist(direct_dist)),
3543            BuiltDist::Path(ref path_dist) => Self::from_path_built_dist(path_dist, root),
3544        }
3545    }
3546
3547    fn from_source_dist(
3548        source_dist: &uv_distribution_types::SourceDist,
3549        root: &Path,
3550    ) -> Result<Self, LockError> {
3551        match *source_dist {
3552            uv_distribution_types::SourceDist::Registry(ref reg_dist) => {
3553                Self::from_registry_source_dist(reg_dist, root)
3554            }
3555            uv_distribution_types::SourceDist::DirectUrl(ref direct_dist) => {
3556                Ok(Self::from_direct_source_dist(direct_dist))
3557            }
3558            uv_distribution_types::SourceDist::Git(ref git_dist) => {
3559                Ok(Self::from_git_dist(git_dist))
3560            }
3561            uv_distribution_types::SourceDist::Path(ref path_dist) => {
3562                Self::from_path_source_dist(path_dist, root)
3563            }
3564            uv_distribution_types::SourceDist::Directory(ref directory) => {
3565                Self::from_directory_source_dist(directory, root)
3566            }
3567        }
3568    }
3569
3570    fn from_registry_built_dist(
3571        reg_dist: &RegistryBuiltDist,
3572        root: &Path,
3573    ) -> Result<Self, LockError> {
3574        Self::from_index_url(&reg_dist.best_wheel().index, root)
3575    }
3576
3577    fn from_registry_source_dist(
3578        reg_dist: &RegistrySourceDist,
3579        root: &Path,
3580    ) -> Result<Self, LockError> {
3581        Self::from_index_url(&reg_dist.index, root)
3582    }
3583
3584    fn from_direct_built_dist(direct_dist: &DirectUrlBuiltDist) -> Self {
3585        Self::Direct(
3586            normalize_url(direct_dist.url.to_url()),
3587            DirectSource { subdirectory: None },
3588        )
3589    }
3590
3591    fn from_direct_source_dist(direct_dist: &DirectUrlSourceDist) -> Self {
3592        Self::Direct(
3593            normalize_url(direct_dist.url.to_url()),
3594            DirectSource {
3595                subdirectory: direct_dist.subdirectory.clone(),
3596            },
3597        )
3598    }
3599
3600    fn from_path_built_dist(path_dist: &PathBuiltDist, root: &Path) -> Result<Self, LockError> {
3601        let path = relative_to(&path_dist.install_path, root)
3602            .or_else(|_| std::path::absolute(&path_dist.install_path))
3603            .map_err(LockErrorKind::DistributionRelativePath)?;
3604        Ok(Self::Path(path.into_boxed_path()))
3605    }
3606
3607    fn from_path_source_dist(path_dist: &PathSourceDist, root: &Path) -> Result<Self, LockError> {
3608        let path = relative_to(&path_dist.install_path, root)
3609            .or_else(|_| std::path::absolute(&path_dist.install_path))
3610            .map_err(LockErrorKind::DistributionRelativePath)?;
3611        Ok(Self::Path(path.into_boxed_path()))
3612    }
3613
3614    fn from_directory_source_dist(
3615        directory_dist: &DirectorySourceDist,
3616        root: &Path,
3617    ) -> Result<Self, LockError> {
3618        let path = relative_to(&directory_dist.install_path, root)
3619            .or_else(|_| std::path::absolute(&directory_dist.install_path))
3620            .map_err(LockErrorKind::DistributionRelativePath)?;
3621        if directory_dist.editable.unwrap_or(false) {
3622            Ok(Self::Editable(path.into_boxed_path()))
3623        } else if directory_dist.r#virtual.unwrap_or(false) {
3624            Ok(Self::Virtual(path.into_boxed_path()))
3625        } else {
3626            Ok(Self::Directory(path.into_boxed_path()))
3627        }
3628    }
3629
3630    fn from_index_url(index_url: &IndexUrl, root: &Path) -> Result<Self, LockError> {
3631        match index_url {
3632            IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
3633                // Remove any sensitive credentials from the index URL.
3634                let redacted = index_url.without_credentials();
3635                let source = RegistrySource::Url(UrlString::from(redacted.as_ref()));
3636                Ok(Self::Registry(source))
3637            }
3638            IndexUrl::Path(url) => {
3639                let path = url
3640                    .to_file_path()
3641                    .map_err(|()| LockErrorKind::UrlToPath { url: url.to_url() })?;
3642                let path = relative_to(&path, root)
3643                    .or_else(|_| std::path::absolute(&path))
3644                    .map_err(LockErrorKind::IndexRelativePath)?;
3645                let source = RegistrySource::Path(path.into_boxed_path());
3646                Ok(Self::Registry(source))
3647            }
3648        }
3649    }
3650
3651    fn from_git_dist(git_dist: &GitSourceDist) -> Self {
3652        Self::Git(
3653            UrlString::from(locked_git_url(git_dist)),
3654            GitSource {
3655                kind: GitSourceKind::from(git_dist.git.reference().clone()),
3656                precise: git_dist.git.precise().unwrap_or_else(|| {
3657                    panic!("Git distribution is missing a precise hash: {git_dist}")
3658                }),
3659                subdirectory: git_dist.subdirectory.clone(),
3660                lfs: git_dist.git.lfs(),
3661            },
3662        )
3663    }
3664
3665    /// Returns `true` if the source should be considered immutable.
3666    ///
3667    /// We assume that registry sources are immutable. In other words, we expect that once a
3668    /// package-version is published to a registry, its metadata will not change.
3669    ///
3670    /// We also assume that Git sources are immutable, since a Git source encodes a specific commit.
3671    fn is_immutable(&self) -> bool {
3672        matches!(self, Self::Registry(..) | Self::Git(_, _))
3673    }
3674
3675    /// Returns `true` if the source is that of a wheel.
3676    fn is_wheel(&self) -> bool {
3677        match self {
3678            Self::Path(path) => {
3679                matches!(
3680                    DistExtension::from_path(path).ok(),
3681                    Some(DistExtension::Wheel)
3682                )
3683            }
3684            Self::Direct(url, _) => {
3685                matches!(
3686                    DistExtension::from_path(url.as_ref()).ok(),
3687                    Some(DistExtension::Wheel)
3688                )
3689            }
3690            Self::Directory(..) => false,
3691            Self::Editable(..) => false,
3692            Self::Virtual(..) => false,
3693            Self::Git(..) => false,
3694            Self::Registry(..) => false,
3695        }
3696    }
3697
3698    /// Returns `true` if the source is that of a source tree.
3699    fn is_source_tree(&self) -> bool {
3700        match self {
3701            Self::Directory(..) | Self::Editable(..) | Self::Virtual(..) => true,
3702            Self::Path(..) | Self::Git(..) | Self::Registry(..) | Self::Direct(..) => false,
3703        }
3704    }
3705
3706    /// Returns the path to the source tree, if the source is a source tree.
3707    fn as_source_tree(&self) -> Option<&Path> {
3708        match self {
3709            Self::Directory(path) | Self::Editable(path) | Self::Virtual(path) => Some(path),
3710            Self::Path(..) | Self::Git(..) | Self::Registry(..) | Self::Direct(..) => None,
3711        }
3712    }
3713
3714    fn to_toml(&self, table: &mut Table) {
3715        let mut source_table = InlineTable::new();
3716        match self {
3717            Self::Registry(source) => match source {
3718                RegistrySource::Url(url) => {
3719                    source_table.insert("registry", Value::from(url.as_ref()));
3720                }
3721                RegistrySource::Path(path) => {
3722                    source_table.insert(
3723                        "registry",
3724                        Value::from(PortablePath::from(path).to_string()),
3725                    );
3726                }
3727            },
3728            Self::Git(url, _) => {
3729                source_table.insert("git", Value::from(url.as_ref()));
3730            }
3731            Self::Direct(url, DirectSource { subdirectory }) => {
3732                source_table.insert("url", Value::from(url.as_ref()));
3733                if let Some(ref subdirectory) = *subdirectory {
3734                    source_table.insert(
3735                        "subdirectory",
3736                        Value::from(PortablePath::from(subdirectory).to_string()),
3737                    );
3738                }
3739            }
3740            Self::Path(path) => {
3741                source_table.insert("path", Value::from(PortablePath::from(path).to_string()));
3742            }
3743            Self::Directory(path) => {
3744                source_table.insert(
3745                    "directory",
3746                    Value::from(PortablePath::from(path).to_string()),
3747                );
3748            }
3749            Self::Editable(path) => {
3750                source_table.insert(
3751                    "editable",
3752                    Value::from(PortablePath::from(path).to_string()),
3753                );
3754            }
3755            Self::Virtual(path) => {
3756                source_table.insert("virtual", Value::from(PortablePath::from(path).to_string()));
3757            }
3758        }
3759        table.insert("source", value(source_table));
3760    }
3761
3762    /// Check if a package is local by examining its source.
3763    pub(crate) fn is_local(&self) -> bool {
3764        matches!(
3765            self,
3766            Self::Path(_) | Self::Directory(_) | Self::Editable(_) | Self::Virtual(_)
3767        )
3768    }
3769}
3770
3771impl Display for Source {
3772    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3773        match self {
3774            Self::Registry(RegistrySource::Url(url)) | Self::Git(url, _) | Self::Direct(url, _) => {
3775                write!(f, "{}+{}", self.name(), url)
3776            }
3777            Self::Registry(RegistrySource::Path(path))
3778            | Self::Path(path)
3779            | Self::Directory(path)
3780            | Self::Editable(path)
3781            | Self::Virtual(path) => {
3782                write!(f, "{}+{}", self.name(), PortablePath::from(path))
3783            }
3784        }
3785    }
3786}
3787
3788impl Source {
3789    fn name(&self) -> &str {
3790        match self {
3791            Self::Registry(..) => "registry",
3792            Self::Git(..) => "git",
3793            Self::Direct(..) => "direct",
3794            Self::Path(..) => "path",
3795            Self::Directory(..) => "directory",
3796            Self::Editable(..) => "editable",
3797            Self::Virtual(..) => "virtual",
3798        }
3799    }
3800
3801    /// Returns `Some(true)` to indicate that the source kind _must_ include a
3802    /// hash.
3803    ///
3804    /// Returns `Some(false)` to indicate that the source kind _must not_
3805    /// include a hash.
3806    ///
3807    /// Returns `None` to indicate that the source kind _may_ include a hash.
3808    fn requires_hash(&self) -> Option<bool> {
3809        match self {
3810            Self::Registry(..) => None,
3811            Self::Direct(..) | Self::Path(..) => Some(true),
3812            Self::Git(..) | Self::Directory(..) | Self::Editable(..) | Self::Virtual(..) => {
3813                Some(false)
3814            }
3815        }
3816    }
3817}
3818
3819#[derive(Clone, Debug, serde::Deserialize)]
3820#[serde(untagged, rename_all = "kebab-case")]
3821enum SourceWire {
3822    Registry {
3823        registry: RegistrySourceWire,
3824    },
3825    Git {
3826        git: String,
3827    },
3828    Direct {
3829        url: UrlString,
3830        subdirectory: Option<PortablePathBuf>,
3831    },
3832    Path {
3833        path: PortablePathBuf,
3834    },
3835    Directory {
3836        directory: PortablePathBuf,
3837    },
3838    Editable {
3839        editable: PortablePathBuf,
3840    },
3841    Virtual {
3842        r#virtual: PortablePathBuf,
3843    },
3844}
3845
3846impl TryFrom<SourceWire> for Source {
3847    type Error = LockError;
3848
3849    fn try_from(wire: SourceWire) -> Result<Self, LockError> {
3850        #[allow(clippy::enum_glob_use)]
3851        use self::SourceWire::*;
3852
3853        match wire {
3854            Registry { registry } => Ok(Self::Registry(registry.into())),
3855            Git { git } => {
3856                let url = DisplaySafeUrl::parse(&git)
3857                    .map_err(|err| SourceParseError::InvalidUrl {
3858                        given: git.clone(),
3859                        err,
3860                    })
3861                    .map_err(LockErrorKind::InvalidGitSourceUrl)?;
3862
3863                let git_source = GitSource::from_url(&url)
3864                    .map_err(|err| match err {
3865                        GitSourceError::InvalidSha => SourceParseError::InvalidSha { given: git },
3866                        GitSourceError::MissingSha => SourceParseError::MissingSha { given: git },
3867                    })
3868                    .map_err(LockErrorKind::InvalidGitSourceUrl)?;
3869
3870                Ok(Self::Git(UrlString::from(url), git_source))
3871            }
3872            Direct { url, subdirectory } => Ok(Self::Direct(
3873                url,
3874                DirectSource {
3875                    subdirectory: subdirectory.map(Box::<std::path::Path>::from),
3876                },
3877            )),
3878            Path { path } => Ok(Self::Path(path.into())),
3879            Directory { directory } => Ok(Self::Directory(directory.into())),
3880            Editable { editable } => Ok(Self::Editable(editable.into())),
3881            Virtual { r#virtual } => Ok(Self::Virtual(r#virtual.into())),
3882        }
3883    }
3884}
3885
3886/// The source for a registry, which could be a URL or a relative path.
3887#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
3888enum RegistrySource {
3889    /// Ex) `https://pypi.org/simple`
3890    Url(UrlString),
3891    /// Ex) `../path/to/local/index`
3892    Path(Box<Path>),
3893}
3894
3895impl Display for RegistrySource {
3896    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3897        match self {
3898            Self::Url(url) => write!(f, "{url}"),
3899            Self::Path(path) => write!(f, "{}", path.display()),
3900        }
3901    }
3902}
3903
3904#[derive(Clone, Debug)]
3905enum RegistrySourceWire {
3906    /// Ex) `https://pypi.org/simple`
3907    Url(UrlString),
3908    /// Ex) `../path/to/local/index`
3909    Path(PortablePathBuf),
3910}
3911
3912impl<'de> serde::de::Deserialize<'de> for RegistrySourceWire {
3913    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
3914    where
3915        D: serde::de::Deserializer<'de>,
3916    {
3917        struct Visitor;
3918
3919        impl serde::de::Visitor<'_> for Visitor {
3920            type Value = RegistrySourceWire;
3921
3922            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
3923                formatter.write_str("a valid URL or a file path")
3924            }
3925
3926            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
3927            where
3928                E: serde::de::Error,
3929            {
3930                if split_scheme(value).is_some() {
3931                    Ok(
3932                        serde::Deserialize::deserialize(serde::de::value::StrDeserializer::new(
3933                            value,
3934                        ))
3935                        .map(RegistrySourceWire::Url)?,
3936                    )
3937                } else {
3938                    Ok(
3939                        serde::Deserialize::deserialize(serde::de::value::StrDeserializer::new(
3940                            value,
3941                        ))
3942                        .map(RegistrySourceWire::Path)?,
3943                    )
3944                }
3945            }
3946        }
3947
3948        deserializer.deserialize_str(Visitor)
3949    }
3950}
3951
3952impl From<RegistrySourceWire> for RegistrySource {
3953    fn from(wire: RegistrySourceWire) -> Self {
3954        match wire {
3955            RegistrySourceWire::Url(url) => Self::Url(url),
3956            RegistrySourceWire::Path(path) => Self::Path(path.into()),
3957        }
3958    }
3959}
3960
3961#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, serde::Deserialize)]
3962#[serde(rename_all = "kebab-case")]
3963struct DirectSource {
3964    subdirectory: Option<Box<Path>>,
3965}
3966
3967/// NOTE: Care should be taken when adding variants to this enum. Namely, new
3968/// variants should be added without changing the relative ordering of other
3969/// variants. Otherwise, this could cause the lockfile to have a different
3970/// canonical ordering of package entries.
3971#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
3972struct GitSource {
3973    precise: GitOid,
3974    subdirectory: Option<Box<Path>>,
3975    kind: GitSourceKind,
3976    lfs: GitLfs,
3977}
3978
3979/// An error that occurs when a source string could not be parsed.
3980#[derive(Clone, Debug, Eq, PartialEq)]
3981enum GitSourceError {
3982    InvalidSha,
3983    MissingSha,
3984}
3985
3986impl GitSource {
3987    /// Extracts a Git source reference from the query pairs and the hash
3988    /// fragment in the given URL.
3989    fn from_url(url: &Url) -> Result<Self, GitSourceError> {
3990        let mut kind = GitSourceKind::DefaultBranch;
3991        let mut subdirectory = None;
3992        let mut lfs = GitLfs::Disabled;
3993        for (key, val) in url.query_pairs() {
3994            match &*key {
3995                "tag" => kind = GitSourceKind::Tag(val.into_owned()),
3996                "branch" => kind = GitSourceKind::Branch(val.into_owned()),
3997                "rev" => kind = GitSourceKind::Rev(val.into_owned()),
3998                "subdirectory" => subdirectory = Some(PortablePathBuf::from(val.as_ref()).into()),
3999                "lfs" => lfs = GitLfs::from(val.eq_ignore_ascii_case("true")),
4000                _ => {}
4001            }
4002        }
4003
4004        let precise = GitOid::from_str(url.fragment().ok_or(GitSourceError::MissingSha)?)
4005            .map_err(|_| GitSourceError::InvalidSha)?;
4006
4007        Ok(Self {
4008            precise,
4009            subdirectory,
4010            kind,
4011            lfs,
4012        })
4013    }
4014}
4015
4016#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, serde::Deserialize)]
4017#[serde(rename_all = "kebab-case")]
4018enum GitSourceKind {
4019    Tag(String),
4020    Branch(String),
4021    Rev(String),
4022    DefaultBranch,
4023}
4024
4025/// Inspired by: <https://discuss.python.org/t/lock-files-again-but-this-time-w-sdists/46593>
4026#[derive(Clone, Debug, serde::Deserialize, PartialEq, Eq)]
4027#[serde(rename_all = "kebab-case")]
4028struct SourceDistMetadata {
4029    /// A hash of the source distribution.
4030    hash: Option<Hash>,
4031    /// The size of the source distribution in bytes.
4032    ///
4033    /// This is only present for source distributions that come from registries.
4034    size: Option<u64>,
4035    /// The upload time of the source distribution.
4036    #[serde(alias = "upload_time")]
4037    upload_time: Option<Timestamp>,
4038}
4039
4040/// A URL or file path where the source dist that was
4041/// locked against was found. The location does not need to exist in the
4042/// future, so this should be treated as only a hint to where to look
4043/// and/or recording where the source dist file originally came from.
4044#[derive(Clone, Debug, serde::Deserialize, PartialEq, Eq)]
4045#[serde(from = "SourceDistWire")]
4046enum SourceDist {
4047    Url {
4048        url: UrlString,
4049        #[serde(flatten)]
4050        metadata: SourceDistMetadata,
4051    },
4052    Path {
4053        path: Box<Path>,
4054        #[serde(flatten)]
4055        metadata: SourceDistMetadata,
4056    },
4057    Metadata {
4058        #[serde(flatten)]
4059        metadata: SourceDistMetadata,
4060    },
4061}
4062
4063impl SourceDist {
4064    fn filename(&self) -> Option<Cow<'_, str>> {
4065        match self {
4066            Self::Metadata { .. } => None,
4067            Self::Url { url, .. } => url.filename().ok(),
4068            Self::Path { path, .. } => path.file_name().map(|filename| filename.to_string_lossy()),
4069        }
4070    }
4071
4072    fn url(&self) -> Option<&UrlString> {
4073        match self {
4074            Self::Metadata { .. } => None,
4075            Self::Url { url, .. } => Some(url),
4076            Self::Path { .. } => None,
4077        }
4078    }
4079
4080    pub(crate) fn hash(&self) -> Option<&Hash> {
4081        match self {
4082            Self::Metadata { metadata } => metadata.hash.as_ref(),
4083            Self::Url { metadata, .. } => metadata.hash.as_ref(),
4084            Self::Path { metadata, .. } => metadata.hash.as_ref(),
4085        }
4086    }
4087
4088    pub(crate) fn size(&self) -> Option<u64> {
4089        match self {
4090            Self::Metadata { metadata } => metadata.size,
4091            Self::Url { metadata, .. } => metadata.size,
4092            Self::Path { metadata, .. } => metadata.size,
4093        }
4094    }
4095
4096    pub(crate) fn upload_time(&self) -> Option<Timestamp> {
4097        match self {
4098            Self::Metadata { metadata } => metadata.upload_time,
4099            Self::Url { metadata, .. } => metadata.upload_time,
4100            Self::Path { metadata, .. } => metadata.upload_time,
4101        }
4102    }
4103}
4104
4105impl SourceDist {
4106    fn from_annotated_dist(
4107        id: &PackageId,
4108        annotated_dist: &AnnotatedDist,
4109    ) -> Result<Option<Self>, LockError> {
4110        match annotated_dist.dist {
4111            // We pass empty installed packages for locking.
4112            ResolvedDist::Installed { .. } => unreachable!(),
4113            ResolvedDist::Installable { ref dist, .. } => Self::from_dist(
4114                id,
4115                dist,
4116                annotated_dist.hashes.as_slice(),
4117                annotated_dist.index(),
4118            ),
4119        }
4120    }
4121
4122    fn from_dist(
4123        id: &PackageId,
4124        dist: &Dist,
4125        hashes: &[HashDigest],
4126        index: Option<&IndexUrl>,
4127    ) -> Result<Option<Self>, LockError> {
4128        match *dist {
4129            Dist::Built(BuiltDist::Registry(ref built_dist)) => {
4130                let Some(sdist) = built_dist.sdist.as_ref() else {
4131                    return Ok(None);
4132                };
4133                Self::from_registry_dist(sdist, index)
4134            }
4135            Dist::Built(_) => Ok(None),
4136            Dist::Source(ref source_dist) => Self::from_source_dist(id, source_dist, hashes, index),
4137        }
4138    }
4139
4140    fn from_source_dist(
4141        id: &PackageId,
4142        source_dist: &uv_distribution_types::SourceDist,
4143        hashes: &[HashDigest],
4144        index: Option<&IndexUrl>,
4145    ) -> Result<Option<Self>, LockError> {
4146        match *source_dist {
4147            uv_distribution_types::SourceDist::Registry(ref reg_dist) => {
4148                Self::from_registry_dist(reg_dist, index)
4149            }
4150            uv_distribution_types::SourceDist::DirectUrl(_) => {
4151                Self::from_direct_dist(id, hashes).map(Some)
4152            }
4153            uv_distribution_types::SourceDist::Path(_) => {
4154                Self::from_path_dist(id, hashes).map(Some)
4155            }
4156            // An actual sdist entry in the lockfile is only required when
4157            // it's from a registry or a direct URL. Otherwise, it's strictly
4158            // redundant with the information in all other kinds of `source`.
4159            uv_distribution_types::SourceDist::Git(_)
4160            | uv_distribution_types::SourceDist::Directory(_) => Ok(None),
4161        }
4162    }
4163
4164    fn from_registry_dist(
4165        reg_dist: &RegistrySourceDist,
4166        index: Option<&IndexUrl>,
4167    ) -> Result<Option<Self>, LockError> {
4168        // Reject distributions from registries that don't match the index URL, as can occur with
4169        // `--find-links`.
4170        if index.is_none_or(|index| *index != reg_dist.index) {
4171            return Ok(None);
4172        }
4173
4174        match &reg_dist.index {
4175            IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
4176                let url = normalize_file_location(&reg_dist.file.url)
4177                    .map_err(LockErrorKind::InvalidUrl)
4178                    .map_err(LockError::from)?;
4179                let hash = reg_dist.file.hashes.iter().max().cloned().map(Hash::from);
4180                let size = reg_dist.file.size;
4181                let upload_time = reg_dist
4182                    .file
4183                    .upload_time_utc_ms
4184                    .map(Timestamp::from_millisecond)
4185                    .transpose()
4186                    .map_err(LockErrorKind::InvalidTimestamp)?;
4187                Ok(Some(Self::Url {
4188                    url,
4189                    metadata: SourceDistMetadata {
4190                        hash,
4191                        size,
4192                        upload_time,
4193                    },
4194                }))
4195            }
4196            IndexUrl::Path(path) => {
4197                let index_path = path
4198                    .to_file_path()
4199                    .map_err(|()| LockErrorKind::UrlToPath { url: path.to_url() })?;
4200                let url = reg_dist
4201                    .file
4202                    .url
4203                    .to_url()
4204                    .map_err(LockErrorKind::InvalidUrl)?;
4205
4206                if url.scheme() == "file" {
4207                    let reg_dist_path = url
4208                        .to_file_path()
4209                        .map_err(|()| LockErrorKind::UrlToPath { url })?;
4210                    let path = relative_to(&reg_dist_path, index_path)
4211                        .or_else(|_| std::path::absolute(&reg_dist_path))
4212                        .map_err(LockErrorKind::DistributionRelativePath)?
4213                        .into_boxed_path();
4214                    let hash = reg_dist.file.hashes.iter().max().cloned().map(Hash::from);
4215                    let size = reg_dist.file.size;
4216                    let upload_time = reg_dist
4217                        .file
4218                        .upload_time_utc_ms
4219                        .map(Timestamp::from_millisecond)
4220                        .transpose()
4221                        .map_err(LockErrorKind::InvalidTimestamp)?;
4222                    Ok(Some(Self::Path {
4223                        path,
4224                        metadata: SourceDistMetadata {
4225                            hash,
4226                            size,
4227                            upload_time,
4228                        },
4229                    }))
4230                } else {
4231                    let url = normalize_file_location(&reg_dist.file.url)
4232                        .map_err(LockErrorKind::InvalidUrl)
4233                        .map_err(LockError::from)?;
4234                    let hash = reg_dist.file.hashes.iter().max().cloned().map(Hash::from);
4235                    let size = reg_dist.file.size;
4236                    let upload_time = reg_dist
4237                        .file
4238                        .upload_time_utc_ms
4239                        .map(Timestamp::from_millisecond)
4240                        .transpose()
4241                        .map_err(LockErrorKind::InvalidTimestamp)?;
4242                    Ok(Some(Self::Url {
4243                        url,
4244                        metadata: SourceDistMetadata {
4245                            hash,
4246                            size,
4247                            upload_time,
4248                        },
4249                    }))
4250                }
4251            }
4252        }
4253    }
4254
4255    fn from_direct_dist(id: &PackageId, hashes: &[HashDigest]) -> Result<Self, LockError> {
4256        let Some(hash) = hashes.iter().max().cloned().map(Hash::from) else {
4257            let kind = LockErrorKind::Hash {
4258                id: id.clone(),
4259                artifact_type: "direct URL source distribution",
4260                expected: true,
4261            };
4262            return Err(kind.into());
4263        };
4264        Ok(Self::Metadata {
4265            metadata: SourceDistMetadata {
4266                hash: Some(hash),
4267                size: None,
4268                upload_time: None,
4269            },
4270        })
4271    }
4272
4273    fn from_path_dist(id: &PackageId, hashes: &[HashDigest]) -> Result<Self, LockError> {
4274        let Some(hash) = hashes.iter().max().cloned().map(Hash::from) else {
4275            let kind = LockErrorKind::Hash {
4276                id: id.clone(),
4277                artifact_type: "path source distribution",
4278                expected: true,
4279            };
4280            return Err(kind.into());
4281        };
4282        Ok(Self::Metadata {
4283            metadata: SourceDistMetadata {
4284                hash: Some(hash),
4285                size: None,
4286                upload_time: None,
4287            },
4288        })
4289    }
4290}
4291
4292#[derive(Clone, Debug, serde::Deserialize)]
4293#[serde(untagged, rename_all = "kebab-case")]
4294enum SourceDistWire {
4295    Url {
4296        url: UrlString,
4297        #[serde(flatten)]
4298        metadata: SourceDistMetadata,
4299    },
4300    Path {
4301        path: PortablePathBuf,
4302        #[serde(flatten)]
4303        metadata: SourceDistMetadata,
4304    },
4305    Metadata {
4306        #[serde(flatten)]
4307        metadata: SourceDistMetadata,
4308    },
4309}
4310
4311impl SourceDist {
4312    /// Returns the TOML representation of this source distribution.
4313    fn to_toml(&self) -> Result<InlineTable, toml_edit::ser::Error> {
4314        let mut table = InlineTable::new();
4315        match self {
4316            Self::Metadata { .. } => {}
4317            Self::Url { url, .. } => {
4318                table.insert("url", Value::from(url.as_ref()));
4319            }
4320            Self::Path { path, .. } => {
4321                table.insert("path", Value::from(PortablePath::from(path).to_string()));
4322            }
4323        }
4324        if let Some(hash) = self.hash() {
4325            table.insert("hash", Value::from(hash.to_string()));
4326        }
4327        if let Some(size) = self.size() {
4328            table.insert(
4329                "size",
4330                toml_edit::ser::ValueSerializer::new().serialize_u64(size)?,
4331            );
4332        }
4333        if let Some(upload_time) = self.upload_time() {
4334            table.insert("upload-time", Value::from(upload_time.to_string()));
4335        }
4336        Ok(table)
4337    }
4338}
4339
4340impl From<SourceDistWire> for SourceDist {
4341    fn from(wire: SourceDistWire) -> Self {
4342        match wire {
4343            SourceDistWire::Url { url, metadata } => Self::Url { url, metadata },
4344            SourceDistWire::Path { path, metadata } => Self::Path {
4345                path: path.into(),
4346                metadata,
4347            },
4348            SourceDistWire::Metadata { metadata } => Self::Metadata { metadata },
4349        }
4350    }
4351}
4352
4353impl From<GitReference> for GitSourceKind {
4354    fn from(value: GitReference) -> Self {
4355        match value {
4356            GitReference::Branch(branch) => Self::Branch(branch),
4357            GitReference::Tag(tag) => Self::Tag(tag),
4358            GitReference::BranchOrTag(rev) => Self::Rev(rev),
4359            GitReference::BranchOrTagOrCommit(rev) => Self::Rev(rev),
4360            GitReference::NamedRef(rev) => Self::Rev(rev),
4361            GitReference::DefaultBranch => Self::DefaultBranch,
4362        }
4363    }
4364}
4365
4366impl From<GitSourceKind> for GitReference {
4367    fn from(value: GitSourceKind) -> Self {
4368        match value {
4369            GitSourceKind::Branch(branch) => Self::Branch(branch),
4370            GitSourceKind::Tag(tag) => Self::Tag(tag),
4371            GitSourceKind::Rev(rev) => Self::from_rev(rev),
4372            GitSourceKind::DefaultBranch => Self::DefaultBranch,
4373        }
4374    }
4375}
4376
4377/// Construct the lockfile-compatible [`DisplaySafeUrl`] for a [`GitSourceDist`].
4378fn locked_git_url(git_dist: &GitSourceDist) -> DisplaySafeUrl {
4379    let mut url = git_dist.git.repository().clone();
4380
4381    // Remove the credentials.
4382    url.remove_credentials();
4383
4384    // Clear out any existing state.
4385    url.set_fragment(None);
4386    url.set_query(None);
4387
4388    // Put the subdirectory in the query.
4389    if let Some(subdirectory) = git_dist
4390        .subdirectory
4391        .as_deref()
4392        .map(PortablePath::from)
4393        .as_ref()
4394        .map(PortablePath::to_string)
4395    {
4396        url.query_pairs_mut()
4397            .append_pair("subdirectory", &subdirectory);
4398    }
4399
4400    // Put lfs=true in the package source git url only when explicitly enabled.
4401    if git_dist.git.lfs().enabled() {
4402        url.query_pairs_mut().append_pair("lfs", "true");
4403    }
4404
4405    // Put the requested reference in the query.
4406    match git_dist.git.reference() {
4407        GitReference::Branch(branch) => {
4408            url.query_pairs_mut().append_pair("branch", branch.as_str());
4409        }
4410        GitReference::Tag(tag) => {
4411            url.query_pairs_mut().append_pair("tag", tag.as_str());
4412        }
4413        GitReference::BranchOrTag(rev)
4414        | GitReference::BranchOrTagOrCommit(rev)
4415        | GitReference::NamedRef(rev) => {
4416            url.query_pairs_mut().append_pair("rev", rev.as_str());
4417        }
4418        GitReference::DefaultBranch => {}
4419    }
4420
4421    // Put the precise commit in the fragment.
4422    url.set_fragment(
4423        git_dist
4424            .git
4425            .precise()
4426            .as_ref()
4427            .map(GitOid::to_string)
4428            .as_deref(),
4429    );
4430
4431    url
4432}
4433
4434#[derive(Clone, Debug, serde::Deserialize, PartialEq, Eq)]
4435struct ZstdWheel {
4436    hash: Option<Hash>,
4437    size: Option<u64>,
4438}
4439
4440/// Inspired by: <https://discuss.python.org/t/lock-files-again-but-this-time-w-sdists/46593>
4441#[derive(Clone, Debug, serde::Deserialize, PartialEq, Eq)]
4442#[serde(try_from = "WheelWire")]
4443struct Wheel {
4444    /// A URL or file path (via `file://`) where the wheel that was locked
4445    /// against was found. The location does not need to exist in the future,
4446    /// so this should be treated as only a hint to where to look and/or
4447    /// recording where the wheel file originally came from.
4448    url: WheelWireSource,
4449    /// A hash of the built distribution.
4450    ///
4451    /// This is only present for wheels that come from registries and direct
4452    /// URLs. Wheels from git or path dependencies do not have hashes
4453    /// associated with them.
4454    hash: Option<Hash>,
4455    /// The size of the built distribution in bytes.
4456    ///
4457    /// This is only present for wheels that come from registries.
4458    size: Option<u64>,
4459    /// The upload time of the built distribution.
4460    ///
4461    /// This is only present for wheels that come from registries.
4462    upload_time: Option<Timestamp>,
4463    /// The filename of the wheel.
4464    ///
4465    /// This isn't part of the wire format since it's redundant with the
4466    /// URL. But we do use it for various things, and thus compute it at
4467    /// deserialization time. Not being able to extract a wheel filename from a
4468    /// wheel URL is thus a deserialization error.
4469    filename: WheelFilename,
4470    /// The zstandard-compressed wheel metadata, if any.
4471    zstd: Option<ZstdWheel>,
4472}
4473
4474impl Wheel {
4475    fn from_annotated_dist(annotated_dist: &AnnotatedDist) -> Result<Vec<Self>, LockError> {
4476        match annotated_dist.dist {
4477            // We pass empty installed packages for locking.
4478            ResolvedDist::Installed { .. } => unreachable!(),
4479            ResolvedDist::Installable { ref dist, .. } => Self::from_dist(
4480                dist,
4481                annotated_dist.hashes.as_slice(),
4482                annotated_dist.index(),
4483            ),
4484        }
4485    }
4486
4487    fn from_dist(
4488        dist: &Dist,
4489        hashes: &[HashDigest],
4490        index: Option<&IndexUrl>,
4491    ) -> Result<Vec<Self>, LockError> {
4492        match *dist {
4493            Dist::Built(ref built_dist) => Self::from_built_dist(built_dist, hashes, index),
4494            Dist::Source(uv_distribution_types::SourceDist::Registry(ref source_dist)) => {
4495                source_dist
4496                    .wheels
4497                    .iter()
4498                    .filter(|wheel| {
4499                        // Reject distributions from registries that don't match the index URL, as can occur with
4500                        // `--find-links`.
4501                        index.is_some_and(|index| *index == wheel.index)
4502                    })
4503                    .map(Self::from_registry_wheel)
4504                    .collect()
4505            }
4506            Dist::Source(_) => Ok(vec![]),
4507        }
4508    }
4509
4510    fn from_built_dist(
4511        built_dist: &BuiltDist,
4512        hashes: &[HashDigest],
4513        index: Option<&IndexUrl>,
4514    ) -> Result<Vec<Self>, LockError> {
4515        match *built_dist {
4516            BuiltDist::Registry(ref reg_dist) => Self::from_registry_dist(reg_dist, index),
4517            BuiltDist::DirectUrl(ref direct_dist) => {
4518                Ok(vec![Self::from_direct_dist(direct_dist, hashes)])
4519            }
4520            BuiltDist::Path(ref path_dist) => Ok(vec![Self::from_path_dist(path_dist, hashes)]),
4521        }
4522    }
4523
4524    fn from_registry_dist(
4525        reg_dist: &RegistryBuiltDist,
4526        index: Option<&IndexUrl>,
4527    ) -> Result<Vec<Self>, LockError> {
4528        reg_dist
4529            .wheels
4530            .iter()
4531            .filter(|wheel| {
4532                // Reject distributions from registries that don't match the index URL, as can occur with
4533                // `--find-links`.
4534                index.is_some_and(|index| *index == wheel.index)
4535            })
4536            .map(Self::from_registry_wheel)
4537            .collect()
4538    }
4539
4540    fn from_registry_wheel(wheel: &RegistryBuiltWheel) -> Result<Self, LockError> {
4541        let url = match &wheel.index {
4542            IndexUrl::Pypi(_) | IndexUrl::Url(_) => {
4543                let url = normalize_file_location(&wheel.file.url)
4544                    .map_err(LockErrorKind::InvalidUrl)
4545                    .map_err(LockError::from)?;
4546                WheelWireSource::Url { url }
4547            }
4548            IndexUrl::Path(path) => {
4549                let index_path = path
4550                    .to_file_path()
4551                    .map_err(|()| LockErrorKind::UrlToPath { url: path.to_url() })?;
4552                let wheel_url = wheel.file.url.to_url().map_err(LockErrorKind::InvalidUrl)?;
4553
4554                if wheel_url.scheme() == "file" {
4555                    let wheel_path = wheel_url
4556                        .to_file_path()
4557                        .map_err(|()| LockErrorKind::UrlToPath { url: wheel_url })?;
4558                    let path = relative_to(&wheel_path, index_path)
4559                        .or_else(|_| std::path::absolute(&wheel_path))
4560                        .map_err(LockErrorKind::DistributionRelativePath)?
4561                        .into_boxed_path();
4562                    WheelWireSource::Path { path }
4563                } else {
4564                    let url = normalize_file_location(&wheel.file.url)
4565                        .map_err(LockErrorKind::InvalidUrl)
4566                        .map_err(LockError::from)?;
4567                    WheelWireSource::Url { url }
4568                }
4569            }
4570        };
4571        let filename = wheel.filename.clone();
4572        let hash = wheel.file.hashes.iter().max().cloned().map(Hash::from);
4573        let size = wheel.file.size;
4574        let upload_time = wheel
4575            .file
4576            .upload_time_utc_ms
4577            .map(Timestamp::from_millisecond)
4578            .transpose()
4579            .map_err(LockErrorKind::InvalidTimestamp)?;
4580        let zstd = wheel.file.zstd.as_ref().map(|zstd| ZstdWheel {
4581            hash: zstd.hashes.iter().max().cloned().map(Hash::from),
4582            size: zstd.size,
4583        });
4584        Ok(Self {
4585            url,
4586            hash,
4587            size,
4588            upload_time,
4589            filename,
4590            zstd,
4591        })
4592    }
4593
4594    fn from_direct_dist(direct_dist: &DirectUrlBuiltDist, hashes: &[HashDigest]) -> Self {
4595        Self {
4596            url: WheelWireSource::Url {
4597                url: normalize_url(direct_dist.url.to_url()),
4598            },
4599            hash: hashes.iter().max().cloned().map(Hash::from),
4600            size: None,
4601            upload_time: None,
4602            filename: direct_dist.filename.clone(),
4603            zstd: None,
4604        }
4605    }
4606
4607    fn from_path_dist(path_dist: &PathBuiltDist, hashes: &[HashDigest]) -> Self {
4608        Self {
4609            url: WheelWireSource::Filename {
4610                filename: path_dist.filename.clone(),
4611            },
4612            hash: hashes.iter().max().cloned().map(Hash::from),
4613            size: None,
4614            upload_time: None,
4615            filename: path_dist.filename.clone(),
4616            zstd: None,
4617        }
4618    }
4619
4620    pub(crate) fn to_registry_wheel(
4621        &self,
4622        source: &RegistrySource,
4623        root: &Path,
4624    ) -> Result<RegistryBuiltWheel, LockError> {
4625        let filename: WheelFilename = self.filename.clone();
4626
4627        match source {
4628            RegistrySource::Url(url) => {
4629                let file_location = match &self.url {
4630                    WheelWireSource::Url { url: file_url } => {
4631                        FileLocation::AbsoluteUrl(file_url.clone())
4632                    }
4633                    WheelWireSource::Path { .. } | WheelWireSource::Filename { .. } => {
4634                        return Err(LockErrorKind::MissingUrl {
4635                            name: filename.name,
4636                            version: filename.version,
4637                        }
4638                        .into());
4639                    }
4640                };
4641                let file = Box::new(uv_distribution_types::File {
4642                    dist_info_metadata: false,
4643                    filename: SmallString::from(filename.to_string()),
4644                    hashes: self.hash.iter().map(|h| h.0.clone()).collect(),
4645                    requires_python: None,
4646                    size: self.size,
4647                    upload_time_utc_ms: self.upload_time.map(Timestamp::as_millisecond),
4648                    url: file_location,
4649                    yanked: None,
4650                    zstd: self
4651                        .zstd
4652                        .as_ref()
4653                        .map(|zstd| uv_distribution_types::Zstd {
4654                            hashes: zstd.hash.iter().map(|h| h.0.clone()).collect(),
4655                            size: zstd.size,
4656                        })
4657                        .map(Box::new),
4658                });
4659                let index = IndexUrl::from(VerbatimUrl::from_url(
4660                    url.to_url().map_err(LockErrorKind::InvalidUrl)?,
4661                ));
4662                Ok(RegistryBuiltWheel {
4663                    filename,
4664                    file,
4665                    index,
4666                })
4667            }
4668            RegistrySource::Path(index_path) => {
4669                let file_location = match &self.url {
4670                    WheelWireSource::Url { url: file_url } => {
4671                        FileLocation::AbsoluteUrl(file_url.clone())
4672                    }
4673                    WheelWireSource::Path { path: file_path } => {
4674                        let file_path = root.join(index_path).join(file_path);
4675                        let file_url =
4676                            DisplaySafeUrl::from_file_path(&file_path).map_err(|()| {
4677                                LockErrorKind::PathToUrl {
4678                                    path: file_path.into_boxed_path(),
4679                                }
4680                            })?;
4681                        FileLocation::AbsoluteUrl(UrlString::from(file_url))
4682                    }
4683                    WheelWireSource::Filename { .. } => {
4684                        return Err(LockErrorKind::MissingPath {
4685                            name: filename.name,
4686                            version: filename.version,
4687                        }
4688                        .into());
4689                    }
4690                };
4691                let file = Box::new(uv_distribution_types::File {
4692                    dist_info_metadata: false,
4693                    filename: SmallString::from(filename.to_string()),
4694                    hashes: self.hash.iter().map(|h| h.0.clone()).collect(),
4695                    requires_python: None,
4696                    size: self.size,
4697                    upload_time_utc_ms: self.upload_time.map(Timestamp::as_millisecond),
4698                    url: file_location,
4699                    yanked: None,
4700                    zstd: self
4701                        .zstd
4702                        .as_ref()
4703                        .map(|zstd| uv_distribution_types::Zstd {
4704                            hashes: zstd.hash.iter().map(|h| h.0.clone()).collect(),
4705                            size: zstd.size,
4706                        })
4707                        .map(Box::new),
4708                });
4709                let index = IndexUrl::from(
4710                    VerbatimUrl::from_absolute_path(root.join(index_path))
4711                        .map_err(LockErrorKind::RegistryVerbatimUrl)?,
4712                );
4713                Ok(RegistryBuiltWheel {
4714                    filename,
4715                    file,
4716                    index,
4717                })
4718            }
4719        }
4720    }
4721}
4722
4723#[derive(Clone, Debug, serde::Deserialize)]
4724#[serde(rename_all = "kebab-case")]
4725struct WheelWire {
4726    #[serde(flatten)]
4727    url: WheelWireSource,
4728    /// A hash of the built distribution.
4729    ///
4730    /// This is only present for wheels that come from registries and direct
4731    /// URLs. Wheels from git or path dependencies do not have hashes
4732    /// associated with them.
4733    hash: Option<Hash>,
4734    /// The size of the built distribution in bytes.
4735    ///
4736    /// This is only present for wheels that come from registries.
4737    size: Option<u64>,
4738    /// The upload time of the built distribution.
4739    ///
4740    /// This is only present for wheels that come from registries.
4741    #[serde(alias = "upload_time")]
4742    upload_time: Option<Timestamp>,
4743    /// The zstandard-compressed wheel metadata, if any.
4744    #[serde(alias = "zstd")]
4745    zstd: Option<ZstdWheel>,
4746}
4747
4748#[derive(Clone, Debug, serde::Deserialize, PartialEq, Eq)]
4749#[serde(untagged, rename_all = "kebab-case")]
4750enum WheelWireSource {
4751    /// Used for all wheels that come from remote sources.
4752    Url {
4753        /// A URL where the wheel that was locked against was found. The location
4754        /// does not need to exist in the future, so this should be treated as
4755        /// only a hint to where to look and/or recording where the wheel file
4756        /// originally came from.
4757        url: UrlString,
4758    },
4759    /// Used for wheels that come from local registries (like `--find-links`).
4760    Path {
4761        /// The path to the wheel, relative to the index.
4762        path: Box<Path>,
4763    },
4764    /// Used for path wheels.
4765    ///
4766    /// We only store the filename for path wheel, since we can't store a relative path in the url
4767    Filename {
4768        /// We duplicate the filename since a lot of code relies on having the filename on the
4769        /// wheel entry.
4770        filename: WheelFilename,
4771    },
4772}
4773
4774impl Wheel {
4775    /// Returns the TOML representation of this wheel.
4776    fn to_toml(&self) -> Result<InlineTable, toml_edit::ser::Error> {
4777        let mut table = InlineTable::new();
4778        match &self.url {
4779            WheelWireSource::Url { url } => {
4780                table.insert("url", Value::from(url.as_ref()));
4781            }
4782            WheelWireSource::Path { path } => {
4783                table.insert("path", Value::from(PortablePath::from(path).to_string()));
4784            }
4785            WheelWireSource::Filename { filename } => {
4786                table.insert("filename", Value::from(filename.to_string()));
4787            }
4788        }
4789        if let Some(ref hash) = self.hash {
4790            table.insert("hash", Value::from(hash.to_string()));
4791        }
4792        if let Some(size) = self.size {
4793            table.insert(
4794                "size",
4795                toml_edit::ser::ValueSerializer::new().serialize_u64(size)?,
4796            );
4797        }
4798        if let Some(upload_time) = self.upload_time {
4799            table.insert("upload-time", Value::from(upload_time.to_string()));
4800        }
4801        if let Some(zstd) = &self.zstd {
4802            let mut inner = InlineTable::new();
4803            if let Some(ref hash) = zstd.hash {
4804                inner.insert("hash", Value::from(hash.to_string()));
4805            }
4806            if let Some(size) = zstd.size {
4807                inner.insert(
4808                    "size",
4809                    toml_edit::ser::ValueSerializer::new().serialize_u64(size)?,
4810                );
4811            }
4812            table.insert("zstd", Value::from(inner));
4813        }
4814        Ok(table)
4815    }
4816}
4817
4818impl TryFrom<WheelWire> for Wheel {
4819    type Error = String;
4820
4821    fn try_from(wire: WheelWire) -> Result<Self, String> {
4822        let filename = match &wire.url {
4823            WheelWireSource::Url { url } => {
4824                let filename = url.filename().map_err(|err| err.to_string())?;
4825                filename.parse::<WheelFilename>().map_err(|err| {
4826                    format!("failed to parse `{filename}` as wheel filename: {err}")
4827                })?
4828            }
4829            WheelWireSource::Path { path } => {
4830                let filename = path
4831                    .file_name()
4832                    .and_then(|file_name| file_name.to_str())
4833                    .ok_or_else(|| {
4834                        format!("path `{}` has no filename component", path.display())
4835                    })?;
4836                filename.parse::<WheelFilename>().map_err(|err| {
4837                    format!("failed to parse `{filename}` as wheel filename: {err}")
4838                })?
4839            }
4840            WheelWireSource::Filename { filename } => filename.clone(),
4841        };
4842
4843        Ok(Self {
4844            url: wire.url,
4845            hash: wire.hash,
4846            size: wire.size,
4847            upload_time: wire.upload_time,
4848            zstd: wire.zstd,
4849            filename,
4850        })
4851    }
4852}
4853
4854/// A single dependency of a package in a lockfile.
4855#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
4856pub struct Dependency {
4857    package_id: PackageId,
4858    extra: BTreeSet<ExtraName>,
4859    /// A marker simplified from the PEP 508 marker in `complexified_marker`
4860    /// by assuming `requires-python` is satisfied. So if
4861    /// `requires-python = '>=3.8'`, then
4862    /// `python_version >= '3.8' and python_version < '3.12'`
4863    /// gets simplified to `python_version < '3.12'`.
4864    ///
4865    /// Generally speaking, this marker should not be exposed to
4866    /// anything outside this module unless it's for a specialized use
4867    /// case. But specifically, it should never be used to evaluate
4868    /// against a marker environment or for disjointness checks or any
4869    /// other kind of marker algebra.
4870    ///
4871    /// It exists because there are some cases where we do actually
4872    /// want to compare markers in their "simplified" form. For
4873    /// example, when collapsing the extras on duplicate dependencies.
4874    /// Even if a dependency has different complexified markers,
4875    /// they might have identical markers once simplified. And since
4876    /// `requires-python` applies to the entire lock file, it's
4877    /// acceptable to do comparisons on the simplified form.
4878    simplified_marker: SimplifiedMarkerTree,
4879    /// The "complexified" marker is a universal marker whose PEP 508
4880    /// marker can stand on its own independent of `requires-python`.
4881    /// It can be safely used for any kind of marker algebra.
4882    complexified_marker: UniversalMarker,
4883}
4884
4885impl Dependency {
4886    fn new(
4887        requires_python: &RequiresPython,
4888        package_id: PackageId,
4889        extra: BTreeSet<ExtraName>,
4890        complexified_marker: UniversalMarker,
4891    ) -> Self {
4892        let simplified_marker =
4893            SimplifiedMarkerTree::new(requires_python, complexified_marker.combined());
4894        let complexified_marker = simplified_marker.into_marker(requires_python);
4895        Self {
4896            package_id,
4897            extra,
4898            simplified_marker,
4899            complexified_marker: UniversalMarker::from_combined(complexified_marker),
4900        }
4901    }
4902
4903    fn from_annotated_dist(
4904        requires_python: &RequiresPython,
4905        annotated_dist: &AnnotatedDist,
4906        complexified_marker: UniversalMarker,
4907        root: &Path,
4908    ) -> Result<Self, LockError> {
4909        let package_id = PackageId::from_annotated_dist(annotated_dist, root)?;
4910        let extra = annotated_dist.extra.iter().cloned().collect();
4911        Ok(Self::new(
4912            requires_python,
4913            package_id,
4914            extra,
4915            complexified_marker,
4916        ))
4917    }
4918
4919    /// Returns the TOML representation of this dependency.
4920    fn to_toml(
4921        &self,
4922        _requires_python: &RequiresPython,
4923        dist_count_by_name: &FxHashMap<PackageName, u64>,
4924    ) -> Table {
4925        let mut table = Table::new();
4926        self.package_id
4927            .to_toml(Some(dist_count_by_name), &mut table);
4928        if !self.extra.is_empty() {
4929            let extra_array = self
4930                .extra
4931                .iter()
4932                .map(ToString::to_string)
4933                .collect::<Array>();
4934            table.insert("extra", value(extra_array));
4935        }
4936        if let Some(marker) = self.simplified_marker.try_to_string() {
4937            table.insert("marker", value(marker));
4938        }
4939
4940        table
4941    }
4942
4943    /// Returns the package name of this dependency.
4944    pub fn package_name(&self) -> &PackageName {
4945        &self.package_id.name
4946    }
4947
4948    /// Returns the extras specified on this dependency.
4949    pub fn extra(&self) -> &BTreeSet<ExtraName> {
4950        &self.extra
4951    }
4952}
4953
4954impl Display for Dependency {
4955    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
4956        match (self.extra.is_empty(), self.package_id.version.as_ref()) {
4957            (true, Some(version)) => write!(f, "{}=={}", self.package_id.name, version),
4958            (true, None) => write!(f, "{}", self.package_id.name),
4959            (false, Some(version)) => write!(
4960                f,
4961                "{}[{}]=={}",
4962                self.package_id.name,
4963                self.extra.iter().join(","),
4964                version
4965            ),
4966            (false, None) => write!(
4967                f,
4968                "{}[{}]",
4969                self.package_id.name,
4970                self.extra.iter().join(",")
4971            ),
4972        }
4973    }
4974}
4975
4976/// A single dependency of a package in a lockfile.
4977#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, serde::Deserialize)]
4978#[serde(rename_all = "kebab-case")]
4979struct DependencyWire {
4980    #[serde(flatten)]
4981    package_id: PackageIdForDependency,
4982    #[serde(default)]
4983    extra: BTreeSet<ExtraName>,
4984    #[serde(default)]
4985    marker: SimplifiedMarkerTree,
4986}
4987
4988impl DependencyWire {
4989    fn unwire(
4990        self,
4991        requires_python: &RequiresPython,
4992        unambiguous_package_ids: &FxHashMap<PackageName, PackageId>,
4993    ) -> Result<Dependency, LockError> {
4994        let complexified_marker = self.marker.into_marker(requires_python);
4995        Ok(Dependency {
4996            package_id: self.package_id.unwire(unambiguous_package_ids)?,
4997            extra: self.extra,
4998            simplified_marker: self.marker,
4999            complexified_marker: UniversalMarker::from_combined(complexified_marker),
5000        })
5001    }
5002}
5003
5004/// A single hash for a distribution artifact in a lockfile.
5005///
5006/// A hash is encoded as a single TOML string in the format
5007/// `{algorithm}:{digest}`.
5008#[derive(Clone, Debug, PartialEq, Eq)]
5009struct Hash(HashDigest);
5010
5011impl From<HashDigest> for Hash {
5012    fn from(hd: HashDigest) -> Self {
5013        Self(hd)
5014    }
5015}
5016
5017impl FromStr for Hash {
5018    type Err = HashParseError;
5019
5020    fn from_str(s: &str) -> Result<Self, HashParseError> {
5021        let (algorithm, digest) = s.split_once(':').ok_or(HashParseError(
5022            "expected '{algorithm}:{digest}', but found no ':' in hash digest",
5023        ))?;
5024        let algorithm = algorithm
5025            .parse()
5026            .map_err(|_| HashParseError("unrecognized hash algorithm"))?;
5027        Ok(Self(HashDigest {
5028            algorithm,
5029            digest: digest.into(),
5030        }))
5031    }
5032}
5033
5034impl Display for Hash {
5035    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
5036        write!(f, "{}:{}", self.0.algorithm, self.0.digest)
5037    }
5038}
5039
5040impl<'de> serde::Deserialize<'de> for Hash {
5041    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
5042    where
5043        D: serde::de::Deserializer<'de>,
5044    {
5045        struct Visitor;
5046
5047        impl serde::de::Visitor<'_> for Visitor {
5048            type Value = Hash;
5049
5050            fn expecting(&self, f: &mut Formatter) -> std::fmt::Result {
5051                f.write_str("a string")
5052            }
5053
5054            fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
5055                Hash::from_str(v).map_err(serde::de::Error::custom)
5056            }
5057        }
5058
5059        deserializer.deserialize_str(Visitor)
5060    }
5061}
5062
5063impl From<Hash> for Hashes {
5064    fn from(value: Hash) -> Self {
5065        match value.0.algorithm {
5066            HashAlgorithm::Md5 => Self {
5067                md5: Some(value.0.digest),
5068                sha256: None,
5069                sha384: None,
5070                sha512: None,
5071                blake2b: None,
5072            },
5073            HashAlgorithm::Sha256 => Self {
5074                md5: None,
5075                sha256: Some(value.0.digest),
5076                sha384: None,
5077                sha512: None,
5078                blake2b: None,
5079            },
5080            HashAlgorithm::Sha384 => Self {
5081                md5: None,
5082                sha256: None,
5083                sha384: Some(value.0.digest),
5084                sha512: None,
5085                blake2b: None,
5086            },
5087            HashAlgorithm::Sha512 => Self {
5088                md5: None,
5089                sha256: None,
5090                sha384: None,
5091                sha512: Some(value.0.digest),
5092                blake2b: None,
5093            },
5094            HashAlgorithm::Blake2b => Self {
5095                md5: None,
5096                sha256: None,
5097                sha384: None,
5098                sha512: None,
5099                blake2b: Some(value.0.digest),
5100            },
5101        }
5102    }
5103}
5104
5105/// Convert a [`FileLocation`] into a normalized [`UrlString`].
5106fn normalize_file_location(location: &FileLocation) -> Result<UrlString, ToUrlError> {
5107    match location {
5108        FileLocation::AbsoluteUrl(absolute) => Ok(absolute.without_fragment().into_owned()),
5109        FileLocation::RelativeUrl(_, _) => Ok(normalize_url(location.to_url()?)),
5110    }
5111}
5112
5113/// Convert a [`DisplaySafeUrl`] into a normalized [`UrlString`] by removing the fragment.
5114fn normalize_url(mut url: DisplaySafeUrl) -> UrlString {
5115    url.set_fragment(None);
5116    UrlString::from(url)
5117}
5118
5119/// Normalize a [`Requirement`], which could come from a lockfile, a `pyproject.toml`, etc.
5120///
5121/// Performs the following steps:
5122///
5123/// 1. Removes any sensitive credentials.
5124/// 2. Ensures that the lock and install paths are appropriately framed with respect to the
5125///    current [`Workspace`].
5126/// 3. Removes the `origin` field, which is only used in `requirements.txt`.
5127/// 4. Simplifies the markers using the provided [`RequiresPython`] instance.
5128fn normalize_requirement(
5129    mut requirement: Requirement,
5130    root: &Path,
5131    requires_python: &RequiresPython,
5132) -> Result<Requirement, LockError> {
5133    // Sort the extras and groups for consistency.
5134    requirement.extras.sort();
5135    requirement.groups.sort();
5136
5137    // Normalize the requirement source.
5138    match requirement.source {
5139        RequirementSource::Git {
5140            git,
5141            subdirectory,
5142            url: _,
5143        } => {
5144            // Reconstruct the Git URL.
5145            let git = {
5146                let mut repository = git.repository().clone();
5147
5148                // Remove the credentials.
5149                repository.remove_credentials();
5150
5151                // Remove the fragment and query from the URL; they're already present in the source.
5152                repository.set_fragment(None);
5153                repository.set_query(None);
5154
5155                GitUrl::from_fields(
5156                    repository,
5157                    git.reference().clone(),
5158                    git.precise(),
5159                    git.lfs(),
5160                )?
5161            };
5162
5163            // Reconstruct the PEP 508 URL from the underlying data.
5164            let url = DisplaySafeUrl::from(ParsedGitUrl {
5165                url: git.clone(),
5166                subdirectory: subdirectory.clone(),
5167            });
5168
5169            Ok(Requirement {
5170                name: requirement.name,
5171                extras: requirement.extras,
5172                groups: requirement.groups,
5173                marker: requires_python.simplify_markers(requirement.marker),
5174                source: RequirementSource::Git {
5175                    git,
5176                    subdirectory,
5177                    url: VerbatimUrl::from_url(url),
5178                },
5179                origin: None,
5180            })
5181        }
5182        RequirementSource::Path {
5183            install_path,
5184            ext,
5185            url: _,
5186        } => {
5187            let install_path =
5188                uv_fs::normalize_path_buf(root.join(&install_path)).into_boxed_path();
5189            let url = VerbatimUrl::from_normalized_path(&install_path)
5190                .map_err(LockErrorKind::RequirementVerbatimUrl)?;
5191
5192            Ok(Requirement {
5193                name: requirement.name,
5194                extras: requirement.extras,
5195                groups: requirement.groups,
5196                marker: requires_python.simplify_markers(requirement.marker),
5197                source: RequirementSource::Path {
5198                    install_path,
5199                    ext,
5200                    url,
5201                },
5202                origin: None,
5203            })
5204        }
5205        RequirementSource::Directory {
5206            install_path,
5207            editable,
5208            r#virtual,
5209            url: _,
5210        } => {
5211            let install_path =
5212                uv_fs::normalize_path_buf(root.join(&install_path)).into_boxed_path();
5213            let url = VerbatimUrl::from_normalized_path(&install_path)
5214                .map_err(LockErrorKind::RequirementVerbatimUrl)?;
5215
5216            Ok(Requirement {
5217                name: requirement.name,
5218                extras: requirement.extras,
5219                groups: requirement.groups,
5220                marker: requires_python.simplify_markers(requirement.marker),
5221                source: RequirementSource::Directory {
5222                    install_path,
5223                    editable: Some(editable.unwrap_or(false)),
5224                    r#virtual: Some(r#virtual.unwrap_or(false)),
5225                    url,
5226                },
5227                origin: None,
5228            })
5229        }
5230        RequirementSource::Registry {
5231            specifier,
5232            index,
5233            conflict,
5234        } => {
5235            // Round-trip the index to remove anything apart from the URL.
5236            let index = index
5237                .map(|index| index.url.into_url())
5238                .map(|mut index| {
5239                    index.remove_credentials();
5240                    index
5241                })
5242                .map(|index| IndexMetadata::from(IndexUrl::from(VerbatimUrl::from_url(index))));
5243            Ok(Requirement {
5244                name: requirement.name,
5245                extras: requirement.extras,
5246                groups: requirement.groups,
5247                marker: requires_python.simplify_markers(requirement.marker),
5248                source: RequirementSource::Registry {
5249                    specifier,
5250                    index,
5251                    conflict,
5252                },
5253                origin: None,
5254            })
5255        }
5256        RequirementSource::Url {
5257            mut location,
5258            subdirectory,
5259            ext,
5260            url: _,
5261        } => {
5262            // Remove the credentials.
5263            location.remove_credentials();
5264
5265            // Remove the fragment from the URL; it's already present in the source.
5266            location.set_fragment(None);
5267
5268            // Reconstruct the PEP 508 URL from the underlying data.
5269            let url = DisplaySafeUrl::from(ParsedArchiveUrl {
5270                url: location.clone(),
5271                subdirectory: subdirectory.clone(),
5272                ext,
5273            });
5274
5275            Ok(Requirement {
5276                name: requirement.name,
5277                extras: requirement.extras,
5278                groups: requirement.groups,
5279                marker: requires_python.simplify_markers(requirement.marker),
5280                source: RequirementSource::Url {
5281                    location,
5282                    subdirectory,
5283                    ext,
5284                    url: VerbatimUrl::from_url(url),
5285                },
5286                origin: None,
5287            })
5288        }
5289    }
5290}
5291
5292#[derive(Debug)]
5293pub struct LockError {
5294    kind: Box<LockErrorKind>,
5295    hint: Option<WheelTagHint>,
5296}
5297
5298impl std::error::Error for LockError {
5299    fn source(&self) -> Option<&(dyn Error + 'static)> {
5300        self.kind.source()
5301    }
5302}
5303
5304impl std::fmt::Display for LockError {
5305    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5306        write!(f, "{}", self.kind)?;
5307        if let Some(hint) = &self.hint {
5308            write!(f, "\n\n{hint}")?;
5309        }
5310        Ok(())
5311    }
5312}
5313
5314impl LockError {
5315    /// Returns true if the [`LockError`] is a resolver error.
5316    pub fn is_resolution(&self) -> bool {
5317        matches!(&*self.kind, LockErrorKind::Resolution { .. })
5318    }
5319}
5320
5321impl<E> From<E> for LockError
5322where
5323    LockErrorKind: From<E>,
5324{
5325    fn from(err: E) -> Self {
5326        Self {
5327            kind: Box::new(LockErrorKind::from(err)),
5328            hint: None,
5329        }
5330    }
5331}
5332
5333#[derive(Debug, Clone, PartialEq, Eq)]
5334#[allow(clippy::enum_variant_names)]
5335enum WheelTagHint {
5336    /// None of the available wheels for a package have a compatible Python language tag (e.g.,
5337    /// `cp310` in `cp310-abi3-manylinux_2_17_x86_64.whl`).
5338    LanguageTags {
5339        package: PackageName,
5340        version: Option<Version>,
5341        tags: BTreeSet<LanguageTag>,
5342        best: Option<LanguageTag>,
5343    },
5344    /// None of the available wheels for a package have a compatible ABI tag (e.g., `abi3` in
5345    /// `cp310-abi3-manylinux_2_17_x86_64.whl`).
5346    AbiTags {
5347        package: PackageName,
5348        version: Option<Version>,
5349        tags: BTreeSet<AbiTag>,
5350        best: Option<AbiTag>,
5351    },
5352    /// None of the available wheels for a package have a compatible platform tag (e.g.,
5353    /// `manylinux_2_17_x86_64` in `cp310-abi3-manylinux_2_17_x86_64.whl`).
5354    PlatformTags {
5355        package: PackageName,
5356        version: Option<Version>,
5357        tags: BTreeSet<PlatformTag>,
5358        best: Option<PlatformTag>,
5359        markers: MarkerEnvironment,
5360    },
5361}
5362
5363impl WheelTagHint {
5364    /// Generate a [`WheelTagHint`] from the given (incompatible) wheels.
5365    fn from_wheels(
5366        name: &PackageName,
5367        version: Option<&Version>,
5368        filenames: &[&WheelFilename],
5369        tags: &Tags,
5370        markers: &MarkerEnvironment,
5371    ) -> Option<Self> {
5372        let incompatibility = filenames
5373            .iter()
5374            .map(|filename| {
5375                tags.compatibility(
5376                    filename.python_tags(),
5377                    filename.abi_tags(),
5378                    filename.platform_tags(),
5379                )
5380            })
5381            .max()?;
5382        match incompatibility {
5383            TagCompatibility::Incompatible(IncompatibleTag::Python) => {
5384                let best = tags.python_tag();
5385                let tags = Self::python_tags(filenames.iter().copied()).collect::<BTreeSet<_>>();
5386                if tags.is_empty() {
5387                    None
5388                } else {
5389                    Some(Self::LanguageTags {
5390                        package: name.clone(),
5391                        version: version.cloned(),
5392                        tags,
5393                        best,
5394                    })
5395                }
5396            }
5397            TagCompatibility::Incompatible(IncompatibleTag::Abi) => {
5398                let best = tags.abi_tag();
5399                let tags = Self::abi_tags(filenames.iter().copied())
5400                    // Ignore `none`, which is universally compatible.
5401                    //
5402                    // As an example, `none` can appear here if we're solving for Python 3.13, and
5403                    // the distribution includes a wheel for `cp312-none-macosx_11_0_arm64`.
5404                    //
5405                    // In that case, the wheel isn't compatible, but when solving for Python 3.13,
5406                    // the `cp312` Python tag _can_ be compatible (e.g., for `cp312-abi3-macosx_11_0_arm64.whl`),
5407                    // so this is considered an ABI incompatibility rather than Python incompatibility.
5408                    .filter(|tag| *tag != AbiTag::None)
5409                    .collect::<BTreeSet<_>>();
5410                if tags.is_empty() {
5411                    None
5412                } else {
5413                    Some(Self::AbiTags {
5414                        package: name.clone(),
5415                        version: version.cloned(),
5416                        tags,
5417                        best,
5418                    })
5419                }
5420            }
5421            TagCompatibility::Incompatible(IncompatibleTag::Platform) => {
5422                let best = tags.platform_tag().cloned();
5423                let incompatible_tags = Self::platform_tags(filenames.iter().copied(), tags)
5424                    .cloned()
5425                    .collect::<BTreeSet<_>>();
5426                if incompatible_tags.is_empty() {
5427                    None
5428                } else {
5429                    Some(Self::PlatformTags {
5430                        package: name.clone(),
5431                        version: version.cloned(),
5432                        tags: incompatible_tags,
5433                        best,
5434                        markers: markers.clone(),
5435                    })
5436                }
5437            }
5438            _ => None,
5439        }
5440    }
5441
5442    /// Returns an iterator over the compatible Python tags of the available wheels.
5443    fn python_tags<'a>(
5444        filenames: impl Iterator<Item = &'a WheelFilename> + 'a,
5445    ) -> impl Iterator<Item = LanguageTag> + 'a {
5446        filenames.flat_map(WheelFilename::python_tags).copied()
5447    }
5448
5449    /// Returns an iterator over the compatible Python tags of the available wheels.
5450    fn abi_tags<'a>(
5451        filenames: impl Iterator<Item = &'a WheelFilename> + 'a,
5452    ) -> impl Iterator<Item = AbiTag> + 'a {
5453        filenames.flat_map(WheelFilename::abi_tags).copied()
5454    }
5455
5456    /// Returns the set of platform tags for the distribution that are ABI-compatible with the given
5457    /// tags.
5458    fn platform_tags<'a>(
5459        filenames: impl Iterator<Item = &'a WheelFilename> + 'a,
5460        tags: &'a Tags,
5461    ) -> impl Iterator<Item = &'a PlatformTag> + 'a {
5462        filenames.flat_map(move |filename| {
5463            if filename.python_tags().iter().any(|wheel_py| {
5464                filename
5465                    .abi_tags()
5466                    .iter()
5467                    .any(|wheel_abi| tags.is_compatible_abi(*wheel_py, *wheel_abi))
5468            }) {
5469                filename.platform_tags().iter()
5470            } else {
5471                [].iter()
5472            }
5473        })
5474    }
5475
5476    fn suggest_environment_marker(markers: &MarkerEnvironment) -> String {
5477        let sys_platform = markers.sys_platform();
5478        let platform_machine = markers.platform_machine();
5479
5480        // Generate the marker string based on actual environment values
5481        if platform_machine.is_empty() {
5482            format!("sys_platform == '{sys_platform}'")
5483        } else {
5484            format!("sys_platform == '{sys_platform}' and platform_machine == '{platform_machine}'")
5485        }
5486    }
5487}
5488
5489impl std::fmt::Display for WheelTagHint {
5490    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5491        match self {
5492            Self::LanguageTags {
5493                package,
5494                version,
5495                tags,
5496                best,
5497            } => {
5498                if let Some(best) = best {
5499                    let s = if tags.len() == 1 { "" } else { "s" };
5500                    let best = if let Some(pretty) = best.pretty() {
5501                        format!("{} (`{}`)", pretty.cyan(), best.cyan())
5502                    } else {
5503                        format!("{}", best.cyan())
5504                    };
5505                    if let Some(version) = version {
5506                        write!(
5507                            f,
5508                            "{}{} You're using {}, but `{}` ({}) only has wheels with the following Python implementation tag{s}: {}",
5509                            "hint".bold().cyan(),
5510                            ":".bold(),
5511                            best,
5512                            package.cyan(),
5513                            format!("v{version}").cyan(),
5514                            tags.iter()
5515                                .map(|tag| format!("`{}`", tag.cyan()))
5516                                .join(", "),
5517                        )
5518                    } else {
5519                        write!(
5520                            f,
5521                            "{}{} You're using {}, but `{}` only has wheels with the following Python implementation tag{s}: {}",
5522                            "hint".bold().cyan(),
5523                            ":".bold(),
5524                            best,
5525                            package.cyan(),
5526                            tags.iter()
5527                                .map(|tag| format!("`{}`", tag.cyan()))
5528                                .join(", "),
5529                        )
5530                    }
5531                } else {
5532                    let s = if tags.len() == 1 { "" } else { "s" };
5533                    if let Some(version) = version {
5534                        write!(
5535                            f,
5536                            "{}{} Wheels are available for `{}` ({}) with the following Python implementation tag{s}: {}",
5537                            "hint".bold().cyan(),
5538                            ":".bold(),
5539                            package.cyan(),
5540                            format!("v{version}").cyan(),
5541                            tags.iter()
5542                                .map(|tag| format!("`{}`", tag.cyan()))
5543                                .join(", "),
5544                        )
5545                    } else {
5546                        write!(
5547                            f,
5548                            "{}{} Wheels are available for `{}` with the following Python implementation tag{s}: {}",
5549                            "hint".bold().cyan(),
5550                            ":".bold(),
5551                            package.cyan(),
5552                            tags.iter()
5553                                .map(|tag| format!("`{}`", tag.cyan()))
5554                                .join(", "),
5555                        )
5556                    }
5557                }
5558            }
5559            Self::AbiTags {
5560                package,
5561                version,
5562                tags,
5563                best,
5564            } => {
5565                if let Some(best) = best {
5566                    let s = if tags.len() == 1 { "" } else { "s" };
5567                    let best = if let Some(pretty) = best.pretty() {
5568                        format!("{} (`{}`)", pretty.cyan(), best.cyan())
5569                    } else {
5570                        format!("{}", best.cyan())
5571                    };
5572                    if let Some(version) = version {
5573                        write!(
5574                            f,
5575                            "{}{} You're using {}, but `{}` ({}) only has wheels with the following Python ABI tag{s}: {}",
5576                            "hint".bold().cyan(),
5577                            ":".bold(),
5578                            best,
5579                            package.cyan(),
5580                            format!("v{version}").cyan(),
5581                            tags.iter()
5582                                .map(|tag| format!("`{}`", tag.cyan()))
5583                                .join(", "),
5584                        )
5585                    } else {
5586                        write!(
5587                            f,
5588                            "{}{} You're using {}, but `{}` only has wheels with the following Python ABI tag{s}: {}",
5589                            "hint".bold().cyan(),
5590                            ":".bold(),
5591                            best,
5592                            package.cyan(),
5593                            tags.iter()
5594                                .map(|tag| format!("`{}`", tag.cyan()))
5595                                .join(", "),
5596                        )
5597                    }
5598                } else {
5599                    let s = if tags.len() == 1 { "" } else { "s" };
5600                    if let Some(version) = version {
5601                        write!(
5602                            f,
5603                            "{}{} Wheels are available for `{}` ({}) with the following Python ABI tag{s}: {}",
5604                            "hint".bold().cyan(),
5605                            ":".bold(),
5606                            package.cyan(),
5607                            format!("v{version}").cyan(),
5608                            tags.iter()
5609                                .map(|tag| format!("`{}`", tag.cyan()))
5610                                .join(", "),
5611                        )
5612                    } else {
5613                        write!(
5614                            f,
5615                            "{}{} Wheels are available for `{}` with the following Python ABI tag{s}: {}",
5616                            "hint".bold().cyan(),
5617                            ":".bold(),
5618                            package.cyan(),
5619                            tags.iter()
5620                                .map(|tag| format!("`{}`", tag.cyan()))
5621                                .join(", "),
5622                        )
5623                    }
5624                }
5625            }
5626            Self::PlatformTags {
5627                package,
5628                version,
5629                tags,
5630                best,
5631                markers,
5632            } => {
5633                let s = if tags.len() == 1 { "" } else { "s" };
5634                if let Some(best) = best {
5635                    let example_marker = Self::suggest_environment_marker(markers);
5636                    let best = if let Some(pretty) = best.pretty() {
5637                        format!("{} (`{}`)", pretty.cyan(), best.cyan())
5638                    } else {
5639                        format!("`{}`", best.cyan())
5640                    };
5641                    let package_ref = if let Some(version) = version {
5642                        format!("`{}` ({})", package.cyan(), format!("v{version}").cyan())
5643                    } else {
5644                        format!("`{}`", package.cyan())
5645                    };
5646                    write!(
5647                        f,
5648                        "{}{} You're on {}, but {} only has wheels for the following platform{s}: {}; consider adding {} to `{}` to ensure uv resolves to a version with compatible wheels",
5649                        "hint".bold().cyan(),
5650                        ":".bold(),
5651                        best,
5652                        package_ref,
5653                        tags.iter()
5654                            .map(|tag| format!("`{}`", tag.cyan()))
5655                            .join(", "),
5656                        format!("\"{example_marker}\"").cyan(),
5657                        "tool.uv.required-environments".green()
5658                    )
5659                } else {
5660                    if let Some(version) = version {
5661                        write!(
5662                            f,
5663                            "{}{} Wheels are available for `{}` ({}) on the following platform{s}: {}",
5664                            "hint".bold().cyan(),
5665                            ":".bold(),
5666                            package.cyan(),
5667                            format!("v{version}").cyan(),
5668                            tags.iter()
5669                                .map(|tag| format!("`{}`", tag.cyan()))
5670                                .join(", "),
5671                        )
5672                    } else {
5673                        write!(
5674                            f,
5675                            "{}{} Wheels are available for `{}` on the following platform{s}: {}",
5676                            "hint".bold().cyan(),
5677                            ":".bold(),
5678                            package.cyan(),
5679                            tags.iter()
5680                                .map(|tag| format!("`{}`", tag.cyan()))
5681                                .join(", "),
5682                        )
5683                    }
5684                }
5685            }
5686        }
5687    }
5688}
5689
5690/// An error that occurs when generating a `Lock` data structure.
5691///
5692/// These errors are sometimes the result of possible programming bugs.
5693/// For example, if there are two or more duplicative distributions given
5694/// to `Lock::new`, then an error is returned. It's likely that the fault
5695/// is with the caller somewhere in such cases.
5696#[derive(Debug, thiserror::Error)]
5697enum LockErrorKind {
5698    /// An error that occurs when multiple packages with the same
5699    /// ID were found.
5700    #[error("Found duplicate package `{id}`", id = id.cyan())]
5701    DuplicatePackage {
5702        /// The ID of the conflicting package.
5703        id: PackageId,
5704    },
5705    /// An error that occurs when there are multiple dependencies for the
5706    /// same package that have identical identifiers.
5707    #[error("For package `{id}`, found duplicate dependency `{dependency}`", id = id.cyan(), dependency = dependency.cyan())]
5708    DuplicateDependency {
5709        /// The ID of the package for which a duplicate dependency was
5710        /// found.
5711        id: PackageId,
5712        /// The ID of the conflicting dependency.
5713        dependency: Dependency,
5714    },
5715    /// An error that occurs when there are multiple dependencies for the
5716    /// same package that have identical identifiers, as part of the
5717    /// that package's optional dependencies.
5718    #[error("For package `{id}`, found duplicate dependency `{dependency}`", id = format!("{id}[{extra}]").cyan(), dependency = dependency.cyan())]
5719    DuplicateOptionalDependency {
5720        /// The ID of the package for which a duplicate dependency was
5721        /// found.
5722        id: PackageId,
5723        /// The name of the extra.
5724        extra: ExtraName,
5725        /// The ID of the conflicting dependency.
5726        dependency: Dependency,
5727    },
5728    /// An error that occurs when there are multiple dependencies for the
5729    /// same package that have identical identifiers, as part of the
5730    /// that package's development dependencies.
5731    #[error("For package `{id}`, found duplicate dependency `{dependency}`", id = format!("{id}:{group}").cyan(), dependency = dependency.cyan())]
5732    DuplicateDevDependency {
5733        /// The ID of the package for which a duplicate dependency was
5734        /// found.
5735        id: PackageId,
5736        /// The name of the dev dependency group.
5737        group: GroupName,
5738        /// The ID of the conflicting dependency.
5739        dependency: Dependency,
5740    },
5741    /// An error that occurs when the URL to a file for a wheel or
5742    /// source dist could not be converted to a structured `url::Url`.
5743    #[error(transparent)]
5744    InvalidUrl(
5745        /// The underlying error that occurred. This includes the
5746        /// errant URL in its error message.
5747        #[from]
5748        ToUrlError,
5749    ),
5750    /// An error that occurs when the extension can't be determined
5751    /// for a given wheel or source distribution.
5752    #[error("Failed to parse file extension for `{id}`; expected one of: {err}", id = id.cyan())]
5753    MissingExtension {
5754        /// The filename that was expected to have an extension.
5755        id: PackageId,
5756        /// The list of valid extensions that were expected.
5757        err: ExtensionError,
5758    },
5759    /// Failed to parse a Git source URL.
5760    #[error("Failed to parse Git URL")]
5761    InvalidGitSourceUrl(
5762        /// The underlying error that occurred. This includes the
5763        /// errant URL in the message.
5764        #[source]
5765        SourceParseError,
5766    ),
5767    #[error("Failed to parse timestamp")]
5768    InvalidTimestamp(
5769        /// The underlying error that occurred. This includes the
5770        /// errant timestamp in the message.
5771        #[source]
5772        jiff::Error,
5773    ),
5774    /// An error that occurs when there's an unrecognized dependency.
5775    ///
5776    /// That is, a dependency for a package that isn't in the lockfile.
5777    #[error("For package `{id}`, found dependency `{dependency}` with no locked package", id = id.cyan(), dependency = dependency.cyan())]
5778    UnrecognizedDependency {
5779        /// The ID of the package that has an unrecognized dependency.
5780        id: PackageId,
5781        /// The ID of the dependency that doesn't have a corresponding package
5782        /// entry.
5783        dependency: Dependency,
5784    },
5785    /// An error that occurs when a hash is expected (or not) for a particular
5786    /// artifact, but one was not found (or was).
5787    #[error("Since the package `{id}` comes from a {source} dependency, a hash was {expected} but one was not found for {artifact_type}", id = id.cyan(), source = id.source.name(), expected = if *expected { "expected" } else { "not expected" })]
5788    Hash {
5789        /// The ID of the package that has a missing hash.
5790        id: PackageId,
5791        /// The specific type of artifact, e.g., "source package"
5792        /// or "wheel".
5793        artifact_type: &'static str,
5794        /// When true, a hash is expected to be present.
5795        expected: bool,
5796    },
5797    /// An error that occurs when a package is included with an extra name,
5798    /// but no corresponding base package (i.e., without the extra) exists.
5799    #[error("Found package `{id}` with extra `{extra}` but no base package", id = id.cyan(), extra = extra.cyan())]
5800    MissingExtraBase {
5801        /// The ID of the package that has a missing base.
5802        id: PackageId,
5803        /// The extra name that was found.
5804        extra: ExtraName,
5805    },
5806    /// An error that occurs when a package is included with a development
5807    /// dependency group, but no corresponding base package (i.e., without
5808    /// the group) exists.
5809    #[error("Found package `{id}` with development dependency group `{group}` but no base package", id = id.cyan())]
5810    MissingDevBase {
5811        /// The ID of the package that has a missing base.
5812        id: PackageId,
5813        /// The development dependency group that was found.
5814        group: GroupName,
5815    },
5816    /// An error that occurs from an invalid lockfile where a wheel comes from a non-wheel source
5817    /// such as a directory.
5818    #[error("Wheels cannot come from {source_type} sources")]
5819    InvalidWheelSource {
5820        /// The ID of the distribution that has a missing base.
5821        id: PackageId,
5822        /// The kind of the invalid source.
5823        source_type: &'static str,
5824    },
5825    /// An error that occurs when a distribution indicates that it is sourced from a remote
5826    /// registry, but is missing a URL.
5827    #[error("Found registry distribution `{name}` ({version}) without a valid URL", name = name.cyan(), version = format!("v{version}").cyan())]
5828    MissingUrl {
5829        /// The name of the distribution that is missing a URL.
5830        name: PackageName,
5831        /// The version of the distribution that is missing a URL.
5832        version: Version,
5833    },
5834    /// An error that occurs when a distribution indicates that it is sourced from a local registry,
5835    /// but is missing a path.
5836    #[error("Found registry distribution `{name}` ({version}) without a valid path", name = name.cyan(), version = format!("v{version}").cyan())]
5837    MissingPath {
5838        /// The name of the distribution that is missing a path.
5839        name: PackageName,
5840        /// The version of the distribution that is missing a path.
5841        version: Version,
5842    },
5843    /// An error that occurs when a distribution indicates that it is sourced from a registry, but
5844    /// is missing a filename.
5845    #[error("Found registry distribution `{id}` without a valid filename", id = id.cyan())]
5846    MissingFilename {
5847        /// The ID of the distribution that is missing a filename.
5848        id: PackageId,
5849    },
5850    /// An error that occurs when a distribution is included with neither wheels nor a source
5851    /// distribution.
5852    #[error("Distribution `{id}` can't be installed because it doesn't have a source distribution or wheel for the current platform", id = id.cyan())]
5853    NeitherSourceDistNorWheel {
5854        /// The ID of the distribution.
5855        id: PackageId,
5856    },
5857    /// An error that occurs when a distribution is marked as both `--no-binary` and `--no-build`.
5858    #[error("Distribution `{id}` can't be installed because it is marked as both `--no-binary` and `--no-build`", id = id.cyan())]
5859    NoBinaryNoBuild {
5860        /// The ID of the distribution.
5861        id: PackageId,
5862    },
5863    /// An error that occurs when a distribution is marked as `--no-binary`, but no source
5864    /// distribution is available.
5865    #[error("Distribution `{id}` can't be installed because it is marked as `--no-binary` but has no source distribution", id = id.cyan())]
5866    NoBinary {
5867        /// The ID of the distribution.
5868        id: PackageId,
5869    },
5870    /// An error that occurs when a distribution is marked as `--no-build`, but no binary
5871    /// distribution is available.
5872    #[error("Distribution `{id}` can't be installed because it is marked as `--no-build` but has no binary distribution", id = id.cyan())]
5873    NoBuild {
5874        /// The ID of the distribution.
5875        id: PackageId,
5876    },
5877    /// An error that occurs when a wheel-only distribution is incompatible with the current
5878    /// platform.
5879    #[error("Distribution `{id}` can't be installed because the binary distribution is incompatible with the current platform", id = id.cyan())]
5880    IncompatibleWheelOnly {
5881        /// The ID of the distribution.
5882        id: PackageId,
5883    },
5884    /// An error that occurs when a wheel-only source is marked as `--no-binary`.
5885    #[error("Distribution `{id}` can't be installed because it is marked as `--no-binary` but is itself a binary distribution", id = id.cyan())]
5886    NoBinaryWheelOnly {
5887        /// The ID of the distribution.
5888        id: PackageId,
5889    },
5890    /// An error that occurs when converting between URLs and paths.
5891    #[error("Found dependency `{id}` with no locked distribution", id = id.cyan())]
5892    VerbatimUrl {
5893        /// The ID of the distribution that has a missing base.
5894        id: PackageId,
5895        /// The inner error we forward.
5896        #[source]
5897        err: VerbatimUrlError,
5898    },
5899    /// An error that occurs when parsing an existing requirement.
5900    #[error("Could not compute relative path between workspace and distribution")]
5901    DistributionRelativePath(
5902        /// The inner error we forward.
5903        #[source]
5904        io::Error,
5905    ),
5906    /// An error that occurs when converting an index URL to a relative path
5907    #[error("Could not compute relative path between workspace and index")]
5908    IndexRelativePath(
5909        /// The inner error we forward.
5910        #[source]
5911        io::Error,
5912    ),
5913    /// An error that occurs when converting a lockfile path from relative to absolute.
5914    #[error("Could not compute absolute path from workspace root and lockfile path")]
5915    AbsolutePath(
5916        /// The inner error we forward.
5917        #[source]
5918        io::Error,
5919    ),
5920    /// An error that occurs when an ambiguous `package.dependency` is
5921    /// missing a `version` field.
5922    #[error("Dependency `{name}` has missing `version` field but has more than one matching package", name = name.cyan())]
5923    MissingDependencyVersion {
5924        /// The name of the dependency that is missing a `version` field.
5925        name: PackageName,
5926    },
5927    /// An error that occurs when an ambiguous `package.dependency` is
5928    /// missing a `source` field.
5929    #[error("Dependency `{name}` has missing `source` field but has more than one matching package", name = name.cyan())]
5930    MissingDependencySource {
5931        /// The name of the dependency that is missing a `source` field.
5932        name: PackageName,
5933    },
5934    /// An error that occurs when parsing an existing requirement.
5935    #[error("Could not compute relative path between workspace and requirement")]
5936    RequirementRelativePath(
5937        /// The inner error we forward.
5938        #[source]
5939        io::Error,
5940    ),
5941    /// An error that occurs when parsing an existing requirement.
5942    #[error("Could not convert between URL and path")]
5943    RequirementVerbatimUrl(
5944        /// The inner error we forward.
5945        #[source]
5946        VerbatimUrlError,
5947    ),
5948    /// An error that occurs when parsing a registry's index URL.
5949    #[error("Could not convert between URL and path")]
5950    RegistryVerbatimUrl(
5951        /// The inner error we forward.
5952        #[source]
5953        VerbatimUrlError,
5954    ),
5955    /// An error that occurs when converting a path to a URL.
5956    #[error("Failed to convert path to URL: {path}", path = path.display().cyan())]
5957    PathToUrl { path: Box<Path> },
5958    /// An error that occurs when converting a URL to a path
5959    #[error("Failed to convert URL to path: {url}", url = url.cyan())]
5960    UrlToPath { url: DisplaySafeUrl },
5961    /// An error that occurs when multiple packages with the same
5962    /// name were found when identifying the root packages.
5963    #[error("Found multiple packages matching `{name}`", name = name.cyan())]
5964    MultipleRootPackages {
5965        /// The ID of the package.
5966        name: PackageName,
5967    },
5968    /// An error that occurs when a root package can't be found.
5969    #[error("Could not find root package `{name}`", name = name.cyan())]
5970    MissingRootPackage {
5971        /// The ID of the package.
5972        name: PackageName,
5973    },
5974    /// An error that occurs when resolving metadata for a package.
5975    #[error("Failed to generate package metadata for `{id}`", id = id.cyan())]
5976    Resolution {
5977        /// The ID of the distribution that failed to resolve.
5978        id: PackageId,
5979        /// The inner error we forward.
5980        #[source]
5981        err: uv_distribution::Error,
5982    },
5983    /// A package has inconsistent versions in a single entry
5984    // Using name instead of id since the version in the id is part of the conflict.
5985    #[error("The entry for package `{name}` ({version}) has wheel `{wheel_filename}` with inconsistent version ({wheel_version}), which indicates a malformed wheel. If this is intentional, set `{env_var}`.", name = name.cyan(), wheel_filename = wheel.filename, wheel_version = wheel.filename.version, env_var = "UV_SKIP_WHEEL_FILENAME_CHECK=1".green())]
5986    InconsistentVersions {
5987        /// The name of the package with the inconsistent entry.
5988        name: PackageName,
5989        /// The version of the package with the inconsistent entry.
5990        version: Version,
5991        /// The wheel with the inconsistent version.
5992        wheel: Wheel,
5993    },
5994    #[error(
5995        "Found conflicting extras `{package1}[{extra1}]` \
5996         and `{package2}[{extra2}]` enabled simultaneously"
5997    )]
5998    ConflictingExtra {
5999        package1: PackageName,
6000        extra1: ExtraName,
6001        package2: PackageName,
6002        extra2: ExtraName,
6003    },
6004    #[error(transparent)]
6005    GitUrlParse(#[from] GitUrlParseError),
6006    #[error("Failed to read `{path}`")]
6007    UnreadablePyprojectToml {
6008        path: PathBuf,
6009        #[source]
6010        err: std::io::Error,
6011    },
6012    #[error("Failed to parse `{path}`")]
6013    InvalidPyprojectToml {
6014        path: PathBuf,
6015        #[source]
6016        err: toml::de::Error,
6017    },
6018    /// An error that occurs when a workspace member has a non-local source.
6019    #[error("Workspace member `{id}` has non-local source", id = id.cyan())]
6020    NonLocalWorkspaceMember {
6021        /// The ID of the workspace member with an invalid source.
6022        id: PackageId,
6023    },
6024}
6025
6026/// An error that occurs when a source string could not be parsed.
6027#[derive(Debug, thiserror::Error)]
6028enum SourceParseError {
6029    /// An error that occurs when the URL in the source is invalid.
6030    #[error("Invalid URL in source `{given}`")]
6031    InvalidUrl {
6032        /// The source string given.
6033        given: String,
6034        /// The URL parse error.
6035        #[source]
6036        err: DisplaySafeUrlError,
6037    },
6038    /// An error that occurs when a Git URL is missing a precise commit SHA.
6039    #[error("Missing SHA in source `{given}`")]
6040    MissingSha {
6041        /// The source string given.
6042        given: String,
6043    },
6044    /// An error that occurs when a Git URL has an invalid SHA.
6045    #[error("Invalid SHA in source `{given}`")]
6046    InvalidSha {
6047        /// The source string given.
6048        given: String,
6049    },
6050}
6051
6052/// An error that occurs when a hash digest could not be parsed.
6053#[derive(Clone, Debug, Eq, PartialEq)]
6054struct HashParseError(&'static str);
6055
6056impl std::error::Error for HashParseError {}
6057
6058impl Display for HashParseError {
6059    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
6060        Display::fmt(self.0, f)
6061    }
6062}
6063
6064/// Format an array so that each element is on its own line and has a trailing comma.
6065///
6066/// Example:
6067///
6068/// ```toml
6069/// dependencies = [
6070///     { name = "idna" },
6071///     { name = "sniffio" },
6072/// ]
6073/// ```
6074fn each_element_on_its_line_array(elements: impl Iterator<Item = impl Into<Value>>) -> Array {
6075    let mut array = elements
6076        .map(|item| {
6077            let mut value = item.into();
6078            // Each dependency is on its own line and indented.
6079            value.decor_mut().set_prefix("\n    ");
6080            value
6081        })
6082        .collect::<Array>();
6083    // With a trailing comma, inserting another entry doesn't change the preceding line,
6084    // reducing the diff noise.
6085    array.set_trailing_comma(true);
6086    // The line break between the last element's comma and the closing square bracket.
6087    array.set_trailing("\n");
6088    array
6089}
6090
6091/// Returns the simplified string-ified version of each marker given.
6092///
6093/// Note that the marker strings returned will include conflict markers if they
6094/// are present.
6095fn simplified_universal_markers(
6096    markers: &[UniversalMarker],
6097    requires_python: &RequiresPython,
6098) -> Vec<String> {
6099    let mut pep508_only = vec![];
6100    let mut seen = FxHashSet::default();
6101    for marker in markers {
6102        let simplified =
6103            SimplifiedMarkerTree::new(requires_python, marker.pep508()).as_simplified_marker_tree();
6104        if seen.insert(simplified) {
6105            pep508_only.push(simplified);
6106        }
6107    }
6108    let any_overlap = pep508_only
6109        .iter()
6110        .tuple_combinations()
6111        .any(|(&marker1, &marker2)| !marker1.is_disjoint(marker2));
6112    let markers = if !any_overlap {
6113        pep508_only
6114    } else {
6115        markers
6116            .iter()
6117            .map(|marker| {
6118                SimplifiedMarkerTree::new(requires_python, marker.combined())
6119                    .as_simplified_marker_tree()
6120            })
6121            .collect()
6122    };
6123    markers
6124        .into_iter()
6125        .filter_map(MarkerTree::try_to_string)
6126        .collect()
6127}
6128
6129#[cfg(test)]
6130mod tests {
6131    use uv_warnings::anstream;
6132
6133    use super::*;
6134
6135    /// Assert a given display snapshot, stripping ANSI color codes.
6136    macro_rules! assert_stripped_snapshot {
6137        ($expr:expr, @$snapshot:literal) => {{
6138            let expr = format!("{}", $expr);
6139            let expr = format!("{}", anstream::adapter::strip_str(&expr));
6140            insta::assert_snapshot!(expr, @$snapshot);
6141        }};
6142    }
6143
6144    #[test]
6145    fn missing_dependency_source_unambiguous() {
6146        let data = r#"
6147version = 1
6148requires-python = ">=3.12"
6149
6150[[package]]
6151name = "a"
6152version = "0.1.0"
6153source = { registry = "https://pypi.org/simple" }
6154sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6155
6156[[package]]
6157name = "b"
6158version = "0.1.0"
6159source = { registry = "https://pypi.org/simple" }
6160sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6161
6162[[package.dependencies]]
6163name = "a"
6164version = "0.1.0"
6165"#;
6166        let result: Result<Lock, _> = toml::from_str(data);
6167        insta::assert_debug_snapshot!(result);
6168    }
6169
6170    #[test]
6171    fn missing_dependency_version_unambiguous() {
6172        let data = r#"
6173version = 1
6174requires-python = ">=3.12"
6175
6176[[package]]
6177name = "a"
6178version = "0.1.0"
6179source = { registry = "https://pypi.org/simple" }
6180sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6181
6182[[package]]
6183name = "b"
6184version = "0.1.0"
6185source = { registry = "https://pypi.org/simple" }
6186sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6187
6188[[package.dependencies]]
6189name = "a"
6190source = { registry = "https://pypi.org/simple" }
6191"#;
6192        let result: Result<Lock, _> = toml::from_str(data);
6193        insta::assert_debug_snapshot!(result);
6194    }
6195
6196    #[test]
6197    fn missing_dependency_source_version_unambiguous() {
6198        let data = r#"
6199version = 1
6200requires-python = ">=3.12"
6201
6202[[package]]
6203name = "a"
6204version = "0.1.0"
6205source = { registry = "https://pypi.org/simple" }
6206sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6207
6208[[package]]
6209name = "b"
6210version = "0.1.0"
6211source = { registry = "https://pypi.org/simple" }
6212sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6213
6214[[package.dependencies]]
6215name = "a"
6216"#;
6217        let result: Result<Lock, _> = toml::from_str(data);
6218        insta::assert_debug_snapshot!(result);
6219    }
6220
6221    #[test]
6222    fn missing_dependency_source_ambiguous() {
6223        let data = r#"
6224version = 1
6225requires-python = ">=3.12"
6226
6227[[package]]
6228name = "a"
6229version = "0.1.0"
6230source = { registry = "https://pypi.org/simple" }
6231sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6232
6233[[package]]
6234name = "a"
6235version = "0.1.1"
6236source = { registry = "https://pypi.org/simple" }
6237sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6238
6239[[package]]
6240name = "b"
6241version = "0.1.0"
6242source = { registry = "https://pypi.org/simple" }
6243sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6244
6245[[package.dependencies]]
6246name = "a"
6247version = "0.1.0"
6248"#;
6249        let result = toml::from_str::<Lock>(data).unwrap_err();
6250        assert_stripped_snapshot!(result, @"Dependency `a` has missing `source` field but has more than one matching package");
6251    }
6252
6253    #[test]
6254    fn missing_dependency_version_ambiguous() {
6255        let data = r#"
6256version = 1
6257requires-python = ">=3.12"
6258
6259[[package]]
6260name = "a"
6261version = "0.1.0"
6262source = { registry = "https://pypi.org/simple" }
6263sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6264
6265[[package]]
6266name = "a"
6267version = "0.1.1"
6268source = { registry = "https://pypi.org/simple" }
6269sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6270
6271[[package]]
6272name = "b"
6273version = "0.1.0"
6274source = { registry = "https://pypi.org/simple" }
6275sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6276
6277[[package.dependencies]]
6278name = "a"
6279source = { registry = "https://pypi.org/simple" }
6280"#;
6281        let result = toml::from_str::<Lock>(data).unwrap_err();
6282        assert_stripped_snapshot!(result, @"Dependency `a` has missing `version` field but has more than one matching package");
6283    }
6284
6285    #[test]
6286    fn missing_dependency_source_version_ambiguous() {
6287        let data = r#"
6288version = 1
6289requires-python = ">=3.12"
6290
6291[[package]]
6292name = "a"
6293version = "0.1.0"
6294source = { registry = "https://pypi.org/simple" }
6295sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6296
6297[[package]]
6298name = "a"
6299version = "0.1.1"
6300source = { registry = "https://pypi.org/simple" }
6301sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6302
6303[[package]]
6304name = "b"
6305version = "0.1.0"
6306source = { registry = "https://pypi.org/simple" }
6307sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6308
6309[[package.dependencies]]
6310name = "a"
6311"#;
6312        let result = toml::from_str::<Lock>(data).unwrap_err();
6313        assert_stripped_snapshot!(result, @"Dependency `a` has missing `source` field but has more than one matching package");
6314    }
6315
6316    #[test]
6317    fn missing_dependency_version_dynamic() {
6318        let data = r#"
6319version = 1
6320requires-python = ">=3.12"
6321
6322[[package]]
6323name = "a"
6324source = { editable = "path/to/a" }
6325
6326[[package]]
6327name = "a"
6328version = "0.1.1"
6329source = { registry = "https://pypi.org/simple" }
6330sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6331
6332[[package]]
6333name = "b"
6334version = "0.1.0"
6335source = { registry = "https://pypi.org/simple" }
6336sdist = { url = "https://example.com", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 0 }
6337
6338[[package.dependencies]]
6339name = "a"
6340source = { editable = "path/to/a" }
6341"#;
6342        let result = toml::from_str::<Lock>(data);
6343        insta::assert_debug_snapshot!(result);
6344    }
6345
6346    #[test]
6347    fn hash_optional_missing() {
6348        let data = r#"
6349version = 1
6350requires-python = ">=3.12"
6351
6352[[package]]
6353name = "anyio"
6354version = "4.3.0"
6355source = { registry = "https://pypi.org/simple" }
6356wheels = [{ url = "https://files.pythonhosted.org/packages/14/fd/2f20c40b45e4fb4324834aea24bd4afdf1143390242c0b33774da0e2e34f/anyio-4.3.0-py3-none-any.whl" }]
6357"#;
6358        let result: Result<Lock, _> = toml::from_str(data);
6359        insta::assert_debug_snapshot!(result);
6360    }
6361
6362    #[test]
6363    fn hash_optional_present() {
6364        let data = r#"
6365version = 1
6366requires-python = ">=3.12"
6367
6368[[package]]
6369name = "anyio"
6370version = "4.3.0"
6371source = { registry = "https://pypi.org/simple" }
6372wheels = [{ url = "https://files.pythonhosted.org/packages/14/fd/2f20c40b45e4fb4324834aea24bd4afdf1143390242c0b33774da0e2e34f/anyio-4.3.0-py3-none-any.whl", hash = "sha256:048e05d0f6caeed70d731f3db756d35dcc1f35747c8c403364a8332c630441b8" }]
6373"#;
6374        let result: Result<Lock, _> = toml::from_str(data);
6375        insta::assert_debug_snapshot!(result);
6376    }
6377
6378    #[test]
6379    fn hash_required_present() {
6380        let data = r#"
6381version = 1
6382requires-python = ">=3.12"
6383
6384[[package]]
6385name = "anyio"
6386version = "4.3.0"
6387source = { path = "file:///foo/bar" }
6388wheels = [{ url = "file:///foo/bar/anyio-4.3.0-py3-none-any.whl", hash = "sha256:048e05d0f6caeed70d731f3db756d35dcc1f35747c8c403364a8332c630441b8" }]
6389"#;
6390        let result: Result<Lock, _> = toml::from_str(data);
6391        insta::assert_debug_snapshot!(result);
6392    }
6393
6394    #[test]
6395    fn source_direct_no_subdir() {
6396        let data = r#"
6397version = 1
6398requires-python = ">=3.12"
6399
6400[[package]]
6401name = "anyio"
6402version = "4.3.0"
6403source = { url = "https://burntsushi.net" }
6404"#;
6405        let result: Result<Lock, _> = toml::from_str(data);
6406        insta::assert_debug_snapshot!(result);
6407    }
6408
6409    #[test]
6410    fn source_direct_has_subdir() {
6411        let data = r#"
6412version = 1
6413requires-python = ">=3.12"
6414
6415[[package]]
6416name = "anyio"
6417version = "4.3.0"
6418source = { url = "https://burntsushi.net", subdirectory = "wat/foo/bar" }
6419"#;
6420        let result: Result<Lock, _> = toml::from_str(data);
6421        insta::assert_debug_snapshot!(result);
6422    }
6423
6424    #[test]
6425    fn source_directory() {
6426        let data = r#"
6427version = 1
6428requires-python = ">=3.12"
6429
6430[[package]]
6431name = "anyio"
6432version = "4.3.0"
6433source = { directory = "path/to/dir" }
6434"#;
6435        let result: Result<Lock, _> = toml::from_str(data);
6436        insta::assert_debug_snapshot!(result);
6437    }
6438
6439    #[test]
6440    fn source_editable() {
6441        let data = r#"
6442version = 1
6443requires-python = ">=3.12"
6444
6445[[package]]
6446name = "anyio"
6447version = "4.3.0"
6448source = { editable = "path/to/dir" }
6449"#;
6450        let result: Result<Lock, _> = toml::from_str(data);
6451        insta::assert_debug_snapshot!(result);
6452    }
6453}