Skip to content

Commit 0baea26

Browse files
mikolalysenkoclaude
andcommitted
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>
1 parent 213ae94 commit 0baea26

3 files changed

Lines changed: 128 additions & 42 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: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,20 +1216,14 @@ fn toml_json(item: &Item) -> serde_json::Value {
12161216
fn drop_package(value: &mut serde_json::Value, parent: &str, canon: &str) {
12171217
use serde_json::Value as Json;
12181218
let names = |name: &str| canonicalize_pypi_name(name) == canon;
1219-
// The package a `files` / `hashes` key is for: its first word, less the
1220-
// `[<extras>]` PDM's older formats append for an extras unit.
1221-
let keys = |key: &str| {
1222-
let unit = key.split_whitespace().next().unwrap_or_default();
1223-
let name = unit
1224-
.strip_suffix(']')
1225-
.and_then(|open| open.split_once('['))
1226-
.map_or(unit, |(name, _)| name);
1227-
names(name)
1228-
};
12291219
match value {
12301220
Json::Object(map) => {
1231-
let keyed = matches!(parent, "sources" | "files" | "hashes");
1232-
map.retain(|key, _| key != "allow-direct-references" && !(keyed && keys(key)));
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));
12331227
for (key, child) in map.iter_mut() {
12341228
drop_package(child, key, canon);
12351229
}
@@ -3691,24 +3685,10 @@ mod tests {
36913685
/// with extras.
36923686
#[test]
36933687
fn confinement_allows_a_pdm_revert_in_every_native_format() {
3688+
use crate::utils::pdm_lock::tests::{fixture as native_lock, REWRITABLE};
36943689
const WHEEL: &str = "urllib3-1.26.18-py2.py3-none-any.whl";
3695-
for fixture in [
3696-
"0.12.3",
3697-
"0.12.3-extras",
3698-
"2.8.2",
3699-
"2.9.3",
3700-
"2.10.4",
3701-
"2.11.2",
3702-
"2.17.3",
3703-
"2.29.2",
3704-
"2.29.2-extras",
3705-
] {
3706-
let original = std::fs::read_to_string(
3707-
Path::new(env!("CARGO_MANIFEST_DIR"))
3708-
.join("tests/fixtures/pdm-native")
3709-
.join(format!("{fixture}.lock")),
3710-
)
3711-
.unwrap();
3690+
for fixture in REWRITABLE {
3691+
let original = native_lock(fixture);
37123692
let vendored = crate::utils::pdm_lock::rewrite_pdm_lock(
37133693
&original,
37143694
"urllib3",
@@ -3726,14 +3706,17 @@ mod tests {
37263706
);
37273707
}
37283708

3729-
// Another package's extras key is still not the package's own.
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.
37303711
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";
37313712
let restored = wired.replace("sha256:cc", "sha256:bb");
37323713
assert_eq!(confinement_refusal(wired, &restored, "six"), None);
37333714
assert!(
37343715
confinement_refusal(wired, &restored.replace("sha256:dd", "sha256:ee"), "six")
37353716
.is_some()
37363717
);
3718+
let sourced = "[tool.uv.sources]\n\"six[socks]\" = { path = \"a\" }\n";
3719+
assert!(confinement_refusal(sourced, &sourced.replace("\"a\"", "\"b\""), "six").is_some());
37373720
}
37383721

37393722
/// ...and what it may not: another package, a setting, or anything in a

‎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)