11use crate :: types:: PackageReq ;
22use libsemverator:: range:: Range as VersionReq ;
3- use std:: collections:: { HashMap , HashSet } ;
3+ use std:: collections:: HashMap ;
44use std:: error:: Error ;
55
6+ /// Projects whose distinct version lines are parallel-installable (different
7+ /// sonames / ICU majors / abseil LTS namespaces). When constraints cannot
8+ /// intersect we keep the extra lines as separate `PackageReq` entries instead
9+ /// of failing the graph. Note: these extra lines are surfaced in the resolved
10+ /// set but their own deps are not re-hydrated — fine while alt-line deps match
11+ /// the primary line (openssl/abseil/unicode), revisit if that stops holding.
12+ ///
13+ /// - unicode.org: ICU major ABI (see pantry#4104, pkgx#899)
14+ /// - openssl.org: libssl.so.1.1 vs libssl.so.3
15+ /// - abseil.io: LTS inline-namespace + soversion (20250127 vs 20250512, …)
16+ const MULTI_VERSION_PROJECTS : & [ & str ] = & [ "unicode.org" , "openssl.org" , "abseil.io" ] ;
17+
18+ fn is_multi_version ( project : & str ) -> bool {
19+ MULTI_VERSION_PROJECTS . contains ( & project)
20+ }
21+
22+ /// Record an extra constraint for a multi-version project, merging into an
23+ /// existing additional entry when the ranges intersect.
24+ fn push_additional ( additional : & mut Vec < PackageReq > , pkg : PackageReq ) {
25+ for existing in additional. iter_mut ( ) . filter ( |p| p. project == pkg. project ) {
26+ if let Ok ( constraint) = intersect_constraints ( & existing. constraint , & pkg. constraint ) {
27+ existing. constraint = constraint;
28+ return ;
29+ }
30+ }
31+ additional. push ( pkg) ;
32+ }
33+
634#[ derive( Clone ) ]
735struct Node {
836 parent : Option < Box < Node > > ,
937 pkg : PackageReq ,
10- children : HashSet < String > ,
1138}
1239
1340impl Node {
1441 fn new ( pkg : PackageReq , parent : Option < Box < Node > > ) -> Self {
15- Self {
16- parent,
17- pkg,
18- children : HashSet :: new ( ) ,
19- }
42+ Self { parent, pkg }
2043 }
2144
2245 fn count ( & self ) -> usize {
@@ -38,36 +61,55 @@ pub async fn hydrate<F>(
3861where
3962 F : Fn ( String ) -> Result < Vec < PackageReq > , Box < dyn Error > > ,
4063{
41- let dry = condense ( input) ;
64+ let dry = condense ( input) ? ;
4265 let mut graph: HashMap < String , Box < Node > > = HashMap :: new ( ) ;
4366 let mut stack: Vec < Box < Node > > = vec ! [ ] ;
44- let mut additional_unicodes : Vec < VersionReq > = vec ! [ ] ;
67+ let mut additional : Vec < PackageReq > = vec ! [ ] ;
4568
4669 for pkg in dry. iter ( ) {
47- let node = graph
48- . entry ( pkg. project . clone ( ) )
49- . or_insert_with ( || Box :: new ( Node :: new ( pkg. clone ( ) , None ) ) ) ;
50- node. pkg . constraint = intersect_constraints ( & node. pkg . constraint , & pkg. constraint )
51- . map_err ( |e| format ! ( "{} for {}" , e, pkg. project) ) ?;
52- stack. push ( node. clone ( ) ) ;
70+ if let Some ( node) = graph. get_mut ( & pkg. project ) {
71+ match intersect_constraints ( & node. pkg . constraint , & pkg. constraint ) {
72+ Ok ( constraint) => {
73+ node. pkg . constraint = constraint;
74+ stack. push ( node. clone ( ) ) ;
75+ }
76+ Err ( e) => {
77+ if is_multi_version ( & pkg. project ) {
78+ push_additional ( & mut additional, pkg. clone ( ) ) ;
79+ } else {
80+ return Err ( format ! ( "{} for {}" , e, pkg. project) . into ( ) ) ;
81+ }
82+ }
83+ }
84+ } else {
85+ let node = Box :: new ( Node :: new ( pkg. clone ( ) , None ) ) ;
86+ graph. insert ( pkg. project . clone ( ) , node. clone ( ) ) ;
87+ stack. push ( node) ;
88+ }
5389 }
5490
55- while let Some ( mut current) = stack. pop ( ) {
91+ while let Some ( current) = stack. pop ( ) {
5692 for child_pkg in get_deps ( current. pkg . project . clone ( ) ) ? {
93+ let was_new = !graph. contains_key ( & child_pkg. project ) ;
5794 let child_node = graph
5895 . entry ( child_pkg. project . clone ( ) )
5996 . or_insert_with ( || Box :: new ( Node :: new ( child_pkg. clone ( ) , Some ( current. clone ( ) ) ) ) ) ;
97+
98+ if was_new {
99+ // Fresh node already carries child_pkg.constraint.
100+ stack. push ( child_node. clone ( ) ) ;
101+ continue ;
102+ }
103+
104+ // Already have a graph node: try the primary constraint, then any
105+ // additional lines for this multi-version project.
60106 let intersection =
61107 intersect_constraints ( & child_node. pkg . constraint , & child_pkg. constraint ) ;
62108 if let Ok ( constraint) = intersection {
63109 child_node. pkg . constraint = constraint;
64- current. children . insert ( child_node. pkg . project . clone ( ) ) ;
65110 stack. push ( child_node. clone ( ) ) ;
66- } else if child_pkg. project == "unicode.org" {
67- // we handle unicode.org for now to allow situations like:
68- // https://github.com/pkgxdev/pantry/issues/4104
69- // https://github.com/pkgxdev/pkgx/issues/899
70- additional_unicodes. push ( child_pkg. constraint ) ;
111+ } else if is_multi_version ( & child_pkg. project ) {
112+ push_additional ( & mut additional, child_pkg) ;
71113 } else {
72114 return Err (
73115 format ! ( "{} for {}" , intersection. unwrap_err( ) , child_pkg. project) . into ( ) ,
@@ -80,30 +122,45 @@ where
80122 pkgs. sort_by_key ( |node| node. count ( ) ) ;
81123 let mut pkgs: Vec < PackageReq > = pkgs. into_iter ( ) . map ( |node| node. pkg . clone ( ) ) . collect ( ) ;
82124
83- // see above explanation
84- for constraint in additional_unicodes {
85- let pkg = PackageReq {
86- project : "unicode.org" . to_string ( ) ,
87- constraint,
88- } ;
89- pkgs. push ( pkg) ;
90- }
125+ pkgs. extend ( additional) ;
91126
92127 Ok ( pkgs)
93128}
94129
95- /// Condenses a list of `PackageRequirement` by intersecting constraints for duplicates.
96- fn condense ( pkgs : & Vec < PackageReq > ) -> Vec < PackageReq > {
130+ /// Condenses a list of `PackageReq` by intersecting constraints for duplicates.
131+ /// Multi-version projects keep non-intersecting constraints as separate entries.
132+ fn condense ( pkgs : & Vec < PackageReq > ) -> Result < Vec < PackageReq > , Box < dyn Error > > {
97133 let mut out: Vec < PackageReq > = vec ! [ ] ;
98134 for pkg in pkgs {
99135 if let Some ( existing) = out. iter_mut ( ) . find ( |p| p. project == pkg. project ) {
100- existing. constraint = intersect_constraints ( & existing. constraint , & pkg. constraint )
101- . expect ( "Failed to intersect constraints" ) ;
136+ match intersect_constraints ( & existing. constraint , & pkg. constraint ) {
137+ Ok ( constraint) => existing. constraint = constraint,
138+ Err ( e) => {
139+ if is_multi_version ( & pkg. project ) {
140+ // merge into a later non-intersecting sibling if possible
141+ let mut merged = false ;
142+ for sibling in out. iter_mut ( ) . filter ( |p| p. project == pkg. project ) . skip ( 1 ) {
143+ if let Ok ( constraint) =
144+ intersect_constraints ( & sibling. constraint , & pkg. constraint )
145+ {
146+ sibling. constraint = constraint;
147+ merged = true ;
148+ break ;
149+ }
150+ }
151+ if !merged {
152+ out. push ( pkg. clone ( ) ) ;
153+ }
154+ } else {
155+ return Err ( format ! ( "{} for {}" , e, pkg. project) . into ( ) ) ;
156+ }
157+ }
158+ }
102159 } else {
103160 out. push ( pkg. clone ( ) ) ;
104161 }
105162 }
106- out
163+ Ok ( out)
107164}
108165
109166/// Intersects two version constraints.
0 commit comments