Skip to content

Commit e426d50

Browse files
committed
Merge remote-tracking branch 'origin/lock-model/2a-pnpm-readers' into lock-model/2b-pnpm-planner
# Conflicts: # docs/design/lock-model-divergences.md
2 parents e61df3f + a303dd6 commit e426d50

6 files changed

Lines changed: 100 additions & 14 deletions

File tree

‎crates/socket-patch-cli/tests/in_process_redirect.rs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6075,6 +6075,53 @@ async fn pnpm_rollback_keeps_tarball_under_workspace_include_tarball_url() {
60756075
assert_eq!(pnpm_pin_and_rollback(tmp.path(), &server), pristine);
60766076
}
60776077

6078+
/// #2009 review: pnpm 11+ records a config dependency bare in the env
6079+
/// lockfile document even under `lockfileIncludeTarballUrl`, so a rollback
6080+
/// of a pin in both documents gives the project entry its `tarball:` back
6081+
/// and leaves the env entry bare. It used to write the project document's
6082+
/// `tarball:` into the env entry too, a lock pnpm never writes.
6083+
#[tokio::test]
6084+
#[serial]
6085+
async fn pnpm_rollback_keeps_the_env_document_bare_under_include_tarball_url() {
6086+
let server = MockServer::start().await;
6087+
mock_discovery(&server).await;
6088+
mock_reference(&server).await;
6089+
mock_view(&server).await;
6090+
mock_npm_registry(&server, "sha512-UPSTREAMupstream==", None).await;
6091+
let tarball = format!(
6092+
"{}/npm-registry/{NAME}/-/{NAME}-{VERSION}.tgz",
6093+
server.uri()
6094+
);
6095+
6096+
let tmp = tempfile::tempdir().unwrap();
6097+
let project = write_pnpm_tarball_project(tmp.path(), &tarball);
6098+
let pristine = format!(
6099+
"---\nlockfileVersion: '9.0'\n\nimporters:\n\n .:\n configDependencies:\n \
6100+
{NAME}:\n specifier: {VERSION}\n version: {VERSION}\n\npackages:\n\n \
6101+
{NAME}@{VERSION}:\n resolution: {{integrity: sha512-UPSTREAMupstream==}}\n\n\
6102+
snapshots:\n\n {NAME}@{VERSION}: {{}}\n\n---\n{project}"
6103+
);
6104+
std::fs::write(tmp.path().join("pnpm-lock.yaml"), &pristine).unwrap();
6105+
std::fs::write(
6106+
tmp.path().join("pnpm-workspace.yaml"),
6107+
"packages:\n - '.'\nlockfileIncludeTarballUrl: true\n",
6108+
)
6109+
.unwrap();
6110+
let pinned_twice = |root: &Path| {
6111+
let lock = std::fs::read_to_string(root.join("pnpm-lock.yaml")).unwrap();
6112+
lock.matches("patch.test").count()
6113+
};
6114+
let env = run_redirect_subprocess(tmp.path(), &server.uri());
6115+
assert_eq!(vlt_hosted_common::redirected(&env), 1, "{env:#}");
6116+
assert_eq!(pinned_twice(tmp.path()), 2, "both documents are pinned");
6117+
let (code, env) = rollback_json(tmp.path(), &server);
6118+
assert_eq!(code, Some(0), "rollback: {env:#}");
6119+
assert_eq!(
6120+
std::fs::read_to_string(tmp.path().join("pnpm-lock.yaml")).unwrap(),
6121+
pristine
6122+
);
6123+
}
6124+
60786125
/// #557, the other case pnpm records `tarball:` in: the registry's
60796126
/// `dist.tarball` is not the conventional URL pnpm would derive, so pnpm
60806127
/// keeps it even without the setting.

