Skip to content

Commit e5049ff

Browse files
committed
Key NuGet lock versions as the writers do; vendored matches hosted
Second review round on NuGet other-version discovery. - Discovery keyed lock versions by `normalize_nuget_version` of the TRIMMED `resolved`, while both writers normalize the raw value: a padded ` 13.0.3 ` beside `13.0.3` was one version to discovery (and attested) but another to the writers (which refuse the lock). It is keyed raw now, so the ref is withheld (`vex_nuget_lock_other_version`). - A vendored ref was still emitted, attested and unpinned, when the lock could not be read; hosted emits none. Vendored now matches hosted. - A vendored ref named `nupkg_leaf(id, lock spelling)` when locked but the feed's real file otherwise; it always names the feed's file. - The unusable-version diagnostic named the root lock even when only a member lock held the entry; it names the locks that do. - Pre-release labels order by identifier (numeric ones numerically), so the lock an other-version mark points at follows NuGet precedence. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 56bffb2 commit e5049ff

2 files changed

Lines changed: 115 additions & 20 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ Discovery is read-only, never touches the network, and never fails the run: a ma
539539
| composer | `composer.lock` (`packages` + `packages-dev`) | `dist.url` on the patch host | `dist: {type: "path", url: ".socket/vendor/composer/<uuid>/…", reference: "<uuid>"}` | `dist.shasum`, required |
540540
| maven | `pom.xml` (+ `.mvn/maven.config`, `.mvn/checksums/checksums.sha256`) | a dependency version `<base>-socket.<hex8>` matching exactly ONE `socket-patch-<uuid>` repository on the patch host | pre-v5 single-POM wiring only: `socket-patch-vendor-<uuid>` repository + exactly one jar under `.socket/vendor/maven/<uuid>/` with a matching `.sha1`. A v5 suffixed pin served from `.socket/vendor/maven2` is no reference here: its ledger entry's wiring check gates it, so without a ledger it is not attested | Trusted Checksums line when enabled; not required (the suffixed version is the pin) |
541541
| gradle (v5.0) | `.socket/gradle/hosted-index.tsv`, the owned hosted script, and every settings script and lock file the script graph reaches | an index row whose repository URL is on the patch host and names the row's uuid, live only while the owned script is intact, every build's settings file applies it with the current index digest, every lock entry of the GA is the suffixed version, no `settings-gradle.lockfile` names the GA and no build script sets a custom `lockFile` (otherwise `patched_ref_invalid`) | none here: a vendored Gradle entry is gated by its ledger entry's wiring check | the suffixed copies installed in the Gradle cache; required (never the lock basis), because the script lets a higher upstream version resolve |
542-
| nuget | the first of `nuget.config` / `NuGet.config` / `NuGet.Config`, + `packages.lock.json` | source `socket-patch-<uuid>` + its exclusive exact-id `<packageSourceMapping>`; version from `packages.lock.json` | the same mapping onto `.socket/vendor/nuget/<uuid>`; version from the feed's single nupkg (the patched version), else the lock | `contentHash`, required. Lock versions match under `normalize_nuget_version` of the trimmed `resolved`, as the writers match them (`1.0` and `1.0.0` are one version; an empty one is `patched_ref_invalid`). When the governed locks also lock the mapped id at a version other than the patched one (another target framework or project), which both writers refuse (`redirect_nuget_lock_other_version`, `vendor_nuget_lock_other_version`), the reference is kept but not attested (`vex_nuget_lock_other_version`, naming the other versions and their locks). Vendored, the patched version is the feed's single nupkg: it is the only reference, even when no lock entry is at it, and other locked versions are never references. A hosted url names no version, so each hosted reference is checked against its own version: any two locked versions withhold them all, and a single locked version other than the patch's is gated by the patch record (`record_mismatch`). |
542+
| nuget | the first of `nuget.config` / `NuGet.config` / `NuGet.Config`, + `packages.lock.json` | source `socket-patch-<uuid>` + its exclusive exact-id `<packageSourceMapping>`; version from `packages.lock.json` | the same mapping onto `.socket/vendor/nuget/<uuid>`; version from the feed's single nupkg (the patched version), else the lock | `contentHash`, required. Lock versions match under `normalize_nuget_version` of the raw `resolved`, as the writers match them (a padded ` 1.0 ` is another version) (`1.0` and `1.0.0` are one version; an empty one is `patched_ref_invalid`). When the governed locks also lock the mapped id at a version other than the patched one (another target framework or project), which both writers refuse (`redirect_nuget_lock_other_version`, `vendor_nuget_lock_other_version`), the reference is kept but not attested (`vex_nuget_lock_other_version`, naming the other versions and their locks). Vendored, the patched version is the feed's single nupkg: it is the only reference, even when no lock entry is at it, and other locked versions are never references. A hosted url names no version, so each hosted reference is checked against its own version: any two locked versions withhold them all, and a single locked version other than the patch's is gated by the patch record (`record_mismatch`). |
543543
| deno | `deno.lock`'s npm section, only as evidence against an npm-family pin of a `name@version` it also locks, which `vex` then omits (see **Unattested references**, #406); hosted and vendored npm runs warn about such a pin (`redirect_npm_deno_lock` / `vendor_npm_deno_lock`) | — (no hosted mode) | — (no vendored backend) | — |
544544

