Skip to content

Commit 478f94f

Browse files
Roll back a vendored PDM 0.x dependency declared with extras (#2060)
* Roll back a vendored PDM 0.x dependency declared with extras `rollback` refused to undo socket-patch's own vendoring of a PyPI package in a PDM 0.x project that declares it with extras (`urllib3 = {version = "==1.26.18", extras = ["socks"]}`): refusing to revert pkg:pypi/urllib3@1.26.18: for pdm.lock, the ledger record for it would change entries other than urllib3's own, which is not an edit socket-patch makes PDM's lock_version 2 format keeps file hashes in `[metadata.files]`, keyed `<name> <version>`, and a unit with extras has a key of its own, `<name>[<extras>] <version>`. Vendoring rewrites both. The revert's confinement check, new in 5.1.0 (#2052), read a key's package as its first word, so `urllib3[socks]` counted as another package's entry and the restore was refused. Nothing was written: the project stayed vendored until its lock was restored from version control. The check now drops the `[<extras>]` suffix before it compares names. Another package's extras key is still refused. Test: confinement_allows_a_pdm_revert_in_every_native_format runs the PDM lock writer over every native lock fixture and holds the check to accepting the revert. It fails on 0.12.3-extras without this change. Found by depscan's PDM patch backtest on the 5.1.0 bump (PDM 0.12.3, extras, vendored). The same cell passes with this build. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Read the PDM files key beside the code that writes it Review follow-up for the extras rollback fix. The check's reading of a `[metadata.files]` key is now `pdm_lock::files_key_package`, next to `legacy_files_key`, which builds the key, with a test that every key the writer builds for the PDM 0.x fixtures reads back as its unit's package. The two can no longer drift apart unnoticed, which is how the 5.1.0 refusal shipped. The `[<extras>]` suffix is dropped only for `files` / `hashes` keys. A `sources` key is read as before, so `"six[socks]"` under `[tool.uv.sources]` is not six's own entry. Tests: revert_round_trip_restores_a_legacy_lock_with_extras vendors and reverts urllib3 in the native PDM 0.12.3 extras lock through wire_pdm and revert_pdm, the path that failed. Without the fix it fails with the refusal users saw. The confinement sweep takes its fixture list from pdm_lock's tests instead of a copy. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 7208343 commit 478f94f

3 files changed

Lines changed: 164 additions & 18 deletions

File tree

‎crates/socket-patch-core/src/utils/pdm_lock.rs‎

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ pub fn legacy_files_key(package: &Table) -> Option<String> {
114114
Some(format!("{name}{suffix} {version}"))
115115
}
116116

117+
/// The package a `[metadata.files]` key is for: [`legacy_files_key`] read
118+
/// back (`<name> <version>`, or `<name>[<extras>] <version>` for a unit with
119+
/// extras). A key that is a bare name, as Poetry's are, is its own answer.
120+
pub fn files_key_package(key: &str) -> &str {
121+
let unit = key.split_whitespace().next().unwrap_or_default();
122+
unit.strip_suffix(']')
123+
.and_then(|open| open.split_once('['))
124+
.map_or(unit, |(name, _)| name)
125+
}
126+
117127
pub fn files_for<'a>(lock: &'a Table, package: &'a Table) -> Option<&'a Array> {
118128
package.get("files").and_then(Item::as_array).or_else(|| {
119129
lock.get("metadata")?
@@ -497,7 +507,20 @@ pub(crate) mod tests {
497507
const WHEEL: &str = "urllib3-1.26.18-py2.py3-none-any.whl";
498508
const PATH: &str = "./.socket/vendor/pypi/e828efa5-5c6d-43f3-9909-03f5ac232b98/urllib3-1.26.18-py2.py3-none-any.whl";
499509

500-
fn fixture(name: &str) -> String {
510+
/// The native locks the rewriter takes (tests/fixtures/pdm-native).
511+
pub(crate) const REWRITABLE: [&str; 9] = [
512+
"0.12.3",
513+
"2.8.2",
514+
"2.9.3",
515+
"2.10.4",
516+
"2.11.2",
517+
"2.17.3",
518+
"2.29.2",
519+
"0.12.3-extras",
520+
"2.29.2-extras",
521+
];
522+
523+
pub(crate) fn fixture(name: &str) -> String {
501524
std::fs::read_to_string(
502525
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
503526
.join("tests/fixtures/pdm-native")
@@ -508,17 +531,7 @@ pub(crate) mod tests {
508531

509532
#[test]
510533
fn native_formats_rewrite_and_reverse_byte_exactly() {
511-
for version in [
512-
"0.12.3",
513-
"2.8.2",
514-
"2.9.3",
515-
"2.10.4",
516-
"2.11.2",
517-
"2.17.3",
518-
"2.29.2",
519-
"0.12.3-extras",
520-
"2.29.2-extras",
521-
] {
534+
for version in REWRITABLE {
522535
for ending in ["\n", "\r\n"] {
523536
let original = fixture(version).replace('\n', ending);
524537
for (kind, source) in [("path", PATH), ("path", &PATH.replace('/', "\\")), ("url", "https://patch.socket.dev/patch/pypi/urllib3/1.26.18/7e52b8b6-53f2-4dc8-860a-1ae7ebd8be0e/e828efa5-5c6d-43f3-9909-03f5ac232b98/urllib3-1.26.18-py2.py3-none-any.whl")] {
@@ -545,6 +558,36 @@ pub(crate) mod tests {
545558
}
546559
}
547560

561+
/// The key the writer builds for a unit reads back as the unit's
562+
/// package, with extras or without: the revert's confinement check
563+
/// depends on it.
564+
#[test]
565+
fn files_key_package_reads_back_every_legacy_key() {
566+
for version in ["0.12.3", "0.12.3-extras"] {
567+
let lock: DocumentMut = fixture(version).parse().unwrap();
568+
let packages = lock["package"].as_array_of_tables().unwrap();
569+
assert!(!packages.is_empty(), "{version}");
570+
for package in packages {
571+
let key = legacy_files_key(package).unwrap();
572+
assert!(lock["metadata"]["files"].get(&key).is_some(), "{key}");
573+
assert_eq!(
574+
files_key_package(&key),
575+
pdm_files_key_name(package["name"].as_str().unwrap()),
576+
"{key}"
577+
);
578+
}
579+
}
580+
for (key, package) in [
581+
("six", "six"),
582+
("zope.interface 5.0", "zope.interface"),
583+
("urllib3[brotli,socks] 1.26.18", "urllib3"),
584+
("odd] 1.0", "odd]"),
585+
("", ""),
586+
] {
587+
assert_eq!(files_key_package(key), package, "{key:?}");
588+
}
589+
}
590+
548591
#[test]
549592
fn native_installers_that_lose_source_identity_refuse() {
550593
for version in ["1.15.5", "2.0.3", "2.1.5", "2.3.4", "2.6.1", "2.7.4"] {

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,19 +1209,21 @@ fn toml_json(item: &Item) -> serde_json::Value {
12091209
/// of the key `parent`: array elements that name the package (a
12101210
/// requirement string, a `{ name = … }` entry, a lock unit), its key in a
12111211
/// `sources` table or in a lock's `files` / `hashes` table (Poetry's and
1212-
/// PDM's older formats, the latter keyed `<name> <version>`), the Hatch
1212+
/// PDM's older formats, the latter keyed `<name> <version>`, or
1213+
/// `<name>[<extras>] <version>` for a unit with extras), the Hatch
12131214
/// direct-reference permission, and then every table and array left
12141215
/// empty.
12151216
fn drop_package(value: &mut serde_json::Value, parent: &str, canon: &str) {
12161217
use serde_json::Value as Json;
12171218
let names = |name: &str| canonicalize_pypi_name(name) == canon;
12181219
match value {
12191220
Json::Object(map) => {
1220-
let keyed = matches!(parent, "sources" | "files" | "hashes");
1221-
map.retain(|key, _| {
1222-
key != "allow-direct-references"
1223-
&& !(keyed && names(key.split_whitespace().next().unwrap_or_default()))
1224-
});
1221+
let own = |key: &str| match parent {
1222+
"sources" => names(key.split_whitespace().next().unwrap_or_default()),
1223+
"files" | "hashes" => names(crate::utils::pdm_lock::files_key_package(key)),
1224+
_ => false,
1225+
};
1226+
map.retain(|key, _| key != "allow-direct-references" && !own(key));
12251227
for (key, child) in map.iter_mut() {
12261228
drop_package(child, key, canon);
12271229
}
@@ -3676,6 +3678,47 @@ mod tests {
36763678
}
36773679
}
36783680

3681+
/// A revert of what the PDM writer wrote is the package's own in every
3682+
/// native lock format. PDM 0.x keys an extras unit's files
3683+
/// `<name>[<extras>] <version>`: read as another package's key, it made
3684+
/// `rollback` refuse to undo its own vendoring of a dependency declared
3685+
/// with extras.
3686+
#[test]
3687+
fn confinement_allows_a_pdm_revert_in_every_native_format() {
3688+
use crate::utils::pdm_lock::tests::{fixture as native_lock, REWRITABLE};
3689+
const WHEEL: &str = "urllib3-1.26.18-py2.py3-none-any.whl";
3690+
for fixture in REWRITABLE {
3691+
let original = native_lock(fixture);
3692+
let vendored = crate::utils::pdm_lock::rewrite_pdm_lock(
3693+
&original,
3694+
"urllib3",
3695+
"1.26.18",
3696+
("path", &format!("./.socket/vendor/pypi/u/{WHEEL}")),
3697+
WHEEL,
3698+
&"a".repeat(64),
3699+
)
3700+
.unwrap();
3701+
assert_ne!(vendored, original, "{fixture}");
3702+
assert_eq!(
3703+
confinement_refusal(&vendored, &original, "urllib3"),
3704+
None,
3705+
"{fixture}"
3706+
);
3707+
}
3708+
3709+
// Another package's extras key is still not the package's own, and
3710+
// a `sources` key is a name: nothing is read off the end of one.
3711+
let wired = "[metadata]\ncontent_hash = \"h\"\n\n[metadata.files]\n\"six[socks] 1.16.0\" = [{ file = \"six.whl\", hash = \"sha256:cc\" }]\n\"idna[socks] 3.7\" = [{ file = \"idna.whl\", hash = \"sha256:dd\" }]\n";
3712+
let restored = wired.replace("sha256:cc", "sha256:bb");
3713+
assert_eq!(confinement_refusal(wired, &restored, "six"), None);
3714+
assert!(
3715+
confinement_refusal(wired, &restored.replace("sha256:dd", "sha256:ee"), "six")
3716+
.is_some()
3717+
);
3718+
let sourced = "[tool.uv.sources]\n\"six[socks]\" = { path = \"a\" }\n";
3719+
assert!(confinement_refusal(sourced, &sourced.replace("\"a\"", "\"b\""), "six").is_some());
3720+
}
3721+
36793722
/// ...and what it may not: another package, a setting, or anything in a
36803723
/// file it cannot read as TOML.
36813724
#[test]

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,66 @@ distribution = false
13281328
}
13291329
}
13301330

1331+
/// PDM 0.x keeps an extras unit's file hashes under a key of its own,
1332+
/// `<name>[<extras>] <version>`. Vendoring rewrites both keys and the
1333+
/// revert takes both back (5.1.0 refused it as a change to another
1334+
/// package's entry).
1335+
#[tokio::test]
1336+
async fn revert_round_trip_restores_a_legacy_lock_with_extras() {
1337+
const WHEEL: &str = "urllib3-1.26.18-py2.py3-none-any.whl";
1338+
let rel_wheel = format!(".socket/vendor/pypi/{UUID}/{WHEEL}");
1339+
let before = crate::utils::pdm_lock::tests::fixture("0.12.3-extras");
1340+
let tmp = write_project(
1341+
&before,
1342+
"[tool.pdm]\nname = \"legacy-extras\"\nversion = \"0.0.0\"\npython_requires = \">=3.8\"\n\n[tool.pdm.dependencies]\nurllib3 = {version = \"==1.26.18\", extras = [\"socks\"]}\n",
1343+
)
1344+
.await;
1345+
let p = load_pdm_project(tmp.path()).await.unwrap();
1346+
let (wiring, meta) = wire_pdm(
1347+
&p,
1348+
tmp.path(),
1349+
"urllib3",
1350+
"1.26.18",
1351+
&rel_wheel,
1352+
WHEEL,
1353+
WHEEL_SHA,
1354+
UUID,
1355+
&[WHEEL_SHA],
1356+
)
1357+
.await
1358+
.unwrap();
1359+
let vendored = read_lock(tmp.path()).await;
1360+
assert_eq!(vendored.matches(&rel_wheel).count(), 2, "{vendored}");
1361+
assert_eq!(
1362+
vendored.matches(&format!("sha256:{WHEEL_SHA}")).count(),
1363+
2,
1364+
"{vendored}"
1365+
);
1366+
let entry = VendorEntry {
1367+
flavor: Some("pdm".into()),
1368+
pdm: Some(meta),
1369+
..VendorEntry::new(
1370+
"pypi".into(),
1371+
"pkg:pypi/urllib3@1.26.18".into(),
1372+
UUID.into(),
1373+
VendorArtifact {
1374+
yarn_berry10c0: None,
1375+
path: rel_wheel.clone(),
1376+
sha256: WHEEL_SHA.into(),
1377+
size: Some(11053),
1378+
platform_locked: None,
1379+
file_inventory: None,
1380+
},
1381+
wiring,
1382+
)
1383+
};
1384+
1385+
let outcome = revert_pdm(&entry, tmp.path(), false).await;
1386+
assert!(outcome.success, "{:?}", outcome.error);
1387+
assert!(outcome.warnings.is_empty(), "{:?}", outcome.warnings);
1388+
assert_eq!(read_lock(tmp.path()).await, before, "byte-identical revert");
1389+
}
1390+
13311391
/// The lock file is user-owned: wiring the splice must not reset its
13321392
/// permission bits (see `atomic_write_bytes_preserving_mode`; the revert
13331393
/// leg is covered in common.rs).

0 commit comments

Comments
 (0)