‎crates/socket-patch-core/src/patch/redirect/upstream/npm.rs‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,10 +1965,22 @@ pub(crate) async fn restore_pnpm_locks(
19651965
let mut splices: Vec<(std::ops::Range<usize>, String)> = Vec::new();
19661966
// (uuid, name@version whose `tarball:` only the guessed setting decided)
19671967
let mut handled: Vec<(String, Option<String>)> = Vec::new();
1968+
// pnpm 11+ records the env lockfile document's resolutions bare
1969+
// whatever `lockfileIncludeTarballUrl` says (see
1970+
// `pnpm_tarball_policy`): only a URL it cannot derive gets
1971+
// `tarball:` there.
1972+
let env_spans: Vec<std::ops::Range<usize>> = pnpm::documents(&text)
1973+
.into_iter()
1974+
.filter(|(_, document)| *document == pnpm::PnpmDocument::Env)
1975+
.map(|(span, _)| span)
1976+
.collect();
19681977
for entry in pnpm::entries(&text) {
19691978
let Some(resolution) = pnpm::resolution(&entry) else {
19701979
continue;
19711980
};
1981+
let in_env = env_spans
1982+
.iter()
1983+
.any(|span| span.contains(&resolution.range.start));
19721984
let Some((_, uuid, name, version)) = hits.iter().find(|(r, ..)| *r == resolution.range)
19731985
else {
19741986
continue;
@@ -1990,13 +2002,15 @@ pub(crate) async fn restore_pnpm_locks(
19902002
// pnpm records `tarball:` under lockfileIncludeTarballUrl and for
19912003
// a URL it cannot derive from the registry (#557).
19922004
let derived = policy.derives_tarball(name, version, &dist.tarball, found.from_project);
1993-
let restored = if policy.always || !derived {
2005+
let restored = if (policy.always && !in_env) || !derived {
19942006
resolution.rewrite(integrity, &dist.tarball)
19952007
} else {
19962008
resolution.restore(integrity)
19972009
};
1998-
// A URL pnpm records anyway is no guess.
1999-
let guessed = (policy.guess.is_some() && derived).then(|| format!("{name}@{version}"));
2010+
// A URL pnpm records anyway is no guess, and neither is an env
2011+
// document entry.
2012+
let guessed =
2013+
(policy.guess.is_some() && derived && !in_env).then(|| format!("{name}@{version}"));
20002014
splices.push((resolution.range.clone(), restored));
20012015
handled.push((uuid.clone(), guessed));
20022016
}

‎crates/socket-patch-core/src/vendor/lock_inventory/pnpm.rs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,12 @@ fn pnpm_lock_text_inventory(text: &str) -> Option<Vec<LockfileEntry>> {
122122
/// the source lock into common/temp and runs pnpm there.
123123
///
124124
/// Called (via `npm_family::inventory_npm_trees_raw_in`) only when
125-
/// `rush.json` is present, which that caller checks once through the
126-
/// view; the locks are read beside whatever lock the router picks. Outside
127-
/// a Rush monorepo ([`is_rush`]) it reads nothing. The locks are the ones
125+
/// `rush.json` is present ([`is_rush`]), which that caller checks once
126+
/// through the view, so this reader does not stat it again; the locks are
127+
/// read beside whatever lock the router picks. The locks are the ones
128128
/// lockfile discovery reads ([`rush_lock_rels`]), each read through the
129129
/// view. Missing files/dirs are skipped fail-soft.
130130
pub(super) async fn inventory_rush_pnpm_locks_in(view: &ProjectView<'_>) -> Vec<LockfileEntry> {
131-
if !is_rush(*view).await {
132-
return Vec::new();
133-
}
134131
let mut out = Vec::new();
135132
for rel in rush_lock_rels(*view).await {
136133
if let Some(entries) = inventory_pnpm_lock_rel_in(view, &rel).await {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,7 @@ impl Discovery {
13081308
/// and the project ref then stands in its place, see
13091309
/// [`Discovery::drop_env_twins`]).
13101310
fn contest_within_locks(&mut self) {
1311+
self.drop_env_twins();
13111312
self.expand_unidentified_copies();
13121313
if self.unpatched_copies.is_empty() && self.refs.iter().all(|r| r.part == TreePart::File) {
13131314
return;
@@ -1491,9 +1492,12 @@ impl Discovery {
14911492
/// file's project document also wires (the same `(purl, uuid, mode)`):
14921493
/// one wiring of the package in that file, whose project instance is
14931494
/// the one an installed `node_modules` copy is verified against, even
1494-
/// when the two pins differ. The project twin counts wherever it ended
1495-
/// up (`refs`, `shadowed` or `rewirable`), so a withheld project ref
1496-
/// never leaves its env twin attested in its place.
1495+
/// when the two pins differ. Runs before any contest
1496+
/// ([`Discovery::contest_within_locks`]), so the twin leaves no
1497+
/// diagnostic or [`Unattested`] mark (keyed by `(purl, uuid)`, which
1498+
/// would withhold the project ref) behind, and a withheld project ref
1499+
/// never leaves it attested in its place. The project twin counts
1500+
/// wherever an extractor put it (`refs`, `shadowed` or `rewirable`).
14971501
fn drop_env_twins(&mut self) {
14981502
let project: HashSet<(PathBuf, String, String, WiringMode)> =
14991503
[&self.refs, &self.shadowed, &self.rewirable]
@@ -1687,7 +1691,6 @@ impl Discovery {
16871691
self.unattested.dedup();
16881692
self.contested.sort();
16891693
self.contested.dedup();
1690-
self.drop_env_twins();
16911694
for refs in [&mut self.refs, &mut self.shadowed, &mut self.rewirable] {
16921695
refs.sort_by(|a, b| {
16931696
(&a.source_file, &a.purl, &a.uuid, a.mode, a.part).cmp(&(

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,6 +3624,31 @@ mod tests {
36243624
out.hosted_claim("pkg:npm/left-pad@1.3.0", UUID_A),
36253625
Some(true)
36263626
);
3627+
// The dropped twin leaves no diagnostic of its own behind.
3628+
assert!(
3629+
out.diagnostics
3630+
.iter()
3631+
.all(|d| !d.detail.contains("(env lockfile)")),
3632+
"{:#?}",
3633+
out.diagnostics
3634+
);
3635+
3636+
// Wired in both documents, the env document bundling a copy: its
3637+
// twin is dropped before any mark, so the project ref (whose tree
3638+
// the env document's copies never reach) is attested.
3639+
let p = Project::new();
3640+
p.write(
3641+
"pnpm-lock.yaml",
3642+
format!(
3643+
"{}{}",
3644+
env(&format!("{env_wired}{bundle}")),
3645+
project(&wired)
3646+
),
3647+
);
3648+
let out = p.discover().await;
3649+
assert_refs(&out, &hosted);
3650+
assert_eq!(out.refs[0].part, TreePart::File);
3651+
assert!(out.unattested.is_empty(), "{:#?}", out.unattested);
36273652

36283653
// The env document's registry entry contests no other lock's ref.
36293654
let npm_lock = format!(

‎docs/design/lock-model-divergences.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ follow from the install-tree decision. N11 was decided on 2026-11-05.
8484
| N1 | pnpm `gitBranchLockfile` with a branch lock present: is the root lock the install tree? | Hosted withholds the root locks and refuses (`hosted/engine.rs` ~930). Vendored refuses. Discovery and the inventory never check the setting. | Hosted. Discovery withholds pnpm root and member-lock attestations whenever any `pnpm-lock.<branch>.yaml` exists, whatever the settings or environment say (owner, 2026-11-05; #2010 (draft)). | false attestation |
8585
| N2 | npm user patches (#711) and `bun patch` (#367) | Hosted skips that one dependency and records it as refused. Vendored refuses the whole npm v4 lock, or the one bun dependency. Discovery never reads `patchedDependencies` / `"patched"` and attests a pre-#711 hosted pin. | Hosted's per-dependency skip, in discovery too, for every package manager's user patches (owner decision; #2010 (draft) for npm and bun, #2018 (draft) for pnpm, yarn and vlt). The override is the existing `--allow-patch-conflicts`; `vex` never attests under a user patch, even with it. | false attestation |
8686
| N3 | A workspace member's stray npm/bun/vlt lock | Discovery treats it as absent. Hosted and vendored refuse. The inventory reads it as the project's lock. | The inventory ignores it | |
87-
| N4 | pnpm 11's env-lockfile document | See [Decisions](lock-model-unification.md#decisions-under-the-hosted-rule). `main_document` takes the text after the last `---`, so a crafted multi-document lock chooses its own "project". | Its own tree; documents identified by content, not position. **Resolved in PR 2a (#2009)**: every reader reads both documents through `PnpmLock`, each entry tagged by document; discovery records carry the env document's install tree (`TreePart::PnpmEnv`) beside the real lock path, and that tree neither contests nor is contested by another lock, and never contests the project document; the project document's registry entry, `file:` copy or bundled copy of the same release does withhold an env ref (owner decision on #2009: a VEX statement covers the release, not one tree); the env document is the FIRST document when its importers carry only `configDependencies` / `packageManagerDependencies` (pnpm's own position rule), and every other document is read as the project. | |
87+
| N4 | pnpm 11's env-lockfile document | See [Decisions](lock-model-unification.md#decisions-under-the-hosted-rule). `main_document` takes the text after the last `---`, so a crafted multi-document lock chooses its own "project". | Its own tree; documents identified by position and content (the env document is the first one, and only when its importers say so). **Resolved in PR 2a (#2009)**: every reader reads both documents through `PnpmLock`, each entry tagged by document; discovery records carry the env document's install tree (`TreePart::PnpmEnv`) beside the real lock path, and that tree neither contests nor is contested by another lock, and never contests the project document; the project document's registry entry, `file:` copy or bundled copy of the same release does withhold an env ref (owner decision on #2009: a VEX statement covers the release, not one tree); the env document is the FIRST document when its importers carry only `configDependencies` / `packageManagerDependencies` (pnpm's own position rule), and every other document is read as the project. | |
8888
| N5 | What counts as Socket wiring | Discovery and the unwind use the allowlist. The inventory records hosted URLs on any host. `parse_vendor_path` accepts `.socket/vendor/` anywhere, so a `../.socket/vendor/…` entry from a sibling checkout is "ours" to the vendored writer and reverter. The hosted pnpm "vendored?" check is a substring heuristic. | The `origin` classifier everywhere. The vendored writers, reverters and GC claim only this project's vendor paths in #2025 (draft). pnpm part **resolved in PR 2a (#2009)**: the hosted pnpm "vendored?" check reads the rekeyed packages entry with `origin::vendor_ref`. **PR 2b (#2029)**: both modes read pnpm keys with one rule, `formats::pnpm::plan::match_key` (`origin::vendor_ref` plus the artifact leaf), so the vendored planner no longer takes a sibling checkout's rekey for its own. The vendored reverter still finds its wiring with `parse_vendor_path` (values such as pnpm 7/8's absolute specifier need it). | revert could rewrite another project's wiring |
8989
| N6 | `npm-shrinkwrap.json` and `package-lock.json` | Hosted, vendored and discovery read both, with a contest. The inventory reads the shrinkwrap only. | Both, everywhere | |
9090
| N7 | npm lockfileVersion | Hosted rewrites v1 and v4. Vendored accepts v2/v3 only. The inventory ignores v1. Discovery reads v1 and v2+. | Hosted, once vendored `file:` wiring under npm 6 and npm 12.1 v4 has fixtures | |

0 commit comments

Comments
 (0)