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 metadata_id = Dist::from_url(name.clone(), url.clone())?.distribution_id();
421
422            // Extract the hashes.
423            let hashes = Self::get_hashes(
424                name,
425                index,
426                Some(url),
427                &metadata_id,
428                version,
429                preferences,
430                in_memory,
431            );
432
433            // Extract the metadata.
434            let metadata = {
435                let response = in_memory
436                    .distributions()
437                    .get(&metadata_id)
438                    .unwrap_or_else(|| {
439                        panic!("Every URL distribution should have metadata: {metadata_id:?}")
440                    });
441
442                let MetadataResponse::Found(archive) = &*response else {
443                    panic!("Every URL distribution should have metadata: {metadata_id:?}")
444                };
445
446                archive.metadata.clone()
447            };
448
449            (
450                ResolvedDist::Installable {
451                    dist: Arc::new(dist),
452                    version: Some(version.clone()),
453                },
454                hashes,
455                Some(metadata),
456            )
457        } else {
458            let (dist, metadata_id) = pins
459                .dist_and_id(name, version)
460                .expect("Every package should be pinned");
461            let dist = dist.clone();
462            let hashes_id = dist.distribution_id();
463
464            // Track yanks for any registry distributions.
465            match dist.yanked() {
466                None | Some(Yanked::Bool(false)) => {}
467                Some(Yanked::Bool(true)) => {
468                    diagnostics.push(ResolutionDiagnostic::YankedVersion {
469                        dist: dist.clone(),
470                        reason: None,
471                    });
472                }
473                Some(Yanked::Reason(reason)) => {
474                    diagnostics.push(ResolutionDiagnostic::YankedVersion {
475                        dist: dist.clone(),
476                        reason: Some(reason.to_string()),
477                    });
478                }
479            }
480
481            // Extract the hashes.
482            let hashes = Self::get_hashes(
483                name,
484                index,
485                None,
486                &hashes_id,
487                version,
488                preferences,
489                in_memory,
490            );
491
492            // Extract the metadata.
493            let metadata = {
494                in_memory
495                    .distributions()
496                    .get(metadata_id)
497                    .and_then(|response| {
498                        if let MetadataResponse::Found(archive) = &*response {
499                            Some(archive.metadata.clone())
500                        } else {
501                            None
502                        }
503                    })
504            };
505
506            (dist, hashes, metadata)
507        })
508    }
509
510    /// Identify the hashes for a concrete distribution, preserving any hashes that were provided
511    /// by the lockfile.
512    fn get_hashes(
513        name: &PackageName,
514        index: Option<&IndexUrl>,
515        url: Option<&VerbatimParsedUrl>,
516        metadata_id: &DistributionId,
517        version: &Version,
518        preferences: &Preferences,
519        in_memory: &InMemoryIndex,
520    ) -> HashDigests {
521        // 1. Look for hashes from the lockfile.
522        if let Some(digests) = preferences.match_hashes(name, version) {
523            if !digests.is_empty() {
524                return HashDigests::from(digests);
525            }
526        }
527
528        // 2. Look for hashes for the distribution (i.e., the specific wheel or source distribution).
529        if let Some(metadata_response) = in_memory.distributions().get(metadata_id) {
530            if let MetadataResponse::Found(ref archive) = *metadata_response {
531                let mut digests = archive.hashes.clone();
532                digests.sort_unstable();
533                if !digests.is_empty() {
534                    return digests;
535                }
536            }
537        }
538
539        // 3. Look for hashes from the registry, which are served at the package level.
540        if url.is_none() {
541            // Query the implicit and explicit indexes (lazily) for the hashes.
542            let implicit_response = in_memory.implicit().get(name);
543            let mut explicit_response = None;
544
545            // Search in the implicit indexes.
546            let hashes = implicit_response
547                .as_ref()
548                .and_then(|response| {
549                    if let VersionsResponse::Found(version_maps) = &**response {
550                        Some(version_maps)
551                    } else {
552                        None
553                    }
554                })
555                .into_iter()
556                .flatten()
557                .filter(|version_map| version_map.index() == index)
558                .find_map(|version_map| version_map.hashes(version))
559                .or_else(|| {
560                    // Search in the explicit indexes.
561                    explicit_response = index
562                        .and_then(|index| in_memory.explicit().get(&(name.clone(), index.clone())));
563                    explicit_response
564                        .as_ref()
565                        .and_then(|response| {
566                            if let VersionsResponse::Found(version_maps) = &**response {
567                                Some(version_maps)
568                            } else {
569                                None
570                            }
571                        })
572                        .into_iter()
573                        .flatten()
574                        .filter(|version_map| version_map.index() == index)
575                        .find_map(|version_map| version_map.hashes(version))
576                });
577
578            if let Some(hashes) = hashes {
579                let mut digests = HashDigests::from(hashes);
580                digests.sort_unstable();
581                if !digests.is_empty() {
582                    return digests;
583                }
584            }
585        }
586
587        HashDigests::empty()
588    }
589
590    /// Returns an iterator over the distinct packages in the graph.
591    fn dists(&self) -> impl Iterator<Item = &AnnotatedDist> {
592        self.graph
593            .node_indices()
594            .filter_map(move |index| match &self.graph[index] {
595                ResolutionGraphNode::Root => None,
596                ResolutionGraphNode::Dist(dist) => Some(dist),
597            })
598    }
599
600    /// Return the number of distinct packages in the graph.
601    pub fn len(&self) -> usize {
602        self.dists().filter(|dist| dist.is_base()).count()
603    }
604
605    /// Return `true` if there are no packages in the graph.
606    pub fn is_empty(&self) -> bool {
607        self.dists().any(AnnotatedDist::is_base)
608    }
609
610    /// Returns `true` if the graph contains the given package.
611    pub fn contains(&self, name: &PackageName) -> bool {
612        self.dists().any(|dist| dist.name() == name)
613    }
614
615    /// Return the [`ResolutionDiagnostic`]s that were encountered while building the graph.
616    pub fn diagnostics(&self) -> &[ResolutionDiagnostic] {
617        &self.diagnostics
618    }
619
620    /// Return the marker tree specific to this resolution.
621    ///
622    /// This accepts an in-memory-index and marker environment, all
623    /// of which should be the same values given to the resolver that produced
624    /// this graph.
625    ///
626    /// The marker tree returned corresponds to an expression that, when true,
627    /// this resolution is guaranteed to be correct. Note though that it's
628    /// possible for resolution to be correct even if the returned marker
629    /// expression is false.
630    ///
631    /// For example, if the root package has a dependency `foo; sys_platform ==
632    /// "macos"` and resolution was performed on Linux, then the marker tree
633    /// returned will contain a `sys_platform == "linux"` expression. This
634    /// means that whenever the marker expression evaluates to true (i.e., the
635    /// current platform is Linux), then the resolution here is correct. But
636    /// it is possible that the resolution is also correct on other platforms
637    /// that aren't macOS, such as Windows. (It is unclear at time of writing
638    /// whether this is fundamentally impossible to compute, or just impossible
639    /// to compute in some cases.)
640    pub fn marker_tree(
641        &self,
642        index: &InMemoryIndex,
643        marker_env: &MarkerEnvironment,
644    ) -> Result<MarkerTree, Box<ParsedUrlError>> {
645        use uv_pep508::{
646            CanonicalMarkerValueString, CanonicalMarkerValueVersion, MarkerExpression,
647            MarkerOperator, MarkerTree,
648        };
649
650        /// A subset of the possible marker values.
651        ///
652        /// We only track the marker parameters that are referenced in a marker
653        /// expression. We'll use references to the parameter later to generate
654        /// values based on the current marker environment.
655        #[derive(Debug, Eq, Hash, PartialEq)]
656        enum MarkerParam {
657            Version(CanonicalMarkerValueVersion),
658            String(CanonicalMarkerValueString),
659        }
660
661        /// Add all marker parameters from the given tree to the given set.
662        fn add_marker_params_from_tree(marker_tree: MarkerTree, set: &mut IndexSet<MarkerParam>) {
663            match marker_tree.kind() {
664                MarkerTreeKind::True => {}
665                MarkerTreeKind::False => {}
666                MarkerTreeKind::Version(marker) => {
667                    set.insert(MarkerParam::Version(marker.key()));
668                    for (_, tree) in marker.edges() {
669                        add_marker_params_from_tree(tree, set);
670                    }
671                }
672                MarkerTreeKind::String(marker) => {
673                    set.insert(MarkerParam::String(marker.key()));
674                    for (_, tree) in marker.children() {
675                        add_marker_params_from_tree(tree, set);
676                    }
677                }
678                MarkerTreeKind::In(marker) => {
679                    set.insert(MarkerParam::String(marker.key()));
680                    for (_, tree) in marker.children() {
681                        add_marker_params_from_tree(tree, set);
682                    }
683                }
684                MarkerTreeKind::Contains(marker) => {
685                    set.insert(MarkerParam::String(marker.key()));
686                    for (_, tree) in marker.children() {
687                        add_marker_params_from_tree(tree, set);
688                    }
689                }
690                // We specifically don't care about these for the
691                // purposes of generating a marker string for a lock
692                // file. Quoted strings are marker values given by the
693                // user. We don't track those here, since we're only
694                // interested in which markers are used.
695                MarkerTreeKind::Extra(marker) => {
696                    for (_, tree) in marker.children() {
697                        add_marker_params_from_tree(tree, set);
698                    }
699                }
700                MarkerTreeKind::List(marker) => {
701                    for (_, tree) in marker.children() {
702                        add_marker_params_from_tree(tree, set);
703                    }
704                }
705            }
706        }
707
708        let mut seen_marker_values = IndexSet::default();
709        for i in self.graph.node_indices() {
710            let ResolutionGraphNode::Dist(dist) = &self.graph[i] else {
711                continue;
712            };
713            let metadata_id = dist.dist.distribution_id();
714            let res = index
715                .distributions()
716                .get(&metadata_id)
717                .expect("every package in resolution graph has metadata");
718            let MetadataResponse::Found(archive, ..) = &*res else {
719                panic!("Every package should have metadata: {metadata_id:?}")
720            };
721            for req in self
722                .constraints
723                .apply(self.overrides.apply(archive.metadata.requires_dist.iter()))
724            {
725                add_marker_params_from_tree(req.marker, &mut seen_marker_values);
726            }
727        }
728
729        // Ensure that we consider markers from direct dependencies.
730        for direct_req in self
731            .constraints
732            .apply(self.overrides.apply(self.requirements.iter()))
733        {
734            add_marker_params_from_tree(direct_req.marker, &mut seen_marker_values);
735        }
736
737        // Generate the final marker expression as a conjunction of
738        // strict equality terms.
739        let mut conjunction = MarkerTree::TRUE;
740        for marker_param in seen_marker_values {
741            let expr = match marker_param {
742                MarkerParam::Version(value_version) => {
743                    let from_env = marker_env.get_version(value_version);
744                    MarkerExpression::Version {
745                        key: value_version.into(),
746                        specifier: VersionSpecifier::equals_version(from_env.clone()),
747                    }
748                }
749                MarkerParam::String(value_string) => {
750                    let from_env = marker_env.get_string(value_string);
751                    MarkerExpression::String {
752                        key: value_string.into(),
753                        operator: MarkerOperator::Equal,
754                        value: from_env.into(),
755                    }
756                }
757            };
758            conjunction.and(MarkerTree::expression(expr));
759        }
760        Ok(conjunction)
761    }
762
763    /// Returns a sequence of conflicting distribution errors from this
764    /// resolution.
765    ///
766    /// Correct resolutions always return an empty sequence. A non-empty
767    /// sequence implies there is a package with two distinct versions in the
768    /// same marker environment in this resolution. This in turn implies that
769    /// an installation in that marker environment could wind up trying to
770    /// install different versions of the same package, which is not allowed.
771    fn find_conflicting_distributions(&self) -> Vec<ConflictingDistributionError> {
772        let mut name_to_markers: BTreeMap<&PackageName, Vec<(&Version, &UniversalMarker)>> =
773            BTreeMap::new();
774        for node in self.graph.node_weights() {
775            let annotated_dist = match node {
776                ResolutionGraphNode::Root => continue,
777                ResolutionGraphNode::Dist(annotated_dist) => annotated_dist,
778            };
779            name_to_markers
780                .entry(&annotated_dist.name)
781                .or_default()
782                .push((&annotated_dist.version, &annotated_dist.marker));
783        }
784        let mut dupes = vec![];
785        for (name, marker_trees) in name_to_markers {
786            for (i, (version1, marker1)) in marker_trees.iter().enumerate() {
787                for (version2, marker2) in &marker_trees[i + 1..] {
788                    if version1 == version2 {
789                        continue;
790                    }
791                    if !marker1.is_disjoint(**marker2) {
792                        dupes.push(ConflictingDistributionError {
793                            name: name.clone(),
794                            version1: (*version1).clone(),
795                            version2: (*version2).clone(),
796                            marker1: **marker1,
797                            marker2: **marker2,
798                        });
799                    }
800                }
801            }
802        }
803        dupes
804    }
805}
806
807/// An error that occurs for conflicting versions of the same package.
808///
809/// Specifically, this occurs when two distributions with the same package
810/// name are found with distinct versions in at least one possible marker
811/// environment. This error reflects an error that could occur when installing
812/// the corresponding resolution into that marker environment.
813#[derive(Debug)]
814pub struct ConflictingDistributionError {
815    name: PackageName,
816    version1: Version,
817    version2: Version,
818    marker1: UniversalMarker,
819    marker2: UniversalMarker,
820}
821
822impl std::error::Error for ConflictingDistributionError {}
823
824impl Display for ConflictingDistributionError {
825    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
826        let Self {
827            ref name,
828            ref version1,
829            ref version2,
830            ref marker1,
831            ref marker2,
832        } = *self;
833        write!(
834            f,
835            "found conflicting versions for package `{name}`:
836             `{marker1:?}` (for version `{version1}`) is not disjoint with \
837             `{marker2:?}` (for version `{version2}`)",
838        )
839    }
840}
841
842/// Convert a [`ResolverOutput`] into a [`uv_distribution_types::Resolution`].
843///
844/// This involves converting [`ResolutionGraphNode`]s into [`Node`]s, which in turn involves
845/// dropping any extras and dependency groups from the graph nodes. Instead, each package is
846/// collapsed into a single node, with  extras and dependency groups annotating the _edges_, rather
847/// than being represented as separate nodes. This is a more natural representation, but a further
848/// departure from the PubGrub model.
849///
850/// For simplicity, this transformation makes the assumption that the resolution only applies to a
851/// subset of markers, i.e., it shouldn't be called on universal resolutions, and expects only a
852/// single version of each package to be present in the graph.
853impl From<ResolverOutput> for uv_distribution_types::Resolution {
854    fn from(output: ResolverOutput) -> Self {
855        let ResolverOutput {
856            graph,
857            diagnostics,
858            fork_markers,
859            ..
860        } = output;
861
862        assert!(
863            fork_markers.is_empty(),
864            "universal resolutions are not supported"
865        );
866
867        let mut transformed = Graph::with_capacity(graph.node_count(), graph.edge_count());
868        let mut inverse = FxHashMap::with_capacity_and_hasher(graph.node_count(), FxBuildHasher);
869
870        // Create the root node.
871        let root = transformed.add_node(Node::Root);
872
873        // Re-add the nodes to the reduced graph.
874        for index in graph.node_indices() {
875            let ResolutionGraphNode::Dist(dist) = &graph[index] else {
876                continue;
877            };
878            if dist.is_base() {
879                inverse.insert(
880                    &dist.name,
881                    transformed.add_node(Node::Dist {
882                        dist: dist.dist.clone(),
883                        hashes: dist.hashes.clone(),
884                        install: true,
885                    }),
886                );
887            }
888        }
889
890        // Re-add the edges to the reduced graph.
891        for edge in graph.edge_indices() {
892            let (source, target) = graph.edge_endpoints(edge).unwrap();
893
894            match (&graph[source], &graph[target]) {
895                (ResolutionGraphNode::Root, ResolutionGraphNode::Dist(target_dist)) => {
896                    let target = inverse[&target_dist.name()];
897                    transformed.update_edge(root, target, Edge::Prod);
898                }
899                (
900                    ResolutionGraphNode::Dist(source_dist),
901                    ResolutionGraphNode::Dist(target_dist),
902                ) => {
903                    let source = inverse[&source_dist.name()];
904                    let target = inverse[&target_dist.name()];
905
906                    let edge = if let Some(extra) = source_dist.extra.as_ref() {
907                        Edge::Optional(extra.clone())
908                    } else if let Some(group) = source_dist.group.as_ref() {
909                        Edge::Dev(group.clone())
910                    } else {
911                        Edge::Prod
912                    };
913
914                    transformed.add_edge(source, target, edge);
915                }
916                _ => {
917                    unreachable!("root should not contain incoming edges");
918                }
919            }
920        }
921
922        Self::new(transformed).with_diagnostics(diagnostics)
923    }
924}
925
926/// Find any packages that don't have any lower bound on them when in resolution-lowest mode.
927fn report_missing_lower_bounds(
928    graph: &Graph<ResolutionGraphNode, UniversalMarker>,
929    diagnostics: &mut Vec<ResolutionDiagnostic>,
930    constraints: &Constraints,
931    overrides: &Overrides,
932) {
933    for node_index in graph.node_indices() {
934        let ResolutionGraphNode::Dist(dist) = graph.node_weight(node_index).unwrap() else {
935            // Ignore the root package.
936            continue;
937        };
938        if !has_lower_bound(node_index, dist.name(), graph, constraints, overrides) {
939            diagnostics.push(ResolutionDiagnostic::MissingLowerBound {
940                package_name: dist.name().clone(),
941            });
942        }
943    }
944}
945
946/// Whether the given package has a lower version bound by another package.
947fn has_lower_bound(
948    node_index: NodeIndex,
949    package_name: &PackageName,
950    graph: &Graph<ResolutionGraphNode, UniversalMarker>,
951    constraints: &Constraints,
952    overrides: &Overrides,
953) -> bool {
954    for neighbor_index in graph.neighbors_directed(node_index, Direction::Incoming) {
955        let neighbor_dist = match graph.node_weight(neighbor_index).unwrap() {
956            ResolutionGraphNode::Root => {
957                // We already handled direct dependencies with a missing constraint
958                // separately.
959                return true;
960            }
961            ResolutionGraphNode::Dist(neighbor_dist) => neighbor_dist,
962        };
963
964        if neighbor_dist.name() == package_name {
965            // Only warn for real packages, not for virtual packages such as dev nodes.
966            return true;
967        }
968
969        let Some(metadata) = neighbor_dist.metadata.as_ref() else {
970            // We can't check for lower bounds if we lack metadata.
971            return true;
972        };
973
974        // Get all individual specifier for the current package and check if any has a lower
975        // bound.
976        for requirement in metadata
977            .requires_dist
978            .iter()
979            // These bounds sources are missing from the graph.
980            .chain(metadata.dependency_groups.values().flatten())
981            .chain(constraints.requirements())
982            .chain(overrides.requirements())
983        {
984            if requirement.name != *package_name {
985                continue;
986            }
987            let Some(specifiers) = requirement.source.version_specifiers() else {
988                // URL requirements are a bound.
989                return true;
990            };
991            if specifiers.iter().any(VersionSpecifier::has_lower_bound) {
992                return true;
993            }
994        }
995    }
996    false
997}