Skip to content

Commit b4224f0

Browse files
mikolalysenkoclaude
andcommitted
Stop attesting npm-family pins pnpm or a user patch overrides
VEX discovery attested two kinds of npm-family pins that the hosted and vendored writers refuse, so the statement could cover bytes the install never uses. N1, pnpm gitBranchLockfile (#556): with the setting on and a pnpm-lock.<branch>.yaml present, pnpm installs the branch from that lock. Hosted withholds the root and member locks and refuses every pnpm pin; vendored refuses the project. Discovery now runs the same detection (utils::pnpm_workspace::git_branch_locks, via a new git_branch_locks_in that skips the environment, as discovery already does for the lockfile setting) and, only while the lockfile setting is on and outside Rush, marks every ref of the root and member pnpm locks UnattestedKind::PnpmGitBranchLock. N2, user-patched npm / Bun packages (#711, #367): hosted leaves a package the project patches itself unwired and never confirms it; vendored refuses. Hosted's npm gate is factored into patch::redirect::NpmUserPatches (package.json patchedDependencies keys plus non-null lock-entry `patched` records in any present npm lock), which the hosted rewriter and npm discovery now share. Bun discovery applies the rewriters' per-dependency rule (bun_lock_text::patched_dependency_key over the manifest keys, plus bun.lock's mirrored keys for the text lock) against each ref's hosted URL or vendored tarball. Matching refs are marked UnattestedKind::UserPatchedDependency. Both keep the ref (rollback / remove / list still find a pre-fix pin and the ledgers keep the wiring live, since no re-run can clear the evidence) and only vex omits it, as vex_pnpm_git_branch_lockfile / vex_npm_patched_dependency. CLI_CONTRACT.md documents both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7bde3ba commit b4224f0

7 files changed

Lines changed: 586 additions & 52 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Large diffs are not rendered by default.

‎crates/socket-patch-cli/src/commands/vex_sources.rs‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ const NOTE_MAVEN_REACTOR_ROOT: &str = "vex_maven_reactor_root";
232232
/// classifier copy at another version, which the pin does not reach
233233
/// (`vex::Unattested`).
234234
const NOTE_MAVEN_CLASSIFIER_UNPATCHED: &str = "vex_maven_classifier_unpatched";
235+
/// Omission tag and note: a pnpm lock pins the patch, but `gitBranchLockfile`
236+
/// makes pnpm install from a `pnpm-lock.<branch>.yaml` (`vex::Unattested`).
237+
const NOTE_PNPM_GIT_BRANCH_LOCKFILE: &str = "vex_pnpm_git_branch_lockfile";
238+
/// Omission tag and note: an npm or Bun pin's package is one the project
239+
/// patches itself (`patchedDependencies`, `vex::Unattested`).
240+
const NOTE_NPM_PATCHED_DEPENDENCY: &str = "vex_npm_patched_dependency";
235241

236242
/// The omission tag of an [`Unattested`](socket_patch_core::vex::Unattested)
237243
/// ref, and the remedy its note ends with.
@@ -268,6 +274,17 @@ fn unattested_note(kind: UnattestedKind) -> (&'static str, &'static str) {
268274
"not attested while that classifier copy resolves an unpatched version; the \
269275
wiring itself is intact, so re-running `scan` does not change this",
270276
),
277+
UnattestedKind::PnpmGitBranchLock => (
278+
NOTE_PNPM_GIT_BRANCH_LOCKFILE,
279+
"not attested while branch locks are in use; the wiring itself is intact, so \
280+
re-running `scan` / `vendor` does not change this (turn gitBranchLockfile off and \
281+
fold the branch locks in with `pnpm install --merge-git-branch-lockfiles`)",
282+
),
283+
UnattestedKind::UserPatchedDependency => (
284+
NOTE_NPM_PATCHED_DEPENDENCY,
285+
"not attested while the project patches the package itself (fold the Socket fix \
286+
into your own patch, or remove the `patchedDependencies` entry and re-run)",
287+
),
271288
}
272289
}
273290

