Skip to main content

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