Skip to main content

uv_resolver/resolution/
output.rs

1use std::collections::BTreeMap;
2use std::fmt::{Display, Formatter};
3use std::sync::Arc;
4
5use indexmap::IndexSet;
6use petgraph::{
7    Directed, Direction,
8    graph::{Graph, NodeIndex},
9};
10use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
11
12use uv_configuration::{Constraints, Overrides};
13use uv_distribution::Metadata;
14use uv_distribution_types::{
15    Dist, DistributionId, Edge, Identifier, IndexUrl, Name, Node, Requirement, RequiresPython,
16    ResolutionDiagnostic, ResolvedDist,
17};
18use uv_git::GitResolver;
19use uv_normalize::{ExtraName, GroupName, PackageName};
20use uv_pep440::{Version, VersionSpecifier};
21use uv_pep508::{MarkerEnvironment, MarkerTree, MarkerTreeKind};
22use uv_pypi_types::{Conflicts, HashDigests, ParsedUrlError, VerbatimParsedUrl, Yanked};
23
24use crate::graph_ops::{marker_reachability, simplify_conflict_markers};
25use crate::pins::FilePins;
26use crate::preferences::Preferences;
27use crate::redirect::url_to_precise;
28use crate::resolution::AnnotatedDist;
29use crate::resolution_mode::ResolutionStrategy;
30use crate::resolver::{Resolution, ResolutionDependencyEdge, ResolutionPackage};
31use crate::universal_marker::{ConflictMarker, UniversalMarker};
32use crate::{
33    InMemoryIndex, MetadataResponse, Options, PythonRequirement, ResolveError, VersionsResponse,
34};
35
36/// The output of a successful resolution.
37///
38/// Includes a complete resolution graph in which every node represents a pinned package and every
39/// edge represents a dependency between two pinned packages.
40#[derive(Debug)]
41pub struct ResolverOutput {
42    /// The underlying graph.
43    pub(crate) graph: Graph<ResolutionGraphNode, UniversalMarker, Directed>,
44    /// The range of supported Python versions.
45    pub(crate) requires_python: RequiresPython,
46    /// If the resolution had non-identical forks, store the forks in the lockfile so we can
47    /// recreate them in subsequent resolutions.
48    pub(crate) fork_markers: Vec<UniversalMarker>,
49    /// Any diagnostics that were encountered while building the graph.
50    pub(crate) diagnostics: Vec<ResolutionDiagnostic>,
51    /// The requirements that were used to build the graph.
52    pub(crate) requirements: Vec<Requirement>,
53    /// The constraints that were used to build the graph.
54    pub(crate) constraints: Constraints,
55    /// The overrides that were used to build the graph.
56    pub(crate) overrides: Overrides,
57    /// The options that were used to build the graph.
58    pub(crate) options: Options,
59}
60
61#[derive(Debug, Clone)]
62#[expect(clippy::large_enum_variant)]
63pub(crate) enum ResolutionGraphNode {
64    Root,
65    Dist(AnnotatedDist),
66}
67
68impl ResolutionGraphNode {
69    pub(crate) fn marker(&self) -> &UniversalMarker {
70        match self {
71            Self::Root => &UniversalMarker::TRUE,
72            Self::Dist(dist) => &dist.marker,
73        }
74    }
75
76    pub(crate) fn package_extra_names(&self) -> Option<(&PackageName, &ExtraName)> {
77        match self {
78            Self::Root => None,
79            Self::Dist(dist) => {
80                let extra = dist.extra.as_ref()?;
81                Some((&dist.name, extra))
82            }
83        }
84    }
85
86    pub(crate) fn package_group_names(&self) -> Option<(&PackageName, &GroupName)> {
87        match self {
88            Self::Root => None,
89            Self::Dist(dist) => {
90                let group = dist.group.as_ref()?;
91                Some((&dist.name, group))
92            }
93        }
94    }
95
96    pub(crate) fn package_name(&self) -> Option<&PackageName> {
97        match self {
98            Self::Root => None,
99            Self::Dist(dist) => Some(&dist.name),
100        }
101    }
102}
103
104impl Display for ResolutionGraphNode {
105    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
106        match self {
107            Self::Root => f.write_str("root"),
108            Self::Dist(dist) => Display::fmt(dist, f),
109        }
110    }
111}
112
113#[derive(Debug, Eq, PartialEq, Hash)]
114struct PackageRef<'a> {
115    package_name: &'a PackageName,
116    version: &'a Version,
117    url: Option<&'a VerbatimParsedUrl>,
118    index: Option<&'a IndexUrl>,
119    extra: Option<&'a ExtraName>,
120    group: Option<&'a GroupName>,
121}
122
123impl ResolverOutput {
124    /// Create a new [`ResolverOutput`] from the resolved PubGrub state.
125    pub(crate) fn from_state(
126        resolutions: &[Resolution],
127        requirements: &[Requirement],
128        constraints: &Constraints,
129        overrides: &Overrides,
130        preferences: &Preferences,
131        index: &InMemoryIndex,
132        git: &GitResolver,
133        python: &PythonRequirement,
134        conflicts: &Conflicts,
135        resolution_strategy: &ResolutionStrategy,
136        options: Options,
137    ) -> Result<Self, ResolveError> {
138        let size_guess = resolutions[0].nodes.len();
139        let mut graph: Graph<ResolutionGraphNode, UniversalMarker, Directed> =
140            Graph::with_capacity(size_guess, size_guess);
141        let mut inverse: FxHashMap<PackageRef, NodeIndex<u32>> =
142            FxHashMap::with_capacity_and_hasher(size_guess, FxBuildHasher);
143        let mut diagnostics = Vec::new();
144
145        // Add the root node.
146        let root_index = graph.add_node(ResolutionGraphNode::Root);
147
148        let mut seen = FxHashSet::default();
149        for resolution in resolutions {
150            // Add every package to the graph.
151            for (package, version) in &resolution.nodes {
152                if !seen.insert((package, version)) {
153                    // Insert each node only once.
154                    continue;
155                }
156                Self::add_version(
157                    &mut graph,
158                    &mut inverse,
159                    &mut diagnostics,
160                    preferences,
161                    &resolution.pins,
162                    index,
163                    git,
164                    package,
165                    version,
166                )?;
167            }
168        }
169
170        let mut seen = FxHashSet::default();
171        for resolution in resolutions {
172            let marker = resolution.env.try_universal_markers().unwrap_or_default();
173
174            // Add every edge to the graph, propagating the marker for the current fork, if
175            // necessary.
176            for edge in &resolution.edges {
177                if !seen.insert((edge, marker)) {
178                    // Insert each node only once.
179                    continue;
180                }
181
182                Self::add_edge(&mut graph, &mut inverse, root_index, edge, marker);
183            }
184        }
185
186        // Extract the `Requires-Python` range, if provided.
187        let requires_python = python.target().clone();
188
189        let fork_markers: Vec<UniversalMarker> = if let [resolution] = resolutions {
190            // In the case of a singleton marker, we only include it if it's not
191            // always true. Otherwise, we keep our `fork_markers` empty as there
192            // are no forks.
193            resolution
194                .env
195                .try_universal_markers()
196                .into_iter()
197                .filter(|marker| !marker.is_true())
198                .collect()
199        } else {
200            resolutions
201                .iter()
202                .map(|resolution| resolution.env.try_universal_markers().unwrap_or_default())
203                .collect()
204        };
205
206        // Compute and apply the marker reachability.
207        let mut reachability = marker_reachability(&graph, &fork_markers);
208
209        // Apply the reachability to the graph and imbibe world
210        // knowledge about conflicts.
211        let conflict_marker = ConflictMarker::from_conflicts(conflicts);
212        for index in graph.node_indices() {
213            if let ResolutionGraphNode::Dist(dist) = &mut graph[index] {
214                dist.marker = reachability.remove(&index).unwrap_or_default();
215                dist.marker.imbibe(conflict_marker);
216            }
217        }
218        for weight in graph.edge_weights_mut() {
219            weight.imbibe(conflict_marker);
220        }
221
222        simplify_conflict_markers(conflicts, &mut graph);
223
224        // Discard any unreachable nodes.
225        graph.retain_nodes(|graph, node| !graph[node].marker().is_false());
226
227        if matches!(resolution_strategy, ResolutionStrategy::Lowest) {
228            report_missing_lower_bounds(&graph, &mut diagnostics, constraints, overrides);
229        }
230
231        let output = Self {
232            graph,
233            requires_python,
234            diagnostics,
235            requirements: requirements.to_vec(),
236            constraints: constraints.clone(),
237            overrides: overrides.clone(),
238            options,
239            fork_markers,
240        };
241
242        // We only do conflicting distribution detection when no
243        // conflicting groups have been specified. The reason here
244        // is that when there are conflicting groups, then from the
245        // perspective of marker expressions only, it may look like
246        // one can install different versions of the same package for
247        // the same marker environment. However, the thing preventing
248        // this is that the only way this should be possible is if
249        // one tries to install two or more conflicting extras at
250        // the same time. At which point, uv will report an error,
251        // thereby sidestepping the possibility of installing different
252        // versions of the same package into the same virtualenv. ---AG
253        //
254        // FIXME: When `UniversalMarker` supports extras/groups, we can
255        // re-enable this.
256        if conflicts.is_empty() {
257            #[allow(unused_mut, reason = "Used in debug_assertions below")]
258            let mut conflicting = output.find_conflicting_distributions();
259            if !conflicting.is_empty() {
260                tracing::warn!(
261                    "found {} conflicting distributions in resolution, \
262                 please report this as a bug at \
263                 https://github.com/astral-sh/uv/issues/new",
264                    conflicting.len()
265                );
266            }
267            // When testing, we materialize any conflicting distributions as an
268            // error to ensure any relevant tests fail. Otherwise, we just leave
269            // it at the warning message above. The reason for not returning an
270            // error "in production" is that an incorrect resolution may only be
271            // incorrect in certain marker environments, but fine in most others.
272            // Returning an error in that case would make `uv` unusable whenever
273            // the bug occurs, but letting it through means `uv` *could* still be
274            // usable.
275            #[cfg(debug_assertions)]
276            if let Some(err) = conflicting.pop() {
277                return Err(ResolveError::ConflictingDistribution(err));
278            }
279        }
280        Ok(output)
281    }
282
283    fn add_edge(
284        graph: &mut Graph<ResolutionGraphNode, UniversalMarker>,
285        inverse: &mut FxHashMap<PackageRef<'_>, NodeIndex>,
286        root_index: NodeIndex,
287        edge: &ResolutionDependencyEdge,
288        marker: UniversalMarker,
289    ) {
290        let from_index = edge.from.as_ref().map_or(root_index, |from| {
291            inverse[&PackageRef {
292                package_name: from,
293                version: &edge.from_version,
294                url: edge.from_url.as_ref(),
295                index: edge.from_index.as_ref(),
296                extra: edge.from_extra.as_ref(),
297                group: edge.from_group.as_ref(),
298            }]
299        });
300        let to_index = inverse[&PackageRef {
301            package_name: &edge.to,
302            version: &edge.to_version,
303            url: edge.to_url.as_ref(),
304            index: edge.to_index.as_ref(),
305            extra: edge.to_extra.as_ref(),
306            group: edge.to_group.as_ref(),
307        }];
308
309        let edge_marker = {
310            let mut edge_marker = edge.universal_marker();
311            edge_marker.and(marker);
312            edge_marker
313        };
314
315        if let Some(weight) = graph
316            .find_edge(from_index, to_index)
317            .and_then(|edge| graph.edge_weight_mut(edge))
318        {
319            // If either the existing marker or new marker is `true`, then the dependency is
320            // included unconditionally, and so the combined marker is `true`.
321            weight.or(edge_marker);
322        } else {
323            graph.update_edge(from_index, to_index, edge_marker);
324        }
325    }
326
327    fn add_version<'a>(
328        graph: &mut Graph<ResolutionGraphNode, UniversalMarker>,
329        inverse: &mut FxHashMap<PackageRef<'a>, NodeIndex>,
330        diagnostics: &mut Vec<ResolutionDiagnostic>,
331        preferences: &Preferences,
332        pins: &FilePins,
333        in_memory: &InMemoryIndex,
334        git: &GitResolver,
335        package: &'a ResolutionPackage,
336        version: &'a Version,
337    ) -> Result<(), ResolveError> {
338        let ResolutionPackage {
339            name,
340            extra,
341            dev: group,
342            url,
343            index,
344        } = &package;
345        // Map the package to a distribution.
346        let (dist, hashes, metadata) = Self::parse_dist(
347            name,
348            index.as_ref(),
349            url.as_ref(),
350            version,
351            pins,
352            diagnostics,
353            preferences,
354            in_memory,
355            git,
356        )?;
357
358        if let Some(metadata) = metadata.as_ref() {
359            // Validate the extra.
360            if let Some(extra) = extra {
361                if !metadata.provides_extra.contains(extra) {
362                    diagnostics.push(ResolutionDiagnostic::MissingExtra {
363                        dist: dist.clone(),
364                        extra: extra.clone(),
365                    });
366                }
367            }
368
369            // Validate the development dependency group.
370            if let Some(dev) = group {
371                if !metadata.dependency_groups.contains_key(dev) {
372                    diagnostics.push(ResolutionDiagnostic::MissingGroup {
373                        dist: dist.clone(),
374                        group: dev.clone(),
375                    });
376                }
377            }
378        }
379
380        // Add the distribution to the graph.
381        let node = graph.add_node(ResolutionGraphNode::Dist(AnnotatedDist {
382            dist,
383            name: name.clone(),
384            version: version.clone(),
385            extra: extra.clone(),
386            group: group.clone(),
387            hashes,
388            metadata,
389            marker: UniversalMarker::TRUE,
390        }));
391        inverse.insert(
392            PackageRef {
393                package_name: name,
394                version,
395                url: url.as_ref(),
396                index: index.as_ref(),
397                extra: extra.as_ref(),
398                group: group.as_ref(),
399            },
400            node,
401        );
402        Ok(())
403    }
404
405    fn parse_dist(
406        name: &PackageName,
407        index: Option<&IndexUrl>,
408        url: Option<&VerbatimParsedUrl>,
409        version: &Version,
410        pins: &FilePins,
411        diagnostics: &mut Vec<ResolutionDiagnostic>,
412        preferences: &Preferences,
413        in_memory: &InMemoryIndex,
414        git: &GitResolver,
415    ) -> Result<(ResolvedDist, HashDigests, Option<Metadata>), ResolveError> {
416        Ok(if let Some(url) = url {
417            // Create the locked distribution and recover the metadata using the original URL that
418            // was requested during resolution.
419            let dist = Dist::from_url(name.clone(), url_to_precise(url.clone(), git))?;
420            let hashes_id = dist.distribution_id();
421            let metadata_id = Dist::from_url(name.clone(), url.clone())?.distribution_id();
422
423            // Extract the hashes.
424            let hashes = Self::get_hashes(
425                name,
426                index,
427                Some(url),
428                &hashes_id,
429                version,
430                preferences,
431                in_memory,
432            );
433
434            // Extract the metadata.
435            let metadata = {
436                let response = in_memory
437                    .distributions()
438                    .get(&metadata_id)
439                    .unwrap_or_else(|| {
440                        panic!("Every URL distribution should have metadata: {metadata_id:?}")
441                    });
442
443                let MetadataResponse::Found(archive) = &*response else {
444                    panic!("Every URL distribution should have metadata: {metadata_id:?}")
445                };
446
447                archive.metadata.clone()
448            };
449
450            (
451                ResolvedDist::Installable {
452                    dist: Arc::new(dist),
453                    version: Some(version.clone()),
454                },
455                hashes,
456                Some(metadata),
457            )
458        } else {
459            let dist = pins
460                .get(name, version)
461                .expect("Every package should be pinned")
462                .clone();
463            let hashes_id = dist.distribution_id();
464            let metadata_id = pins
465                .metadata_id(name, version)
466                .expect("Every package should have pinned metadata");
467
468            // Track yanks for any registry distributions.
469            match dist.yanked() {
470                None | Some(Yanked::Bool(false)) => {}
471                Some(Yanked::Bool(true)) => {
472                    diagnostics.push(ResolutionDiagnostic::YankedVersion {
473                        dist: dist.clone(),
474                        reason: None,
475                    });
476                }
477                Some(Yanked::Reason(reason)) => {
478                    diagnostics.push(ResolutionDiagnostic::YankedVersion {
479                        dist: dist.clone(),
480                        reason: Some(reason.to_string()),
481                    });
482                }
483            }
484
485            // Extract the hashes.
486            let hashes = Self::get_hashes(
487                name,
488                index,
489                None,
490                &hashes_id,
491                version,
492                preferences,
493                in_memory,
494            );
495
496            // Extract the metadata.
497            let metadata = {
498                in_memory
499                    .distributions()
500                    .get(metadata_id)
501                    .and_then(|response| {
502                        if let MetadataResponse::Found(archive) = &*response {
503                            Some(archive.metadata.clone())
504                        } else {
505                            None
506                        }
507                    })
508            };
509
510            (dist, hashes, metadata)
511        })
512    }
513
514    /// Identify the hashes for a concrete distribution, preserving any hashes that were provided
515    /// by the lockfile.
516    fn get_hashes(
517        name: &PackageName,
518        index: Option<&IndexUrl>,
519        url: Option<&VerbatimParsedUrl>,
520        metadata_id: &DistributionId,
521        version: &Version,
522        preferences: &Preferences,
523        in_memory: &InMemoryIndex,
524    ) -> HashDigests {
525        // 1. Look for hashes from the lockfile.
526        if let Some(digests) = preferences.match_hashes(name, version) {
527            if !digests.is_empty() {
528                return HashDigests::from(digests);
529            }
530        }
531
532        // 2. Look for hashes for the distribution (i.e., the specific wheel or source distribution).
533        if let Some(metadata_response) = in_memory.distributions().get(metadata_id) {
534            if let MetadataResponse::Found(ref archive) = *metadata_response {
535                let mut digests = archive.hashes.clone();
536                digests.sort_unstable();
537                if !digests.is_empty() {
538                    return digests;
539                }
540            }
541        }
542
543        // 3. Look for hashes from the registry, which are served at the package level.
544        if url.is_none() {
545            // Query the implicit and explicit indexes (lazily) for the hashes.
546            let implicit_response = in_memory.implicit().get(name);
547            let mut explicit_response = None;
548
549            // Search in the implicit indexes.
550            let hashes = implicit_response
551                .as_ref()
552                .and_then(|response| {
553                    if let VersionsResponse::Found(version_maps) = &**response {
554                        Some(version_maps)
555                    } else {
556                        None
557                    }
558                })
559                .into_iter()
560                .flatten()
561                .filter(|version_map| version_map.index() == index)
562                .find_map(|version_map| version_map.hashes(version))
563                .or_else(|| {
564                    // Search in the explicit indexes.
565                    explicit_response = index
566                        .and_then(|index| in_memory.explicit().get(&(name.clone(), index.clone())));
567                    explicit_response
568                        .as_ref()
569                        .and_then(|response| {
570                            if let VersionsResponse::Found(version_maps) = &**response {
571                                Some(version_maps)
572                            } else {
573                                None
574                            }
575                        })
576                        .into_iter()
577                        .flatten()
578                        .filter(|version_map| version_map.index() == index)
579                        .find_map(|version_map| version_map.hashes(version))
580                });
581
582            if let Some(hashes) = hashes {
583                let mut digests = HashDigests::from(hashes);
584                digests.sort_unstable();
585                if !digests.is_empty() {
586                    return digests;
587                }
588            }
589        }
590
591        HashDigests::empty()
592    }
593
594    /// Returns an iterator over the distinct packages in the graph.
595    fn dists(&self) -> impl Iterator<Item = &AnnotatedDist> {
596        self.graph
597            .node_indices()
598            .filter_map(move |index| match &self.graph[index] {
599                ResolutionGraphNode::Root => None,
600                ResolutionGraphNode::Dist(dist) => Some(dist),
601            })
602    }
603
604    /// Return the number of distinct packages in the graph.
605    pub fn len(&self) -> usize {
606        self.dists().filter(|dist| dist.is_base()).count()
607    }
608
609    /// Return `true` if there are no packages in the graph.
610    pub fn is_empty(&self) -> bool {
611        self.dists().any(AnnotatedDist::is_base)
612    }
613
614    /// Returns `true` if the graph contains the given package.
615    pub fn contains(&self, name: &PackageName) -> bool {
616        self.dists().any(|dist| dist.name() == name)
617    }
618
619    /// Return the [`ResolutionDiagnostic`]s that were encountered while building the graph.
620    pub fn diagnostics(&self) -> &[ResolutionDiagnostic] {
621        &self.diagnostics
622    }
623
624    /// Return the marker tree specific to this resolution.
625    ///
626    /// This accepts an in-memory-index and marker environment, all
627    /// of which should be the same values given to the resolver that produced
628    /// this graph.
629    ///
630    /// The marker tree returned corresponds to an expression that, when true,
631    /// this resolution is guaranteed to be correct. Note though that it's
632    /// possible for resolution to be correct even if the returned marker
633    /// expression is false.
634    ///
635    /// For example, if the root package has a dependency `foo; sys_platform ==
636    /// "macos"` and resolution was performed on Linux, then the marker tree
637    /// returned will contain a `sys_platform == "linux"` expression. This
638    /// means that whenever the marker expression evaluates to true (i.e., the
639    /// current platform is Linux), then the resolution here is correct. But
640    /// it is possible that the resolution is also correct on other platforms
641    /// that aren't macOS, such as Windows. (It is unclear at time of writing
642    /// whether this is fundamentally impossible to compute, or just impossible
643    /// to compute in some cases.)
644    pub fn marker_tree(
645        &self,
646        index: &InMemoryIndex,
647        marker_env: &MarkerEnvironment,
648    ) -> Result<MarkerTree, Box<ParsedUrlError>> {
649        use uv_pep508::{
650            CanonicalMarkerValueString, CanonicalMarkerValueVersion, MarkerExpression,
651            MarkerOperator, MarkerTree,
652        };
653
654        /// A subset of the possible marker values.
655        ///
656        /// We only track the marker parameters that are referenced in a marker
657        /// expression. We'll use references to the parameter later to generate
658        /// values based on the current marker environment.
659        #[derive(Debug, Eq, Hash, PartialEq)]
660        enum MarkerParam {
661            Version(CanonicalMarkerValueVersion),
662            String(CanonicalMarkerValueString),
663        }
664
665        /// Add all marker parameters from the given tree to the given set.
666        fn add_marker_params_from_tree(marker_tree: MarkerTree, set: &mut IndexSet<MarkerParam>) {
667            match marker_tree.kind() {
668                MarkerTreeKind::True => {}
669                MarkerTreeKind::False => {}
670                MarkerTreeKind::Version(marker) => {
671                    set.insert(MarkerParam::Version(marker.key()));
672                    for (_, tree) in marker.edges() {
673                        add_marker_params_from_tree(tree, set);
674                    }
675                }
676                MarkerTreeKind::String(marker) => {
677                    set.insert(MarkerParam::String(marker.key()));
678                    for (_, tree) in marker.children() {
679                        add_marker_params_from_tree(tree, set);
680                    }
681                }
682                MarkerTreeKind::In(marker) => {
683                    set.insert(MarkerParam::String(marker.key()));
684                    for (_, tree) in marker.children() {
685                        add_marker_params_from_tree(tree, set);
686                    }
687                }
688                MarkerTreeKind::Contains(marker) => {
689                    set.insert(MarkerParam::String(marker.key()));
690                    for (_, tree) in marker.children() {
691                        add_marker_params_from_tree(tree, set);
692                    }
693                }
694                // We specifically don't care about these for the
695                // purposes of generating a marker string for a lock
696                // file. Quoted strings are marker values given by the
697                // user. We don't track those here, since we're only
698                // interested in which markers are used.
699                MarkerTreeKind::Extra(marker) => {
700                    for (_, tree) in marker.children() {
701                        add_marker_params_from_tree(tree, set);
702                    }
703                }
704                MarkerTreeKind::List(marker) => {
705                    for (_, tree) in marker.children() {
706                        add_marker_params_from_tree(tree, set);
707                    }
708                }
709            }
710        }
711
712        let mut seen_marker_values = IndexSet::default();
713        for i in self.graph.node_indices() {
714            let ResolutionGraphNode::Dist(dist) = &self.graph[i] else {
715                continue;
716            };
717            let metadata_id = dist.dist.distribution_id();
718            let res = index
719                .distributions()
720                .get(&metadata_id)
721                .expect("every package in resolution graph has metadata");
722            let MetadataResponse::Found(archive, ..) = &*res else {
723                panic!("Every package should have metadata: {metadata_id:?}")
724            };
725            for req in self
726                .constraints
727                .apply(self.overrides.apply(archive.metadata.requires_dist.iter()))
728            {
729                add_marker_params_from_tree(req.marker, &mut seen_marker_values);
730            }
731        }
732
733        // Ensure that we consider markers from direct dependencies.
734        for direct_req in self
735            .constraints
736            .apply(self.overrides.apply(self.requirements.iter()))
737        {
738            add_marker_params_from_tree(direct_req.marker, &mut seen_marker_values);
739        }
740
741        // Generate the final marker expression as a conjunction of
742        // strict equality terms.
743        let mut conjunction = MarkerTree::TRUE;
744        for marker_param in seen_marker_values {
745            let expr = match marker_param {
746                MarkerParam::Version(value_version) => {
747                    let from_env = marker_env.get_version(value_version);
748                    MarkerExpression::Version {
749                        key: value_version.into(),
750                        specifier: VersionSpecifier::equals_version(from_env.clone()),
751                    }
752                }
753                MarkerParam::String(value_string) => {
754                    let from_env = marker_env.get_string(value_string);
755                    MarkerExpression::String {
756                        key: value_string.into(),
757                        operator: MarkerOperator::Equal,
758                        value: from_env.into(),
759                    }
760                }
761            };
762            conjunction.and(MarkerTree::expression(expr));
763        }
764        Ok(conjunction)
765    }
766
767    /// Returns a sequence of conflicting distribution errors from this
768    /// resolution.
769    ///
770    /// Correct resolutions always return an empty sequence. A non-empty
771    /// sequence implies there is a package with two distinct versions in the
772    /// same marker environment in this resolution. This in turn implies that
773    /// an installation in that marker environment could wind up trying to
774    /// install different versions of the same package, which is not allowed.
775    fn find_conflicting_distributions(&self) -> Vec<ConflictingDistributionError> {
776        let mut name_to_markers: BTreeMap<&PackageName, Vec<(&Version, &UniversalMarker)>> =
777            BTreeMap::new();
778        for node in self.graph.node_weights() {
779            let annotated_dist = match node {
780                ResolutionGraphNode::Root => continue,
781                ResolutionGraphNode::Dist(annotated_dist) => annotated_dist,
782            };
783            name_to_markers
784                .entry(&annotated_dist.name)
785                .or_default()
786                .push((&annotated_dist.version, &annotated_dist.marker));
787        }
788        let mut dupes = vec![];
789        for (name, marker_trees) in name_to_markers {
790            for (i, (version1, marker1)) in marker_trees.iter().enumerate() {
791                for (version2, marker2) in &marker_trees[i + 1..] {
792                    if version1 == version2 {
793                        continue;
794                    }
795                    if !marker1.is_disjoint(**marker2) {
796                        dupes.push(ConflictingDistributionError {
797                            name: name.clone(),
798                            version1: (*version1).clone(),
799                            version2: (*version2).clone(),
800                            marker1: **marker1,
801                            marker2: **marker2,
802                        });
803                    }
804                }
805            }
806        }
807        dupes
808    }
809}
810
811/// An error that occurs for conflicting versions of the same package.
812///
813/// Specifically, this occurs when two distributions with the same package
814/// name are found with distinct versions in at least one possible marker
815/// environment. This error reflects an error that could occur when installing
816/// the corresponding resolution into that marker environment.
817#[derive(Debug)]
818pub struct ConflictingDistributionError {
819    name: PackageName,
820    version1: Version,
821    version2: Version,
822    marker1: UniversalMarker,
823    marker2: UniversalMarker,
824}
825
826impl std::error::Error for ConflictingDistributionError {}
827
828impl Display for ConflictingDistributionError {
829    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
830        let Self {
831            ref name,
832            ref version1,
833            ref version2,
834            ref marker1,
835            ref marker2,
836        } = *self;
837        write!(
838            f,
839            "found conflicting versions for package `{name}`:
840             `{marker1:?}` (for version `{version1}`) is not disjoint with \
841             `{marker2:?}` (for version `{version2}`)",
842        )
843    }
844}
845
846/// Convert a [`ResolverOutput`] into a [`uv_distribution_types::Resolution`].
847///
848/// This involves converting [`ResolutionGraphNode`]s into [`Node`]s, which in turn involves
849/// dropping any extras and dependency groups from the graph nodes. Instead, each package is
850/// collapsed into a single node, with  extras and dependency groups annotating the _edges_, rather
851/// than being represented as separate nodes. This is a more natural representation, but a further
852/// departure from the PubGrub model.
853///
854/// For simplicity, this transformation makes the assumption that the resolution only applies to a
855/// subset of markers, i.e., it shouldn't be called on universal resolutions, and expects only a
856/// single version of each package to be present in the graph.
857impl From<ResolverOutput> for uv_distribution_types::Resolution {
858    fn from(output: ResolverOutput) -> Self {
859        let ResolverOutput {
860            graph,
861            diagnostics,
862            fork_markers,
863            ..
864        } = output;
865
866        assert!(
867            fork_markers.is_empty(),
868            "universal resolutions are not supported"
869        );
870
871        let mut transformed = Graph::with_capacity(graph.node_count(), graph.edge_count());
872        let mut inverse = FxHashMap::with_capacity_and_hasher(graph.node_count(), FxBuildHasher);
873
874        // Create the root node.
875        let root = transformed.add_node(Node::Root);
876
877        // Re-add the nodes to the reduced graph.
878        for index in graph.node_indices() {
879            let ResolutionGraphNode::Dist(dist) = &graph[index] else {
880                continue;
881            };
882            if dist.is_base() {
883                inverse.insert(
884                    &dist.name,
885                    transformed.add_node(Node::Dist {
886                        dist: dist.dist.clone(),
887                        hashes: dist.hashes.clone(),
888                        install: true,
889                    }),
890                );
891            }
892        }
893
894        // Re-add the edges to the reduced graph.
895        for edge in graph.edge_indices() {
896            let (source, target) = graph.edge_endpoints(edge).unwrap();
897
898            match (&graph[source], &graph[target]) {
899                (ResolutionGraphNode::Root, ResolutionGraphNode::Dist(target_dist)) => {
900                    let target = inverse[&target_dist.name()];
901                    transformed.update_edge(root, target, Edge::Prod);
902                }
903                (
904                    ResolutionGraphNode::Dist(source_dist),
905                    ResolutionGraphNode::Dist(target_dist),
906                ) => {
907                    let source = inverse[&source_dist.name()];
908                    let target = inverse[&target_dist.name()];
909
910                    let edge = if let Some(extra) = source_dist.extra.as_ref() {
911                        Edge::Optional(extra.clone())
912                    } else if let Some(group) = source_dist.group.as_ref() {
913                        Edge::Dev(group.clone())
914                    } else {
915                        Edge::Prod
916                    };
917
918                    transformed.add_edge(source, target, edge);
919                }
920                _ => {
921                    unreachable!("root should not contain incoming edges");
922                }
923            }
924        }
925
926        Self::new(transformed).with_diagnostics(diagnostics)
927    }
928}
929
930/// Find any packages that don't have any lower bound on them when in resolution-lowest mode.
931fn report_missing_lower_bounds(
932    graph: &Graph<ResolutionGraphNode, UniversalMarker>,
933    diagnostics: &mut Vec<ResolutionDiagnostic>,
934    constraints: &Constraints,
935    overrides: &Overrides,
936) {
937    for node_index in graph.node_indices() {
938        let ResolutionGraphNode::Dist(dist) = graph.node_weight(node_index).unwrap() else {
939            // Ignore the root package.
940            continue;
941        };
942        if !has_lower_bound(node_index, dist.name(), graph, constraints, overrides) {
943            diagnostics.push(ResolutionDiagnostic::MissingLowerBound {
944                package_name: dist.name().clone(),
945            });
946        }
947    }
948}
949
950/// Whether the given package has a lower version bound by another package.
951fn has_lower_bound(
952    node_index: NodeIndex,
953    package_name: &PackageName,
954    graph: &Graph<ResolutionGraphNode, UniversalMarker>,
955    constraints: &Constraints,
956    overrides: &Overrides,
957) -> bool {
958    for neighbor_index in graph.neighbors_directed(node_index, Direction::Incoming) {
959        let neighbor_dist = match graph.node_weight(neighbor_index).unwrap() {
960            ResolutionGraphNode::Root => {
961                // We already handled direct dependencies with a missing constraint
962                // separately.
963                return true;
964            }
965            ResolutionGraphNode::Dist(neighbor_dist) => neighbor_dist,
966        };
967
968        if neighbor_dist.name() == package_name {
969            // Only warn for real packages, not for virtual packages such as dev nodes.
970            return true;
971        }
972
973        let Some(metadata) = neighbor_dist.metadata.as_ref() else {
974            // We can't check for lower bounds if we lack metadata.
975            return true;
976        };
977
978        // Get all individual specifier for the current package and check if any has a lower
979        // bound.
980        for requirement in metadata
981            .requires_dist
982            .iter()
983            // These bounds sources are missing from the graph.
984            .chain(metadata.dependency_groups.values().flatten())
985            .chain(constraints.requirements())
986            .chain(overrides.requirements())
987        {
988            if requirement.name != *package_name {
989                continue;
990            }
991            let Some(specifiers) = requirement.source.version_specifiers() else {
992                // URL requirements are a bound.
993                return true;
994            };
995            if specifiers.iter().any(VersionSpecifier::has_lower_bound) {
996                return true;
997            }
998        }
999    }
1000    false
1001}