Skip to content

Commit 8b59a5a

Browse files
mikolalysenkoclaude
andcommitted
Address the second code review of the pnpm planner PR
- The hosted confirmation probe skips a pnpm lock pnpm does not read (N11): a stale Socket URL in a shadowed shrinkwrap.yaml confirmed the dependency, which the attribution gate then dropped as redirect_unattributable. - The pnpm trust/heal post-pass takes the governing root lock from planned_locks, so under a pnpm 1 / 2 pin a stray v9 pnpm-lock.yaml no longer plans a trustLockfile write. - Mixed line endings get their own error type instead of overloading io::ErrorKind::Unsupported, which an OS ENOTSUP read also returns; read_pnpm_text normalizes through utils::line_endings::to_lf. - The pnpm pin is read only when both pnpm-lock.yaml and shrinkwrap.yaml exist (planned_locks, vendoring, discovery). - Docs: divergences N16 names the two checks; read_project refuses mixed endings, not CRLF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ac8e9e0 commit 8b59a5a

5 files changed

Lines changed: 148 additions & 19 deletions

File tree

‎crates/socket-patch-core/src/formats/pnpm/plan.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,19 @@ pub(crate) fn governing_pnpm_lock(
100100
pub(crate) fn planned_locks(files: &BTreeMap<String, String>) -> Vec<&String> {
101101
let planned = |key: &&String| {
102102
let leaf = key.rsplit('/').next().unwrap_or(key);
103+
if !matches!(leaf, PNPM_LOCK | PNPM_SHRINKWRAP_LEGACY) {
104+
return false;
105+
}
103106
// `dir` keeps its trailing `/` (empty at the root).
104107
let in_dir = |rel: &str| format!("{}{rel}", &key[..key.len() - leaf.len()]);
105-
let pinned = pinned_pnpm_major(|rel| files.get(&in_dir(rel)).cloned());
106108
let has = |rel: &str| files.contains_key(&in_dir(rel));
107-
matches!(leaf, PNPM_LOCK | PNPM_SHRINKWRAP_LEGACY)
108-
&& governing_pnpm_lock(has(PNPM_LOCK), has(PNPM_SHRINKWRAP_LEGACY), pinned)
109-
== Some(leaf)
109+
let both = (has(PNPM_LOCK), has(PNPM_SHRINKWRAP_LEGACY));
110+
// The pin decides only between the two, so it is read only then.
111+
let pinned = match both {
112+
(true, true) => pinned_pnpm_major(|rel| files.get(&in_dir(rel)).cloned()),
113+
_ => None,
114+
};
115+
governing_pnpm_lock(both.0, both.1, pinned) == Some(leaf)
110116
};
111117
files.keys().filter(planned).collect()
112118
}

‎crates/socket-patch-core/src/hosted/engine.rs‎

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,8 +2693,19 @@ fn pnpm_trust(
26932693
.count();
26942694
// The locks pnpm installs the project from: the root lock, plus each
26952695
// member's own under `sharedWorkspaceLockfile: false` (#492). A Rush
2696-
// lock is never one: rush installs it from common/temp.
2697-
let governing: Vec<&str> = std::iter::once("pnpm-lock.yaml")
2696+
// lock is never one: rush installs it from common/temp. The root lock
2697+
// is the one pnpm reads (N11): `shrinkwrap.yaml` under a pnpm 1 / 2
2698+
// pin, so a stray `pnpm-lock.yaml` never drives the heal or the trust
2699+
// config.
2700+
let planned = crate::formats::pnpm::plan::planned_locks(files);
2701+
let root_lock = [
2702+
crate::constants::npm_family::PNPM_LOCK,
2703+
crate::constants::npm_family::PNPM_SHRINKWRAP_LEGACY,
2704+
]
2705+
.into_iter()
2706+
.find(|lock| planned.iter().any(|key| key.as_str() == *lock))
2707+
.unwrap_or(crate::constants::npm_family::PNPM_LOCK);
2708+
let governing: Vec<&str> = std::iter::once(root_lock)
26982709
.chain(member_lock_keys.iter().map(String::as_str))
26992710
.filter(|key| !rush_lock_keys.iter().any(|rush| rush == key))
27002711
.collect();
@@ -3334,9 +3345,24 @@ fn confirm(
33343345
name.ends_with("/pom.xml")
33353346
|| maven_reactor_refused && (name == "pom.xml" || name.starts_with(".mvn/"))
33363347
};
3348+
// A pnpm lock pnpm does not read (a `shrinkwrap.yaml` beside a
3349+
// `pnpm-lock.yaml`, or the reverse under a pnpm 1 / 2 pin, N11) is
3350+
// never rewritten, so a Socket URL an earlier run left in it pins
3351+
// nothing either.
3352+
let planned_pnpm = crate::formats::pnpm::plan::planned_locks(files);
3353+
let is_shadowed_pnpm = |name: &str| {
3354+
matches!(
3355+
name.rsplit('/').next(),
3356+
Some(
3357+
crate::constants::npm_family::PNPM_LOCK
3358+
| crate::constants::npm_family::PNPM_SHRINKWRAP_LEGACY
3359+
)
3360+
) && !planned_pnpm.iter().any(|planned| planned.as_str() == name)
3361+
};
33373362
let final_texts: Vec<(&str, &String)> = files
33383363
.iter()
33393364
.filter(|(name, _)| !(pdm_inactive && name.as_str() == "pdm.lock"))
3365+
.filter(|(name, _)| !is_shadowed_pnpm(name))
33403366
.filter(|(name, _)| !is_npm_manifest(name))
33413367
.filter(|(name, _)| !is_unpinned_maven(name))
33423368
.filter(|(name, _)| !crate::patch::redirect::sbt::is_synthetic_key(name))
@@ -4579,6 +4605,72 @@ mod tests {
45794605
);
45804606
}
45814607

4608+
/// N11 review: under a pnpm 1 / 2 pin `shrinkwrap.yaml` governs, so a
4609+
/// stray v9 `pnpm-lock.yaml` an earlier run redirected is no heal root
4610+
/// and plans no `trustLockfile` write for a lock pnpm never reads.
4611+
#[tokio::test]
4612+
async fn a_shadowed_pnpm_lock_is_no_heal_root() {
4613+
let redirected = LEFT_PAD_V9_LOCK.replace(
4614+
"resolution: {integrity: sha512-UPSTREAM==}",
4615+
&format!(
4616+
"resolution: {{integrity: sha512-PATCHED==, tarball: {}}}",
4617+
left_pad_url()
4618+
),
4619+
);
4620+
let mut p = MemoryProject::new();
4621+
p.insert_text("pnpm-lock.yaml", redirected.as_str());
4622+
p.insert_text(
4623+
"shrinkwrap.yaml",
4624+
"shrinkwrapMinorVersion: 9\nshrinkwrapVersion: 3\n",
4625+
);
4626+
p.insert_text(
4627+
"package.json",
4628+
r#"{"name":"app","packageManager":"pnpm@2.25.7"}"#,
4629+
);
4630+
let (_, done) = npm_rewrite(&ProjectView::Memory(&p), &BTreeSet::new()).await;
4631+
assert!(
4632+
!done.rewrite.files.contains_key(PNPM_WORKSPACE_REL),
4633+
"{:?}",
4634+
done.rewrite.files.keys()
4635+
);
4636+
4637+
// Without the pin, pnpm-lock.yaml governs and the heal plans it.
4638+
p.insert_text("package.json", r#"{"name":"app"}"#);
4639+
let (_, done) = npm_rewrite(&ProjectView::Memory(&p), &BTreeSet::new()).await;
4640+
assert!(
4641+
done.rewrite.files.contains_key(PNPM_WORKSPACE_REL),
4642+
"{:?}",
4643+
done.rewrite.files.keys()
4644+
);
4645+
}
4646+
4647+
/// N11 review: a shadowed `shrinkwrap.yaml` still carrying an earlier
4648+
/// run's Socket URL confirms nothing when the governing
4649+
/// `pnpm-lock.yaml` no longer pins the package. It used to confirm by
4650+
/// substring, and only the attribution gate then dropped the dependency
4651+
/// as `redirect_unattributable`, blaming the project's lockfiles.
4652+
#[tokio::test]
4653+
async fn a_shadowed_pnpm_lock_confirms_nothing() {
4654+
let mut p = MemoryProject::new();
4655+
p.insert_text(
4656+
"pnpm-lock.yaml",
4657+
"lockfileVersion: '9.0'\n\npackages:\n other@1.0.0:\n \
4658+
resolution: {integrity: sha512-OTHER==}\n",
4659+
);
4660+
p.insert_text(
4661+
"shrinkwrap.yaml",
4662+
format!(
4663+
"shrinkwrapVersion: 3\nshrinkwrapMinorVersion: 9\npackages:\n /left-pad/1.3.0:\n \
4664+
resolution:\n integrity: sha512-PATCHED==\n tarball: {}\n",
4665+
left_pad_url()
4666+
)
4667+
.as_str(),
4668+
);
4669+
let (_, done) = npm_rewrite(&ProjectView::Memory(&p), &BTreeSet::new()).await;
4670+
assert!(done.confirmed.is_empty(), "{:?}", done.confirmed);
4671+
assert!(done.unattributed.is_empty(), "{:?}", done.unattributed);
4672+
}
4673+
45824674
#[tokio::test]
45834675
async fn a_pnpm_lock_pair_reads_the_package_manager_pin() {
45844676
// pnpm 2 pinned in package.json, no install record: pnpm reads

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,8 @@ pub(super) async fn git_branch_lock_refusal(
550550
}
551551

552552
/// Read the pair, refusing (before any write) a file that is missing,
553-
/// unreadable, not the shape the surgery has fixtures for, or CRLF.
553+
/// unreadable, not the shape the surgery has fixtures for, or that mixes
554+
/// CRLF and LF line endings ([`read_pnpm_text`]).
554555
async fn read_project(
555556
project_root: &Path,
556557
dialect: PnpmDialect,
@@ -584,8 +585,12 @@ async fn read_project(
584585
// pnpm 1 / 2 read only `shrinkwrap.yaml`, whose grammar the rekey has no
585586
// fixture for: there `pnpm-lock.yaml` is stray, and wiring it would
586587
// patch nothing pnpm installs (N11, the choice hosted and discovery make).
588+
// The pin matters only beside a shrinkwrap, so it is read only then.
587589
let shrinkwrap = view.exists_no_follow(PNPM_SHRINKWRAP_LEGACY).await;
588-
let pinned = crate::utils::pnpm_workspace::pinned_pnpm_major_in(&view).await;
590+
let pinned = match shrinkwrap {
591+
true => crate::utils::pnpm_workspace::pinned_pnpm_major_in(&view).await,
592+
false => None,
593+
};
589594
if governing_pnpm_lock(true, shrinkwrap, pinned) == Some(PNPM_SHRINKWRAP_LEGACY) {
590595
let why = "the project pins pnpm 1 / 2, which installs from shrinkwrap.yaml and ignores \
591596
pnpm-lock.yaml; vendored mode does not write that grammar — upgrade pnpm \
@@ -703,29 +708,51 @@ async fn read_project(
703708
/// endings it is written back with. A CRLF file (a Windows autocrlf
704709
/// checkout) is edited like its LF twin and written back CRLF, as hosted
705710
/// mode's byte splices keep it (divergences N15). A file that mixes endings
706-
/// is an `Unsupported` error, which no read of a regular file returns: no
707-
/// one ending writes it back byte-faithfully, so vendoring refuses it and
708-
/// the revert fails before any write.
711+
/// is a [`MixedLineEndings`] error ([`is_mixed_line_endings`]): no one
712+
/// ending writes it back byte-faithfully, so vendoring refuses it and the
713+
/// revert fails before any write.
709714
async fn read_pnpm_text(root: &Path, file: &str) -> std::io::Result<(String, LineEndings)> {
710715
let text = read_regular_to_string(&root.join(file)).await?;
711716
match LineEndings::of(&text) {
712717
LineEndings::Mixed => Err(std::io::Error::new(
713-
std::io::ErrorKind::Unsupported,
714-
format!(
718+
std::io::ErrorKind::InvalidData,
719+
MixedLineEndings(format!(
715720
"{file} mixes CRLF and LF line endings, which this rewriter cannot write back \
716721
byte-faithfully — normalize the file to one line ending and retry"
717-
),
722+
)),
723+
)),
724+
LineEndings::Crlf => Ok((
725+
crate::utils::line_endings::to_lf(&text).into_owned(),
726+
LineEndings::Crlf,
718727
)),
719-
LineEndings::Crlf => Ok((text.replace("\r\n", "\n"), LineEndings::Crlf)),
720728
eol => Ok((text, eol)),
721729
}
722730
}
723731

732+
/// [`read_pnpm_text`]'s error for a file that mixes line endings, told
733+
/// apart from an OS read error by its type, not its `io::ErrorKind`.
734+
#[derive(Debug)]
735+
struct MixedLineEndings(String);
736+
737+
impl std::fmt::Display for MixedLineEndings {
738+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
739+
f.write_str(&self.0)
740+
}
741+
}
742+
743+
impl std::error::Error for MixedLineEndings {}
744+
745+
/// Whether `e` is [`read_pnpm_text`]'s mixed-line-ending refusal.
746+
fn is_mixed_line_endings(e: &std::io::Error) -> bool {
747+
e.get_ref()
748+
.is_some_and(|inner| inner.is::<MixedLineEndings>())
749+
}
750+
724751
/// The vendoring refusal for a [`read_pnpm_text`] error other than a
725752
/// missing file: `vendor_lockfile_crlf_unsupported` for mixed endings, else
726753
/// `vendor_lockfile_missing` with `remedy`.
727754
fn read_refusal(file: &str, e: std::io::Error, remedy: &str) -> Box<VendorOutcome> {
728-
Box::new(match e.kind() == std::io::ErrorKind::Unsupported {
755+
Box::new(match is_mixed_line_endings(&e) {
729756
true => refused("vendor_lockfile_crlf_unsupported", e.to_string()),
730757
false => refused(
731758
"vendor_lockfile_missing",
@@ -1014,7 +1041,7 @@ pub(super) async fn revert_pnpm_dialect(
10141041
.flatten()
10151042
.filter_map(|r| r.as_ref().err())
10161043
{
1017-
if e.kind() == std::io::ErrorKind::Unsupported {
1044+
if is_mixed_line_endings(e) {
10181045
return RevertOutcome::failed(format!("{e}; nothing was reverted"));
10191046
}
10201047
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,9 +812,13 @@ async fn extract_pnpm(ctx: &DiscoverCtx<'_>, out: &mut Discovery) {
812812
// the other one: the patches it still names are recognized as NOT
813813
// wired (module docs, rule 11), so a ledger record its stale Socket
814814
// url alone keeps "mentioned" is dead.
815-
let pinned = crate::utils::pnpm_workspace::pinned_pnpm_major_in(&ctx.view).await;
816815
let (lock, shrinkwrap) = (PNPM_LOCK, PNPM_SHRINKWRAP_LEGACY);
817816
let both = (ctx.exists(lock).await, ctx.exists(shrinkwrap).await);
817+
// The pin decides only between the two, so it is read only then.
818+
let pinned = match both {
819+
(true, true) => crate::utils::pnpm_workspace::pinned_pnpm_major_in(&ctx.view).await,
820+
_ => None,
821+
};
818822
if let Some(governing) = governing_pnpm_lock(both.0, both.1, pinned) {
819823
extract_pnpm_lock(ctx, governing, disabled, out).await;
820824
read.push(governing.to_string());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ follow from the install-tree decision. N11 was decided on 2026-11-05.
9696
| N13 | Plug'n'Play | Vendored refuses. The hosted berry preflight has no PnP gate. The inventory returns an error, so `scan` sees nothing in a project hosted pinned. | The inventory reads PnP locks. Vendored keeps its refusal. | target-specific (vendored) |
9797
| N14 | pnpm `lockfile=false` | Hosted refuses. Discovery withholds hosted refs, keeps vendored ones. Vendored proceeds. The inventory ignores the setting. | The modes stay as they are; the inventory follows discovery. **Resolved in PR 2a (#2009)**: under the setting (not under Rush), the inventory keeps a Socket-hosted pnpm entry but drops its integrity, as discovery withholds the lock pin. | target-specific |
9898
| N15 | CRLF `pnpm-lock.yaml` / `pnpm-workspace.yaml` | Vendored refuses; the rest handle it | Handle it everywhere. **Resolved in PR 2b (#2029)**: the vendored planner and reverter edit a CRLF lock or workspace file like its LF twin and write it back CRLF. Only a file mixing CRLF and LF is still refused (`vendor_lockfile_crlf_unsupported`), since no single ending writes it back byte-faithfully. | |
99-
| N16 | Lock versions | pnpm: vendored accepts only 5.4/6.0/9.0; hosted any but early shrinkwrap. vlt: vendored refuses a lock with no version; hosted rewrites and warns. | Hosted. **pnpm, PR 2b (#2029)**: one table, `plan::Target::check_lock`. Vendored keeps its 5.4/6.0/9.0 range: no other version has a captured pnpm `file:` override shape that proves its rekey, so the other versions stay refused, target-specifically. vlt is unchanged. | target-specific (pnpm vendored) |
99+
| N16 | Lock versions | pnpm: vendored accepts only 5.4/6.0/9.0; hosted any but early shrinkwrap. vlt: vendored refuses a lock with no version; hosted rewrites and warns. | Hosted. **pnpm, PR 2b (#2029)**: two target-specific checks: hosted refuses only pnpm 1's early shrinkwrap (`plan::hosted_refuses_lock`), and the vendored dialects route by `formats::pnpm::sniff_lock_grammar` (`PnpmDialect::check_lock`). Vendored keeps its 5.4/6.0/9.0 range: no other version has a captured pnpm `file:` override shape that proves its rekey, so the other versions stay refused, target-specifically. vlt is unchanged. | target-specific (pnpm vendored) |
100100
| N17 | Bun workspaces, yarn classic offline mirror | Refused differently by the two modes | Stays | target-specific |
101101
| N18 | An unparseable npm lock | Hosted warns and skips. Vendored refuses the primary lock. Discovery diagnoses. The inventory returns nothing silently. | Discovery's diagnostic, surfaced by every consumer | |
102102
| N19 | yarn.lock with a stray top-level line | Discovery rejects the file; hosted and the inventory scan its blocks | Discovery's rule | |

0 commit comments

Comments
 (0)