Skip to content

Commit 027d881

Browse files
Fix vendored pip marker split across -r files (#1104) (#1703)
* Start fix for #1104 Assisted-by: Claude Code:claude-opus-5-5 * Vendor pip marker splits across -r includes A project that keeps a version-specific pin in a separate requirements file, e.g. `six==1.16.0 ; python_version < "3.12"` in requirements.txt and `six==1.17.0 ; python_version >= "3.12"` in an included base.txt, could not vendor a patch for that package: the run exited 1 with a false "six is not pinned to ==1.16.0", and a fresh install stayed unpatched. pip merges the root file and its -r includes into one requirement set, but the marker-branch check from #928 was made one file at a time, so the file holding only the other version's branch looked ambiguous. Make that decision once over the whole include tree. The vendored branch is rewritten where it lives and the other branch is left alone; an unmarked target pin, or no target pin at all, still refuses. Fixes #1104 Assisted-by: Claude Code:claude-opus-5-5 * Test pip marker split across includes end to end Vendor a six marker split whose branches sit in different files of the -r tree, in both layouts, then install the result with pip --no-index in a fresh checkout and check the patched module is imported, and that revert restores both files byte for byte. Refs #1104 Assisted-by: Claude Code:claude-opus-5-5 * 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 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 97cfdbf commit 027d881

2 files changed

Lines changed: 348 additions & 38 deletions

File tree

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

Lines changed: 126 additions & 1 deletion
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,
@@ -1874,6 +1887,118 @@ fn pip_vendored_requirements_evaluate_environment_markers() {
18741887
}
18751888
}
18761889

1890+
/// #1104: the #928 marker split with its branches in different files of
1891+
/// the `-r` tree (a layered `base.txt` plus a root file). pip merges the
1892+
/// tree into one requirement set, so vendoring must rewrite the 1.16.0
1893+
/// branch where it lives, leave the other version's branch alone in either
1894+
/// layout, install the patched wheel from a fresh `--no-index` checkout,
1895+
/// and revert byte-identically. Before the fix the file holding only the
1896+
/// other branch made vendored refuse with a false
1897+
/// `pypi_requirement_not_pinned`.
1898+
#[test]
1899+
#[serial_test::serial]
1900+
fn pip_vendored_requirements_marker_split_across_includes() {
1901+
let python =
1902+
find_python().expect("Python is required for the pip cross-file marker-split regression");
1903+
let target = "six==1.16.0 ; python_full_version >= '3'\n";
1904+
// Never matches the running interpreter.
1905+
let other = "six==1.17.0 ; python_full_version < '3'\n";
1906+
for (label, root_pin, include_pin) in [("root", target, other), ("include", other, target)] {
1907+
let tmp = tempfile::tempdir().unwrap();
1908+
let project = tmp.path().join("proj");
1909+
std::fs::create_dir_all(&project).unwrap();
1910+
assert_tool_ok(
1911+
&tool(Path::new(python), &project, &["-m", "venv", ".venv"], &[]),
1912+
"create source venv",
1913+
);
1914+
let venv = project.join(".venv");
1915+
assert_tool_ok(
1916+
&tool(
1917+
&venv.join("bin/pip"),
1918+
&project,
1919+
&["install", "--disable-pip-version-check", "six==1.16.0"],
1920+
&[],
1921+
),
1922+
"install upstream six",
1923+
);
1924+
let patched = stage_patch(&project, &site_packages(&venv).join("six.py"));
1925+
let root_original = format!("-r base.txt\n{root_pin}");
1926+
let base_original = include_pin.to_string();
1927+
std::fs::write(project.join("requirements.txt"), &root_original).unwrap();
1928+
std::fs::write(project.join("base.txt"), &base_original).unwrap();
1929+
1930+
let (code, stdout, stderr) = run_vendored(&VendorDriver::VendorOffline, &project);
1931+
assert_eq!(code, 0, "[{label}] vendor failed: {stdout}\n{stderr}");
1932+
assert_vendored_applied(&parse_envelope(&stdout));
1933+
let (wired, untouched, untouched_original) = if label == "root" {
1934+
("requirements.txt", "base.txt", &base_original)
1935+
} else {
1936+
("base.txt", "requirements.txt", &root_original)
1937+
};
1938+
assert_eq!(
1939+
&std::fs::read_to_string(project.join(untouched)).unwrap(),
1940+
untouched_original,
1941+
"[{label}] the other branch's file must stay untouched"
1942+
);
1943+
let wired_now = std::fs::read_to_string(project.join(wired)).unwrap();
1944+
let vendor_line = wired_now.lines().last().unwrap();
1945+
assert!(
1946+
vendor_line.starts_with("./.socket/vendor/pypi/")
1947+
&& vendor_line.contains(" ; python_full_version >= '3' # socket-patch vendor:"),
1948+
"[{label}] the 1.16.0 branch becomes a marker-carrying vendor line: {vendor_line}"
1949+
);
1950+
1951+
let fresh = project.join("fresh");
1952+
std::fs::create_dir_all(&fresh).unwrap();
1953+
for file in ["requirements.txt", "base.txt"] {
1954+
std::fs::copy(project.join(file), fresh.join(file)).unwrap();
1955+
}
1956+
copy_dir_recursive(&project.join(".socket"), &fresh.join(".socket"));
1957+
assert_tool_ok(
1958+
&tool(Path::new(python), &fresh, &["-m", "venv", ".venv"], &[]),
1959+
"create fresh venv",
1960+
);
1961+
let fresh_venv = fresh.join(".venv");
1962+
assert_tool_ok(
1963+
&tool(
1964+
&fresh_venv.join("bin/pip"),
1965+
&fresh,
1966+
&[
1967+
"install",
1968+
"--disable-pip-version-check",
1969+
"--no-index",
1970+
"-r",
1971+
"requirements.txt",
1972+
],
1973+
&[],
1974+
),
1975+
"install vendored cross-file marker-split requirements",
1976+
);
1977+
assert_eq!(python_oracle(&fresh_venv, &fresh), "1", "[{label}]");
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+
);
1987+
1988+
let (code, stdout, stderr) =
1989+
run_socket(&project, &["vendor", "--revert", "--offline", "--json"]);
1990+
assert_eq!(code, 0, "[{label}] revert failed: {stdout}\n{stderr}");
1991+
assert_eq!(
1992+
std::fs::read_to_string(project.join("requirements.txt")).unwrap(),
1993+
root_original
1994+
);
1995+
assert_eq!(
1996+
std::fs::read_to_string(project.join("base.txt")).unwrap(),
1997+
base_original
1998+
);
1999+
}
2000+
}
2001+
18772002
/// #928: `uv pip compile --universal` splits a package across marker
18782003
/// branches when it resolves differently per Python. Vendoring the patched
18792004
/// 1.16.0 must rewrite only that branch (marker and hash kept) and leave

0 commit comments

Comments
 (0)