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