@@ -418,7 +435,9 @@ pub(crate) async fn plan(
418435
// Wired, but a build bypasses the pin (`Unattested`: a Gradle lock
419436
// above the hosted base resolves the newer upstream release, a pnpm
420437
// bundled copy, a deno.lock copy, an npm shrinkwrap with no
421-
// package-lock.json twin, which npm >= 12 ignores): the ref keeps
438+
// package-lock.json twin, which npm >= 12 ignores, a pnpm lock
439+
// `gitBranchLockfile` bypasses, a package the project patches itself):
440+
// the ref keeps
422441
// rollback, remove, list and the ledgers' liveness working, and the
423442
// patch is omitted.
424443
cands.retain(|c| {
@@ -1762,6 +1781,33 @@ mod tests {
17621781
assert!(live.hosted.contains_key(PURL));
17631782
}
17641783

1784+
/// Each npm-family bypass (#556 branch locks, #711 / #367 user patches)
1785+
/// is omitted under its own tag, distinct from every other kind's.
1786+
#[test]
1787+
fn npm_family_unattested_kinds_have_their_own_tags() {
1788+
let kinds = [
1789+
UnattestedKind::LockAboveBase,
1790+
UnattestedKind::BundledCopy,
1791+
UnattestedKind::DenoLock,
1792+
UnattestedKind::NpmShrinkwrapOnly,
1793+
UnattestedKind::MavenReactorRoot,
1794+
UnattestedKind::MavenClassifierUnpatched,
1795+
UnattestedKind::PnpmGitBranchLock,
1796+
UnattestedKind::UserPatchedDependency,
1797+
];
1798+
let tags: std::collections::BTreeSet<&str> =
1799+
kinds.iter().map(|k| unattested_note(*k).0).collect();
1800+
assert_eq!(tags.len(), kinds.len(), "{tags:?}");
1801+
assert_eq!(
1802+
unattested_note(UnattestedKind::PnpmGitBranchLock).0,
1803+
"vex_pnpm_git_branch_lockfile"
1804+
);
1805+
assert_eq!(
1806+
unattested_note(UnattestedKind::UserPatchedDependency).0,
1807+
"vex_npm_patched_dependency"
1808+
);
1809+
}
1810+
17651811
/// Ledger-only records with no wiring anywhere are gated, except the
17661812
/// manifest-owned one, which falls back to agent-mode verification; an
17671813
/// in-run confirmed purl is live by construction.

‎crates/socket-patch-core/src/patch/redirect/mod.rs‎

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,52 +1302,22 @@ fn rewrite_npm_lock(
13021302
// would still stack the user's diff on the hosted bytes. Each present
13031303
// lock is parsed ONCE, here, and the parse is handed to its rewrite;
13041304
// only a lock that spells `"patched"` at all is walked for the gate.
1305-
let user_patched = crate::vendor::bun_lock_text::patched_dependency_keys(
1306-
files.get("package.json").map(String::as_str),
1307-
None,
1308-
);
1305+
let mut user_patches =
1306+
NpmUserPatches::from_manifest(files.get("package.json").map(String::as_str));
13091307
let parsed: Vec<(&str, Option<Value>)> = present
13101308
.iter()
13111309
.map(|lockfile| (*lockfile, parse_json_text(&files[*lockfile]).ok()))
13121310
.collect();
1313-
let lock_patched: Vec<(String, String, &str, String)> = parsed
1314-
.iter()
1315-
.filter(|(lockfile, _)| files[*lockfile].contains("\"patched\""))
1316-
.filter_map(|(lockfile, lock)| Some((*lockfile, lock.as_ref()?)))
1317-
.flat_map(|(lockfile, lock)| {
1318-
npm_lock_entries(lock)
1319-
.into_iter()
1320-
.filter(|e| e.section == NpmLockSection::Packages && e.is_dependency())
1321-
.filter(|e| e.value.get("patched").is_some_and(|p| !p.is_null()))
1322-
.filter_map(|e| {
1323-
let version = e.node.version?.to_string();
1324-
Some((
1325-
e.node.name.to_string(),
1326-
version,
1327-
lockfile,
1328-
e.key.to_string(),
1329-
))
1330-
})
1331-
.collect::<Vec<_>>()
1332-
})
1333-
.collect();
1311+
for (lockfile, lock) in &parsed {
1312+
if let (Some(lock), true) = (lock, files[*lockfile].contains("\"patched\"")) {
1313+
user_patches.add_lock(lockfile, lock);
1314+
}
1315+
}
13341316
let npm: Vec<&DepOverride> = npm
13351317
.into_iter()
13361318
.filter(|dep| {
13371319
let fname = full_name(dep);
1338-
let source = if let Some(key) = crate::vendor::bun_lock_text::patched_dependency_key(
1339-
&user_patched,
1340-
&fname,
1341-
&dep.version,
1342-
&dep.artifact_url,
1343-
) {
1344-
format!("package.json `patchedDependencies` has `{key}`")
1345-
} else if let Some((_, _, lockfile, key)) = lock_patched
1346-
.iter()
1347-
.find(|(name, version, _, _)| *name == fname && *version == dep.version)
1348-
{
1349-
format!("{lockfile} entry `{key}` carries npm's `patched` record")
1350-
} else {
1320+
let Some(source) = user_patches.source(&fname, &dep.version, &dep.artifact_url) else {
13511321
return true;
13521322
};
13531323
// A lock an earlier (pre-#711) hosted run already pinned keeps
@@ -1440,6 +1410,70 @@ pub fn npm_deno_lock_detail(packages: &[String], how: &str) -> String {
14401410
)
14411411
}
14421412

1413+
/// The packages a project patches itself with npm 12's native `npm patch`
1414+
/// (#711), as the hosted npm rewriter's gate reads them: a root
1415+
/// `package.json` `patchedDependencies` key, or the non-null `patched`
1416+
/// record npm writes on a lock's `packages` dependency entry (lockfileVersion
1417+
/// 4) in any present npm lock. npm applies that diff to every install of the
1418+
/// package, whichever lock names it. `vex` discovery reads the same
1419+
/// evidence, so it never attests a pin hosted mode refuses.
1420+
#[derive(Debug, Default)]
1421+
pub(crate) struct NpmUserPatches {
1422+
/// The root manifest's `patchedDependencies` keys.
1423+
keys: Vec<String>,
1424+
/// `(name, version, lockfile, entry key)` of every lock entry carrying
1425+
/// a `patched` record.
1426+
locked: Vec<(String, String, String, String)>,
1427+
}
1428+
1429+
impl NpmUserPatches {
1430+
/// The root `package.json`'s `patchedDependencies` keys (none when
1431+
/// `manifest` is absent or unparseable).
1432+
pub(crate) fn from_manifest(manifest: Option<&str>) -> Self {
1433+
NpmUserPatches {
1434+
keys: crate::vendor::bun_lock_text::patched_dependency_keys(manifest, None),
1435+
locked: Vec::new(),
1436+
}
1437+
}
1438+
1439+
/// Add the `patched` records of the parsed npm lock `lockfile`.
1440+
pub(crate) fn add_lock(&mut self, lockfile: &str, lock: &Value) {
1441+
let patched = npm_lock_entries(lock)
1442+
.into_iter()
1443+
.filter(|e| e.section == NpmLockSection::Packages && e.is_dependency())
1444+
.filter(|e| e.value.get("patched").is_some_and(|p| !p.is_null()))
1445+
.filter_map(|e| {
1446+
let version = e.node.version?.to_string();
1447+
Some((
1448+
e.node.name.to_string(),
1449+
version,
1450+
lockfile.to_string(),
1451+
e.key.to_string(),
1452+
))
1453+
});
1454+
self.locked.extend(patched);
1455+
}
1456+
1457+
/// Why `name@version` counts as patched by the project itself, for a
1458+
/// warning or diagnostic, when it does. `target` is what the wiring
1459+
/// resolves the package to (the hosted URL, or the vendored tarball
1460+
/// path): a `patchedDependencies` key naming exactly it is no conflict
1461+
/// ([`crate::vendor::bun_lock_text::patched_dependency_key`]).
1462+
pub(crate) fn source(&self, name: &str, version: &str, target: &str) -> Option<String> {
1463+
if let Some(key) =
1464+
crate::vendor::bun_lock_text::patched_dependency_key(&self.keys, name, version, target)
1465+
{
1466+
return Some(format!("package.json `patchedDependencies` has `{key}`"));
1467+
}
1468+
self.locked
1469+
.iter()
1470+
.find(|(n, v, _, _)| n == name && v == version)
1471+
.map(|(_, _, lockfile, key)| {
1472+
format!("{lockfile} entry `{key}` carries npm's `patched` record")
1473+
})
1474+
}
1475+
}
1476+
14431477
/// Leave `dep` on its registry entry because the project patches it with
14441478
/// npm 12's native `npm patch` (#711). npm extracts whatever tarball the
14451479
/// lock names and then applies the user's diff, failing the whole install

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,14 @@ pub fn is_git_branch_lock_name(name: &str) -> bool {
775775
/// on pnpm 11+, `npm_config_git_branch_lockfile` on pnpm 10 and older,
776776
/// see [`env_setting`]).
777777
pub async fn git_branch_locks(view: &ProjectView<'_>) -> Option<GitBranchLocks> {
778+
git_branch_locks_in(view, true).await
779+
}
780+
781+
/// [`git_branch_locks`], consulting the environment only with `env`: the
782+
/// hosted and vendored writers run in the install's environment, while
783+
/// `vex` discovery answers from the project's files alone (as it does for
784+
/// [`lockfile_disabled`]).
785+
pub async fn git_branch_locks_in(view: &ProjectView<'_>, env: bool) -> Option<GitBranchLocks> {
778786
let mut locks = branch_lock_names(view, "").await;
779787
let mut members_unlisted = None;
780788
match workspace_members(view).await {
@@ -808,7 +816,7 @@ pub async fn git_branch_locks(view: &ProjectView<'_>) -> Option<GitBranchLocks>
808816
});
809817
}
810818
}
811-
if setting.is_none() {
819+
if setting.is_none() && env {
812820
setting = view_setting(view, None, None, KEYS.0, KEYS.1).await;
813821
}
814822
let setting = git_branch_on(setting)?;

0 commit comments

Comments
 (0)