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