@@ -81,21 +81,45 @@ struct PinScan {
8181 other_branches : bool ,
8282}
8383
84- /// Whether the other version's marker branches (if any) are harmless given
85- /// the target pins: every target pin must carry a marker too, and there
86- /// must be one. That is the shape `uv pip compile --universal` writes when
84+ /// Whether the other version's marker branches (if any) in a requirement
85+ /// set are harmless given its target pins: every target pin must carry a
86+ /// marker too, and there must be one. That is the shape `uv pip compile --universal` writes when
8787/// a package resolves differently per Python (#928), and pip installs
8888/// exactly one branch. Only the target branches are rewritten; the other
8989/// branch is left alone, as hosted requirements and vendored pylock already
9090/// do. Without markers on both sides pip could see both pins at once, so it
9191/// stays a refusal.
9292///
9393/// pip merges the root file and its `-r` includes into one requirement set,
94- /// so [`plan_requirements`] asks this once over every file's pins : the two
94+ /// so [`plan_requirements`] asks this once over every file's scan : the two
9595/// branches may sit in different files (#1104).
96- fn marker_split_ok < ' a > ( other_branches : bool , exact : impl Iterator < Item = & ' a PinSpan > ) -> bool {
97- let mut exact = exact. peekable ( ) ;
98- !other_branches || ( exact. peek ( ) . is_some ( ) && exact. all ( |( _, _, marker, _) | marker. is_some ( ) ) )
96+ fn marker_split_ok ( scans : & [ PinScan ] ) -> bool {
97+ let mut exact = scans. iter ( ) . flat_map ( |s| s. exact . iter ( ) ) . peekable ( ) ;
98+ !scans. iter ( ) . any ( |s| s. other_branches )
99+ || ( exact. peek ( ) . is_some ( ) && exact. all ( |( _, _, marker, _) | marker. is_some ( ) ) )
100+ }
101+
102+ /// Why one file's pins refuse the vendor, in fail-closed precedence: any
103+ /// extras occurrence wins over any ambiguous occurrence wins over the other
104+ /// version's marker branch when the requirement set's target pins do not
105+ /// all carry a marker (`split_ok` from [`marker_split_ok`]).
106+ #[ derive( Debug , PartialEq , Eq ) ]
107+ enum PinRefusal {
108+ Extras ,
109+ NotPinned ,
110+ UnmarkedSplit ,
111+ }
112+
113+ fn pin_refusal ( scan : & PinScan , split_ok : bool ) -> Option < PinRefusal > {
114+ if scan. extras {
115+ Some ( PinRefusal :: Extras )
116+ } else if scan. range {
117+ Some ( PinRefusal :: NotPinned )
118+ } else if scan. other_branches && !split_ok {
119+ Some ( PinRefusal :: UnmarkedSplit )
120+ } else {
121+ None
122+ }
99123}
100124
101125/// Scan one file for the target package: every clean exact
@@ -173,18 +197,18 @@ pub(super) fn names_package(content: &str, canon_name: &str) -> bool {
173197}
174198
175199/// Find the target pin in one file's content, read as the whole
176- /// requirement set. Precedence is fail-closed: any extras occurrence wins
177- /// over any non-pin occurrence wins over a clean exact pin — a file that
178- /// names the package ambiguously is never rewritten (a marker-split other
179- /// version is not ambiguous; see [`marker_split_ok`]).
200+ /// requirement set, through the same [`pin_refusal`] precedence
201+ /// [`plan_requirements`] applies: a file that names the package ambiguously
202+ /// is never rewritten (a marker-split other version is not ambiguous; see
203+ /// [`marker_split_ok`]).
180204#[ cfg( test) ]
181205fn find_pin ( content : & str , canon_name : & str , version : & str ) -> PinSearch {
182206 let scan = scan_pins ( content, canon_name, version) ;
183- if scan . extras {
184- return PinSearch :: Extras ;
185- }
186- if scan . range || ! marker_split_ok ( scan . other_branches , scan . exact . iter ( ) ) {
187- return PinSearch :: Range ;
207+ let split_ok = marker_split_ok ( std :: slice :: from_ref ( & scan ) ) ;
208+ match pin_refusal ( & scan , split_ok ) {
209+ Some ( PinRefusal :: Extras ) => return PinSearch :: Extras ,
210+ Some ( PinRefusal :: NotPinned | PinRefusal :: UnmarkedSplit ) => return PinSearch :: Range ,
211+ None => { }
188212 }
189213 match scan. exact . into_iter ( ) . next ( ) {
190214 Some ( ( line_start, line_count, marker, hashed) ) => PinSearch :: Exact {
@@ -746,34 +770,46 @@ async fn plan_requirements(
746770 . collect ( ) ;
747771 // pip reads the root and its includes as one requirement set, so the
748772 // other version's marker branch may sit in a different file than the
749- // target's (#1104): decide that over the whole tree, and name the file
750- // holding the other version when it is ambiguous.
751- let split_ok = marker_split_ok (
752- scans. iter ( ) . any ( |s| s. other_branches ) ,
753- scans. iter ( ) . flat_map ( |s| s. exact . iter ( ) ) ,
754- ) ;
773+ // target's (#1104): decide that over the whole tree.
774+ let split_ok = marker_split_ok ( & scans) ;
755775
756776 for ( file, scan) in files. iter ( ) . zip ( scans) {
757- if scan. extras {
758- return Err ( (
759- "pypi_extras_unsupported" ,
760- format ! (
761- "{}: the {canon_name} pin declares extras, which a vendored wheel path \
762- line cannot express; remove the extras or use agent mode \
763- (`scan --mode agent` + `socket-patch apply`) instead",
764- file. rel
765- ) ,
766- ) ) ;
767- }
768- if scan. range || ( scan. other_branches && !split_ok) {
769- return Err ( (
770- "pypi_requirement_not_pinned" ,
771- format ! (
772- "{}: {canon_name} is not pinned to =={version}; pin it exactly or use \
773- agent mode (`scan --mode agent` + `socket-patch apply`) instead",
774- file. rel
775- ) ,
776- ) ) ;
777+ match pin_refusal ( & scan, split_ok) {
778+ Some ( PinRefusal :: Extras ) => {
779+ return Err ( (
780+ "pypi_extras_unsupported" ,
781+ format ! (
782+ "{}: the {canon_name} pin declares extras, which a vendored wheel path \
783+ line cannot express; remove the extras or use agent mode \
784+ (`scan --mode agent` + `socket-patch apply`) instead",
785+ file. rel
786+ ) ,
787+ ) ) ;
788+ }
789+ Some ( PinRefusal :: NotPinned ) => {
790+ return Err ( (
791+ "pypi_requirement_not_pinned" ,
792+ format ! (
793+ "{}: {canon_name} is not pinned to =={version}; pin it exactly or use \
794+ agent mode (`scan --mode agent` + `socket-patch apply`) instead",
795+ file. rel
796+ ) ,
797+ ) ) ;
798+ }
799+ Some ( PinRefusal :: UnmarkedSplit ) => {
800+ return Err ( (
801+ "pypi_requirement_not_pinned" ,
802+ format ! (
803+ "{}: pins another {canon_name} version on an environment-marker \
804+ branch, but not every =={version} pin in the requirements tree sits \
805+ on a marker branch of its own (or there is none), so pip may install \
806+ either version; pin =={version} on its own marker branch or use agent \
807+ mode (`scan --mode agent` + `socket-patch apply`) instead",
808+ file. rel
809+ ) ,
810+ ) ) ;
811+ }
812+ None => { }
777813 }
778814 if scan. exact . is_empty ( ) {
779815 continue ;
@@ -2168,7 +2204,13 @@ mod tests {
21682204 . await
21692205 . unwrap_err ( ) ;
21702206 assert_eq ! ( err. 0 , "pypi_requirement_not_pinned" , "{root:?} / {base:?}" ) ;
2171- assert ! ( err. 1 . starts_with( & format!( "{named}: " ) ) , "{}" , err. 1 ) ;
2207+ assert ! (
2208+ err. 1
2209+ . starts_with( & format!( "{named}: pins another six version" ) )
2210+ && err. 1 . contains( "pin ==1.16.0 on its own marker branch" ) ,
2211+ "{}" ,
2212+ err. 1
2213+ ) ;
21722214 assert_eq ! ( read_root( tmp. path( ) ) . await , root) ;
21732215 assert_eq ! (
21742216 tokio:: fs:: read_to_string( tmp. path( ) . join( "base.txt" ) )
0 commit comments