Repository navigation
Description
[agent] Filed by the scheduled architecture refactor routine. Found by running vendored mode over PEP 723 scripts locked by the real uv (0.11.32, uv lock --script) against a stand-in patch service. main at cd7c5c5.
Problem
A vendored patch of a package in a uv script lock (<script>.py.lock) replaces the entry's source and wheels and removes its sdist. The undo puts the entry back from the ledger. When the lock is exactly what the vendoring wrote, the recorded original is written back whole. When it is not (another package of the same lock is vendored too), the undo restores the entry key by key (restore_document), and a key vendoring had removed is put back at the end of the table. Only keys that hold tables are returned to their place in the original's order:
- the insert:
vendor/pypi_lock.rslines 744 to 756; - the reordering, for table-valued keys only: lines 801 to 809.
uv writes sdist = { … } as an inline value above wheels, so every restored entry comes back with sdist below wheels. rollback undoes the packages in the order the scan vendored them, so with two or more vendored packages in one script lock every undo takes this path: a full rollback, a rollback <purl> of one package, and whatever else reverts one entry while another stays.
rollback says Reverted vendoring for … and exits 0. The lock is not the one that was committed: git status shows it modified, and a later uv lock --script does not rewrite it, because uv reads the reordered lock as current.
Harm: low. uv accepts the lock (uv lock --script --check and uv run --locked --script pass), so nothing installs differently. What is wrong is the claim: the undo is built to give the original bytes back (the module's tests assert it), and a checkout that was clean before the scan is dirty after scan and rollback.
It is the class #1623 fixed for sub-tables (1bae9be, "Fix pylock revert misplacing wheel hashes"): that fix returns a restored table to its slot and leaves a restored value at the end.
Evidence on main
$ cat two.py
# /// script
# requires-python = ">=3.9"
# dependencies = ["six==1.16.0", "idna==3.7"]
# ///
import six, idna
$ uv lock --script two.py # uv 0.11.32
$ cp two.py.lock two.py.lock.orig
$ socket-patch scan --mode vendored --yes
Vendored 2 packages. # exit 0; uv run --locked --script two.py loads both patched wheels
$ socket-patch rollback --yes
Reverted vendoring for pkg:pypi/idna@3.7
Reverted vendoring for pkg:pypi/six@1.16.0 # exit 0; .socket/ is gone; two.py is byte for byte
$ diff two.py.lock.orig two.py.lock
15d14
< sdist = { url = "https://files.pythonhosted.org/packages/21/ed/…/idna-3.7.tar.gz", hash = "sha256:028ff3aa…", size = 189575, upload-time = "2024-04-11T03:34:43.276Z" }
18a18
> sdist = { url = "https://files.pythonhosted.org/packages/21/ed/…/idna-3.7.tar.gz", hash = "sha256:028ff3aa…", size = 189575, upload-time = "2024-04-11T03:34:43.276Z" }
24d23
< sdist = { url = "https://files.pythonhosted.org/packages/71/39/…/six-1.16.0.tar.gz", hash = "sha256:1e61c374…", size = 34041, upload-time = "2021-05-05T14:18:18.379Z" }
27a27
> sdist = { url = "https://files.pythonhosted.org/packages/71/39/…/six-1.16.0.tar.gz", hash = "sha256:1e61c374…", size = 34041, upload-time = "2021-05-05T14:18:18.379Z" }
$ uv lock --script two.py --check # exit 0
$ uv lock --script two.py && cmp two.py.lock two.py.lock.orig
two.py.lock two.py.lock.orig differ # uv keeps the reordered lock
rollback pkg:pypi/idna@3.7 alone gives the idna entry back with sdist below wheels, and the later undo of six does the same to six.
Controls, same run. A script with one vendored package, a uv.lock project with the same two packages, and a pylock.toml (uv pip compile and uv export) with the same two packages all come back byte for byte. The pylock does by luck: vendoring removes index, sdist and wheels, and the undo appends them in alphabetical order, which is the order uv wrote. The hosted undo of the same two-package script lock is byte for byte (it rebuilds the entry).
The tests that claim it. separate_package_reverts_do_not_depend_on_order and crlf_two_package_reverts_converge_out_of_order assert that two reverts end on the original text. Their lock has name, version and wheels only, so no key vendoring removes sits above one it keeps. The same test over a lock in uv's shape, beside the first one in crates/socket-patch-core/src/vendor/pypi_lock.rs:
/// A script lock as uv 0.11 writes it (`uv lock --script`): each
/// registry package records `sdist` before `wheels`.
const UV_SCRIPT_LOCK: &str = "version = 1\nrevision = 3\nrequires-python = \">=3.9\"\n\n[manifest]\nrequirements = [\n { name = \"idna\", specifier = \"==3.7\" },\n { name = \"six\", specifier = \"==1.16.0\" },\n]\n\n[[package]]\nname = \"idna\"\nversion = \"3.7\"\nsource = { registry = \"https://pypi.org/simple\" }\nsdist = { url = \"https://files.pythonhosted.org/packages/21/ed/idna-3.7.tar.gz\", hash = \"sha256:028f\", size = 189575, upload-time = \"2024-04-11T03:34:43.276Z\" }\nwheels = [\n { url = \"https://files.pythonhosted.org/packages/e5/3e/idna-3.7-py3-none-any.whl\", hash = \"sha256:82fe\", size = 66836, upload-time = \"2024-04-11T03:34:41.447Z\" },\n]\n\n[[package]]\nname = \"six\"\nversion = \"1.16.0\"\nsource = { registry = \"https://pypi.org/simple\" }\nsdist = { url = \"https://files.pythonhosted.org/packages/71/39/six-1.16.0.tar.gz\", hash = \"sha256:1e61\", size = 34041, upload-time = \"2021-05-05T14:18:18.379Z\" }\nwheels = [\n { url = \"https://files.pythonhosted.org/packages/d9/5a/six-1.16.0-py2.py3-none-any.whl\", hash = \"sha256:8abb\", size = 11053, upload-time = \"2021-05-05T14:18:17.237Z\" },\n]\n";
/// Two packages of a real uv script lock, vendored in the scan's order
/// and reverted in the same order (what `rollback` does): the lock
/// must come back byte for byte.
#[test]
fn uv_script_lock_two_package_reverts_restore_the_lock() {
let first = rewrite_python_lock(
UV_SCRIPT_LOCK,
"idna",
"3.7",
ArtifactSource::Path(".socket/vendor/idna-3.7-py3-none-any.whl"),
"first",
)
.unwrap()
.unwrap();
let both = rewrite_python_lock(
&first,
"six",
"1.16.0",
ArtifactSource::Path(".socket/vendor/six-1.16.0-py2.py3-none-any.whl"),
"second",
)
.unwrap()
.unwrap();
let (second_only, drifted) = restore_document(&both, UV_SCRIPT_LOCK, &first).unwrap();
assert!(!drifted);
let (restored, drifted) = restore_document(&second_only, &first, &both).unwrap();
assert!(!drifted);
assert_eq!(restored, UV_SCRIPT_LOCK);
}$ cargo test -p socket-patch-core --lib -- uv_script_lock_two_package_reverts_restore_the_lock separate_package_reverts_do_not_depend_on_order
test vendor::pypi_lock::tests::separate_package_reverts_do_not_depend_on_order ... ok
assertion `left == right` failed
left: "…source = { registry = \"https://pypi.org/simple\" }\nwheels = [\n { url = …idna-3.7-py3-none-any.whl… },\n]\nsdist = { url = …idna-3.7.tar.gz… }\n\n[[package]]\nname = \"six\"…wheels = [ … ]\nsdist = { … }\n"
right: "…source = { registry = \"https://pypi.org/simple\" }\nsdist = { url = …idna-3.7.tar.gz… }\nwheels = [\n { url = …idna-3.7-py3-none-any.whl… },\n]\n\n[[package]]\nname = \"six\"…sdist = { … }\nwheels = [ … ]\n"
test result: FAILED. 1 passed; 1 failed
Planned fix
In restore_header_table, return every key the restore put back to its slot in the original's key order, not only the keys that hold tables (the unplace step stays for tables alone).
Tried on a build: with inserted.retain(|key| live.contains_key(key)) in place of the table filter, 242 of the 243 tests matching vendor::pypi_lock, vendor::pypi::tests and vendor::pypi_hatch pass, the one above among them. The one that fails, removed_vendored_table_does_not_restore_into_a_namesake, ends by expecting a restored specifier below a marker the user added after vendoring, which is the appended position; with the change specifier comes back where path stood, above marker. That last expectation would change with the fix. The test's subject, drift detection, is unaffected.
The trial was not run through the binary, and not against the inline-table rows of [manifest] (their restore goes through restore_value, which this does not touch).
No JSON shape, exit code or message changes.
Not run: Windows and macOS, a script lock with more than two vendored packages, uv before 0.11, a lock the user or uv edited between the scan and the undo.
Generated by Claude Code
Activity
refactor: L228 to L230 filed (#2521 to #2523)