Skip to content

Commit e317089

Browse files
committed
Explain refused marker splits across -r files
When a requirements tree pins another version of a package on its own environment-marker branch but the patched version's pin has no marker (or there is none), vendoring refuses because pip may install either version. The error used to say the package "is not pinned", which reads as false when an exact pin is right there in another file. It now says that the other version's marker branch needs a marker-branch pin of the patched version beside it. The per-file refusal order now lives in one helper shared by planning and its tests, and the end-to-end test also checks VEX when the vendor line lands in an include. Refs #1104 Assisted-by: Claude Code:claude-opus-5-5
1 parent 89e8337 commit e317089

2 files changed

Lines changed: 108 additions & 54 deletions

File tree

‎crates/socket-patch-cli/tests/e2e_vendor_pypi_build.rs‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ const VEX_VULNS: &[(&str, &[&str])] = &[("GHSA-vend-pypi-real", &["CVE-2024-8888
8989
/// API: the staged patch's `six.py` afterHash + advisory), `original`
9090
/// being the registry requirements the revert step restores.
9191
fn manifestless_vex(fresh: &Path, what: &str, patched: &[u8], original: &[u8]) {
92+
manifestless_vex_reverting(fresh, what, patched, &|p: &Path| {
93+
std::fs::write(p.join("requirements.txt"), original).unwrap()
94+
});
95+
}
96+
97+
/// [`manifestless_vex`] for a wiring that `revert` un-does (the vendor line
98+
/// may sit in a `-r` include rather than the root file).
99+
fn manifestless_vex_reverting(
100+
fresh: &Path,
101+
what: &str,
102+
patched: &[u8],
103+
revert: &(dyn Fn(&Path) + Sync),
104+
) {
92105
let view =
93106
vex_e2e_common::patch_view(UUID, PURL, &[("six.py", &git_sha256(patched))], VEX_VULNS);
94107
vex_pipenv_pip_steps::run_manifestless_steps(&vex_pipenv_pip_steps::Steps {
@@ -101,7 +114,7 @@ fn manifestless_vex(fresh: &Path, what: &str, patched: &[u8], original: &[u8]) {
101114
records: vex_pipenv_pip_steps::Records::Mock(vec![(UUID.to_string(), view)]),
102115
patch_server_url: None,
103116
product: "pkg:pypi/app@0.1.0",
104-
revert: &|p: &Path| std::fs::write(p.join("requirements.txt"), original).unwrap(),
117+
revert,
105118
envs: Vec::new(),
106119
on_step: None,
107120
expect_verified: true,
@@ -1962,16 +1975,15 @@ fn pip_vendored_requirements_marker_split_across_includes() {
19621975
"install vendored cross-file marker-split requirements",
19631976
);
19641977
assert_eq!(python_oracle(&fresh_venv, &fresh), "1", "[{label}]");
1965-
// The VEX steps un-wire by restoring the root file, which holds the
1966-
// vendor line only in this layout.
1967-
if label == "root" {
1968-
manifestless_vex(
1969-
&fresh,
1970-
"pip vendored cross-file marker split",
1971-
&patched,
1972-
root_original.as_bytes(),
1973-
);
1974-
}
1978+
manifestless_vex_reverting(
1979+
&fresh,
1980+
&format!("pip vendored cross-file marker split {label}"),
1981+
&patched,
1982+
&|p: &Path| {
1983+
std::fs::write(p.join("requirements.txt"), &root_original).unwrap();
1984+
std::fs::write(p.join("base.txt"), &base_original).unwrap();
1985+
},
1986+
);
19751987

19761988
let (code, stdout, stderr) =
19771989
run_socket(&project, &["vendor", "--revert", "--offline", "--json"]);

‎crates/socket-patch-core/src/vendor/pypi_requirements.rs‎

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
181205
fn 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

Comments
 (0)