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,33 +122,210 @@ 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.
110167fn intersect_constraints ( a : & VersionReq , b : & VersionReq ) -> Result < VersionReq , Box < dyn Error > > {
111168 a. intersect ( b) . map_err ( |e| e. into ( ) )
112169}
170+
171+ #[ cfg( test) ]
172+ mod tests {
173+ use super :: * ;
174+
175+ fn req ( project : & str , constraint : & str ) -> PackageReq {
176+ PackageReq {
177+ project : project. to_string ( ) ,
178+ constraint : VersionReq :: parse ( constraint) . unwrap ( ) ,
179+ }
180+ }
181+
182+ fn pkgs_for < ' a > ( pkgs : & ' a [ PackageReq ] , project : & str ) -> Vec < & ' a PackageReq > {
183+ pkgs. iter ( ) . filter ( |p| p. project == project) . collect ( )
184+ }
185+
186+ /// True if some hydrated line for `project` intersects `range` (same ABI line).
187+ fn has_line ( pkgs : & [ PackageReq ] , project : & str , range : & str ) -> bool {
188+ let want = VersionReq :: parse ( range) . unwrap ( ) ;
189+ pkgs_for ( pkgs, project)
190+ . into_iter ( )
191+ . any ( |p| p. constraint . intersect ( & want) . is_ok ( ) )
192+ }
193+
194+ /// Assert two ranges remain disjoint (cannot be collapsed).
195+ fn assert_disjoint ( a : & str , b : & str ) {
196+ assert ! ( VersionReq :: parse( a)
197+ . unwrap( )
198+ . intersect( & VersionReq :: parse( b) . unwrap( ) )
199+ . is_err( ) ) ;
200+ }
201+
202+ #[ tokio:: test]
203+ async fn hydrates_unicode_multi ( ) {
204+ let input = vec ! [ req( "npmjs.com" , "*" ) , req( "python.org" , "~3.9" ) ] ;
205+ let pkgs = hydrate ( & input, |project| match project. as_str ( ) {
206+ "python.org" => Ok ( vec ! [ req( "unicode.org" , "^73" ) ] ) ,
207+ "npmjs.com" => Ok ( vec ! [ req( "unicode.org" , "^71" ) ] ) ,
208+ _ => Ok ( vec ! [ ] ) ,
209+ } )
210+ . await
211+ . unwrap ( ) ;
212+
213+ assert_eq ! ( pkgs_for( & pkgs, "unicode.org" ) . len( ) , 2 ) ;
214+ assert ! ( has_line( & pkgs, "unicode.org" , "^71" ) ) ;
215+ assert ! ( has_line( & pkgs, "unicode.org" , "^73" ) ) ;
216+ assert_disjoint ( "^71" , "^73" ) ;
217+ }
218+
219+ #[ tokio:: test]
220+ async fn hydrates_openssl_multi ( ) {
221+ // python locks ^1.1; cryptography needs ^3 — must coexist
222+ let input = vec ! [ req( "python.org" , "*" ) , req( "cryptography.io" , "*" ) ] ;
223+ let pkgs = hydrate ( & input, |project| match project. as_str ( ) {
224+ "python.org" => Ok ( vec ! [ req( "openssl.org" , "^1.1" ) ] ) ,
225+ "cryptography.io" => Ok ( vec ! [ req( "openssl.org" , "^3" ) ] ) ,
226+ _ => Ok ( vec ! [ ] ) ,
227+ } )
228+ . await
229+ . unwrap ( ) ;
230+
231+ assert_eq ! ( pkgs_for( & pkgs, "openssl.org" ) . len( ) , 2 ) ;
232+ assert ! ( has_line( & pkgs, "openssl.org" , "^1.1" ) ) ;
233+ assert ! ( has_line( & pkgs, "openssl.org" , "^3" ) ) ;
234+ assert_disjoint ( "^1.1" , "^3" ) ;
235+ }
236+
237+ #[ tokio:: test]
238+ async fn hydrates_abseil_multi ( ) {
239+ // re2 on one LTS line, grpc on another
240+ let input = vec ! [ req( "github.com/google/re2" , "*" ) , req( "grpc.io" , "*" ) ] ;
241+ let pkgs = hydrate ( & input, |project| match project. as_str ( ) {
242+ "github.com/google/re2" => Ok ( vec ! [ req( "abseil.io" , "^20250127" ) ] ) ,
243+ "grpc.io" => Ok ( vec ! [ req( "abseil.io" , ">=20250512" ) ] ) ,
244+ _ => Ok ( vec ! [ ] ) ,
245+ } )
246+ . await
247+ . unwrap ( ) ;
248+
249+ assert_eq ! ( pkgs_for( & pkgs, "abseil.io" ) . len( ) , 2 ) ;
250+ assert ! ( has_line( & pkgs, "abseil.io" , "^20250127" ) ) ;
251+ assert ! ( has_line( & pkgs, "abseil.io" , ">=20250512" ) ) ;
252+ assert_disjoint ( "^20250127" , ">=20250512" ) ;
253+ }
254+
255+ #[ tokio:: test]
256+ async fn hydrates_openssl_dry_input ( ) {
257+ // explicit +openssl^1.1 +openssl^3
258+ let input = vec ! [ req( "openssl.org" , "^1.1" ) , req( "openssl.org" , "^3" ) ] ;
259+ let pkgs = hydrate ( & input, |_| Ok ( vec ! [ ] ) ) . await . unwrap ( ) ;
260+
261+ assert_eq ! ( pkgs_for( & pkgs, "openssl.org" ) . len( ) , 2 ) ;
262+ assert ! ( has_line( & pkgs, "openssl.org" , "^1.1" ) ) ;
263+ assert ! ( has_line( & pkgs, "openssl.org" , "^3" ) ) ;
264+ }
265+
266+ #[ tokio:: test]
267+ async fn hydrates_multi_version_three_way ( ) {
268+ // three consumers, two openssl lines (two share ^1.1, one needs ^3)
269+ // order cryptography first so ^3 is the graph node and both ^1.1 merge
270+ // into a single additional entry.
271+ let input = vec ! [
272+ req( "cryptography.io" , "*" ) ,
273+ req( "python.org" , "*" ) ,
274+ req( "curl.se" , "*" ) ,
275+ ] ;
276+ let pkgs = hydrate ( & input, |project| match project. as_str ( ) {
277+ "python.org" | "curl.se" => Ok ( vec ! [ req( "openssl.org" , "^1.1" ) ] ) ,
278+ "cryptography.io" => Ok ( vec ! [ req( "openssl.org" , "^3" ) ] ) ,
279+ _ => Ok ( vec ! [ ] ) ,
280+ } )
281+ . await
282+ . unwrap ( ) ;
283+
284+ assert_eq ! ( pkgs_for( & pkgs, "openssl.org" ) . len( ) , 2 ) ;
285+ assert ! ( has_line( & pkgs, "openssl.org" , "^1.1" ) ) ;
286+ assert ! ( has_line( & pkgs, "openssl.org" , "^3" ) ) ;
287+ }
288+
289+ #[ tokio:: test]
290+ async fn hydrates_cannot_intersect ( ) {
291+ let input = vec ! [ req( "npmjs.com" , "*" ) , req( "python.org" , "~3.9" ) ] ;
292+ let err = hydrate ( & input, |project| match project. as_str ( ) {
293+ "python.org" => Ok ( vec ! [ req( "nodejs.com" , "^73" ) ] ) ,
294+ "npmjs.com" => Ok ( vec ! [ req( "nodejs.com" , "^71" ) ] ) ,
295+ _ => Ok ( vec ! [ ] ) ,
296+ } )
297+ . await
298+ . unwrap_err ( ) ;
299+
300+ assert ! ( err. to_string( ) . contains( "nodejs.com" ) ) ;
301+ }
302+
303+ #[ tokio:: test]
304+ async fn hydrates_compatible_intersect ( ) {
305+ let input = vec ! [ req( "pipenv.pypa.io" , "*" ) , req( "python.org" , "~3.9" ) ] ;
306+ let pkgs = hydrate ( & input, |project| match project. as_str ( ) {
307+ "pipenv.pypa.io" => Ok ( vec ! [ req( "python.org" , ">=3.7" ) ] ) ,
308+ _ => Ok ( vec ! [ ] ) ,
309+ } )
310+ . await
311+ . unwrap ( ) ;
312+
313+ let pythons = pkgs_for ( & pkgs, "python.org" ) ;
314+ assert_eq ! ( pythons. len( ) , 1 ) ;
315+ // dry ~3.9 wins over looser >=3.7
316+ assert ! ( has_line( & pkgs, "python.org" , "~3.9" ) ) ;
317+ assert ! ( !has_line( & pkgs, "python.org" , "~3.10" ) ) ;
318+ assert ! ( !has_line( & pkgs, "python.org" , "~3.8" ) ) ;
319+ }
320+
321+ #[ tokio:: test]
322+ async fn hydrates_multi_version_dry_condense_compatible ( ) {
323+ // two compatible dry openssl constraints still collapse to one
324+ let input = vec ! [ req( "openssl.org" , "^1.1" ) , req( "openssl.org" , ">=1.1.1" ) ] ;
325+ let pkgs = hydrate ( & input, |_| Ok ( vec ! [ ] ) ) . await . unwrap ( ) ;
326+
327+ assert_eq ! ( pkgs_for( & pkgs, "openssl.org" ) . len( ) , 1 ) ;
328+ assert ! ( has_line( & pkgs, "openssl.org" , "^1.1" ) ) ;
329+ assert ! ( !has_line( & pkgs, "openssl.org" , "^3" ) ) ;
330+ }
331+ }
0 commit comments