545545
Recognition rules that hold for every ecosystem:

‎crates/socket-patch-core/src/vex/discover/nuget.rs‎

Lines changed: 114 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,11 @@ async fn load_lock(ctx: &DiscoverCtx<'_>, ids: &BTreeSet<String>, out: &mut Disc
389389
continue;
390390
}
391391
let id_locks = index.entry(id).or_default();
392-
let resolved = entry.resolved.trim();
393-
if resolved.is_empty() {
392+
// Keyed exactly as the writers key it (`normalize_nuget_version`
393+
// of the raw `resolved`, no trim): a padded spelling is another
394+
// version to them, so it is one here too.
395+
let resolved = entry.resolved;
396+
if resolved.trim().is_empty() {
394397
if id_locks.empty.insert(rel.clone()) {
395398
out.diag(
396399
DIAG_REF_INVALID,
@@ -480,8 +483,12 @@ fn sorted_versions(locks: &IdLocks) -> Vec<(&String, &Pins)> {
480483
}
481484

482485
/// A normalized NuGet version's sort key: numeric segments, then a release
483-
/// above its pre-releases, then the pre-release label. Orders listings only.
484-
fn precedence(norm: &str) -> (Vec<u64>, bool, &str) {
486+
/// above its pre-releases, then the pre-release label by dot-separated
487+
/// identifiers as NuGet (SemVer 2) compares them: a numeric one
488+
/// numerically and below an alphanumeric one, an alphanumeric one ignoring
489+
/// case, a shorter label first when it is a prefix (`rc.2` < `rc.10`).
490+
/// Orders listings only.
491+
fn precedence(norm: &str) -> (Vec<u64>, bool, Vec<(bool, u64, String)>) {
485492
let (core, pre) = match norm.split_once('-') {
486493
Some((core, pre)) => (core, pre),
487494
None => (norm, ""),
@@ -490,7 +497,15 @@ fn precedence(norm: &str) -> (Vec<u64>, bool, &str) {
490497
.split('.')
491498
.map(|s| s.parse().unwrap_or(u64::MAX))
492499
.collect();
493-
(segments, pre.is_empty(), pre)
500+
let label = pre
501+
.split('.')
502+
.filter(|_| !pre.is_empty())
503+
.map(|id| match id.parse::<u64>() {
504+
Ok(n) if id.bytes().all(|b| b.is_ascii_digit()) => (false, n, String::new()),
505+
_ => (true, 0, id.to_ascii_lowercase()),
506+
})
507+
.collect();
508+
(segments, pre.is_empty(), label)
494509
}
495510

496511
/// `(file, detail)` of an [`other_versions`] finding.
@@ -680,12 +695,19 @@ fn emit_hosted(
680695
);
681696
for row in lock_rows(locks, src, cfg_rel, out) {
682697
let Some(purl) = simple_purl("nuget", &src.id, &row.spelling) else {
698+
let files: Vec<&str> = locks.versions[&row.norm]
699+
.files
700+
.iter()
701+
.map(String::as_str)
702+
.collect();
683703
out.diag(
684704
DIAG_REF_INVALID,
685-
PACKAGES_LOCK,
705+
files[0],
686706
format!(
687-
"{PACKAGES_LOCK}: {} resolves to the unusable version {:?}",
688-
src.id, row.spelling
707+
"{}: {} resolves to the unusable version {:?}",
708+
files.join(", "),
709+
src.id,
710+
row.spelling
689711
),
690712
);
691713
continue;
@@ -722,7 +744,10 @@ async fn emit_vendored(
722744
let id_lower = src.id.to_ascii_lowercase();
723745
let locks = match lock {
724746
Lock::Parsed(index) => index.get(&id_lower),
725-
Lock::Absent | Lock::Unusable => None,
747+
Lock::Absent => None,
748+
// A lock that cannot be read (diagnosed) might lock another version
749+
// the writers refuse: no ref, as hosted.
750+
Lock::Unusable => return,
726751
};
727752
let serves = format!("the vendored feed {VENDOR_DIR}/nuget/{}", src.uuid);
728753
// (purl version, artifact leaf, integrity, other versions)
@@ -738,10 +763,9 @@ async fn emit_vendored(
738763
.into_iter()
739764
.find(|row| row.norm == norm);
740765
match locked {
741-
Some(row) => {
742-
let leaf = nupkg_leaf(&id_lower, &row.spelling);
743-
rows.push((row.spelling, leaf, row.integrity, other));
744-
}
766+
// The feed's own file, as on disk, whether or not the
767+
// lock pins it.
768+
Some(row) => rows.push((row.spelling, leaf.clone(), row.integrity, other)),
745769
// Locked at conflicting hashes: diagnosed, no ref.
746770
None if locks.versions.contains_key(&norm) => {}
747771
// Not locked at the patched version: the feed's own
@@ -2035,6 +2059,59 @@ mod tests {
20352059
assert!(out.unattested.is_empty(), "{:?}", out.unattested);
20362060
}
20372061

2062+
/// Review on #2007 (round 2): a vendored ref behaves as a hosted one
2063+
/// when the lock cannot be read (no ref: it might lock another version
2064+
/// the writers refuse), and names the feed's file as it is on disk
2065+
/// whether or not the lock pins it.
2066+
#[tokio::test]
2067+
async fn vendored_unreadable_lock_and_feed_leaf_spelling() {
2068+
let cfg = config(
2069+
&[ORG, (&socket_key(UUID_A), &feed(UUID_A))],
2070+
&[("nuget.org", "*"), (&socket_key(UUID_A), "Newtonsoft.Json")],
2071+
);
2072+
let nupkg = format!(".socket/vendor/nuget/{UUID_A}/newtonsoft.json.13.0.3.0.nupkg");
2073+
let p = Project::new();
2074+
p.write("nuget.config", &cfg);
2075+
p.write(&nupkg, b"PK");
2076+
p.write("packages.lock.json", "{ not json");
2077+
let out = run(&p).await;
2078+
assert_refs(&out, &[]);
2079+
2080+
let p = Project::new();
2081+
p.write("nuget.config", &cfg);
2082+
p.write(&nupkg, b"PK");
2083+
p.write(
2084+
"packages.lock.json",
2085+
lock(&[("net8.0", "Newtonsoft.Json", "13.0.3", Some(HASH))]),
2086+
);
2087+
let out = run(&p).await;
2088+
assert_eq!(out.refs.len(), 1, "{:?}", out.refs);
2089+
assert_eq!(out.refs[0].artifact_rel.as_deref(), Some(nupkg.as_str()));
2090+
}
2091+
2092+
/// Pre-release labels order by identifier, numerically where numeric.
2093+
#[test]
2094+
fn precedence_orders_prerelease_identifiers() {
2095+
let mut v = [
2096+
"1.0.0",
2097+
"1.0.0-rc.10",
2098+
"1.0.0-rc.2",
2099+
"1.0.0-beta",
2100+
"1.0.0-RC.3",
2101+
];
2102+
v.sort_by(|a, b| super::precedence(a).cmp(&super::precedence(b)));
2103+
assert_eq!(
2104+
v,
2105+
[
2106+
"1.0.0-beta",
2107+
"1.0.0-rc.2",
2108+
"1.0.0-RC.3",
2109+
"1.0.0-rc.10",
2110+
"1.0.0"
2111+
]
2112+
);
2113+
}
2114+
20382115
/// The vendored feed names the patched version (its single
20392116
/// `<idLower>.<versionNorm>.nupkg`): the ref is that version alone, and
20402117
/// a lock entry at any other version, beside the patched one or
@@ -2136,11 +2213,12 @@ mod tests {
21362213
assert!(out.unattested.is_empty(), "{:?}", out.unattested);
21372214
}
21382215

2139-
/// Versions are keyed through `normalize_nuget_version` of the trimmed
2140-
/// `resolved`, as the writers match them: `13.0.3`, ` 13.0.3` and
2141-
/// `13.0.3.0` in several target frameworks are ONE package, so one ref
2142-
/// whose pin is checked across all, never several refs with
2143-
/// independent pins.
2216+
/// Versions are keyed through `normalize_nuget_version` of the raw
2217+
/// `resolved`, as the writers match them: `13.0.3` and `13.0.3.0` in
2218+
/// several target frameworks are ONE package, so one ref whose pin is
2219+
/// checked across all, never several refs with independent pins. A
2220+
/// padded ` 13.0.3 ` is another version to the writers (they refuse
2221+
/// the lock), so it withholds the ref.
21442222
#[tokio::test]
21452223
async fn non_normalized_version_spellings_are_one_version() {
21462224
let cfg = config(
@@ -2153,7 +2231,6 @@ mod tests {
21532231
"packages.lock.json",
21542232
lock(&[
21552233
("net6.0", "Newtonsoft.Json", "13.0.3", Some(HASH)),
2156-
("net7.0", "Newtonsoft.Json", " 13.0.3 ", Some(HASH)),
21572234
("net8.0", "Newtonsoft.Json", "13.0.3.0", Some(HASH)),
21582235
]),
21592236
);
@@ -2170,6 +2247,24 @@ mod tests {
21702247
assert!(out.unattested.is_empty(), "{:?}", out.unattested);
21712248
assert!(out.diagnostics.is_empty(), "{:?}", out.diagnostics);
21722249

2250+
let p = Project::new();
2251+
p.write("nuget.config", &cfg);
2252+
p.write(
2253+
"packages.lock.json",
2254+
lock(&[
2255+
("net6.0", "Newtonsoft.Json", "13.0.3", Some(HASH)),
2256+
("net7.0", "Newtonsoft.Json", " 13.0.3 ", Some(HASH)),
2257+
]),
2258+
);
2259+
let out = run(&p).await;
2260+
assert!(
2261+
out.unattested
2262+
.iter()
2263+
.any(|u| u.purl == "pkg:nuget/newtonsoft.json@13.0.3"),
2264+
"{:?}",
2265+
out.unattested
2266+
);
2267+
21732268
// The two spellings pinned to different bytes: one package with
21742269
// conflicting contentHash values, which NuGet fails to restore. The
21752270
// diagnostic names the locks and the spellings.

0 commit comments

Comments
 (0)