Skip to content

Take the root alone for a pnpm workspace pattern that names the root - #2121

Open
Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
mainfrom
bazel-small/23-pnpm-root-pattern
Open

Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
mainfrom
bazel-small/23-pnpm-root-pattern

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

A second socket-patch scan (hosted or vendored mode) and socket-patch vex no longer skip every npm package of a project whose workspace lists only its root when a symlink at the project root leads to a directory with a package.json, which is what Bazel's bazel-<directory> link is after a build.

What the release does today

Measured with the binary built from main (89c482d) on real Bazel 9.3.2, 8.8.2 and 7.7.2, macOS arm64, in the rules_js fixture of the npm real-Bazel tests (aspect_rules_js 3.6.0, a pnpm lock of version 9.0), in hosted and in vendored mode. The first commit adds these steps to both npm flows and passes on main's product code:

  1. The first scan --mode hosted --yes or scan --mode vendored --yes patches the package and writes a pnpm-workspace.yaml with packages: ['.'].
  2. bazel build leaves bazel-bin, bazel-out, bazel-testlogs and bazel-<directory> in the checkout. The test asks Bazel where the checkout is and asserts that all four are there.
  3. The same scan again: exit 0, status success, one event skipped / user_patch_conflict whose reason names <checkout>/bazel-ws (linked workspace cannot be inspected safely), and scannedPackages: 0.
  4. vex --output <file>: exit 1, no_applicable_patches, one event skipped / vex_npm_patched_dependency, no document.
  5. No file of the checkout changes.

In the same checkout built by a Bazel that makes no links (--symlink_prefix=/), main's code reports on the second run: hosted scan applied, vendored scan skipped / already_vendored, scannedPackages: 1, vex exit 0 with one verified event and a document that names the package.

What changes and for whom

The cause. The inspection of the project's own patches (UserPatchIndex::workspace_members in crates/socket-patch-core/src/patch/user_patches.rs) walks each workspace pattern to a depth it takes from the pattern's segments. . leaves no segment, but the depth was counted on the segments joined again, and an empty string split at / is one item. So for a pattern that names the root, the walk listed the root's children, one level, as it does for *. No child can match an empty pattern, so no workspace member ever came from that listing. What came from it: every link at the root that leads to a directory with a package.json was recorded as a workspace member that "cannot be inspected safely", and one such entry protects every npm package of the project.

The fix. One expression: the depth is the number of segments after the literal prefix, counted on the segments themselves. It is 0 for a pattern that names the root and unchanged for every pattern that has a segment. Such a pattern then has one member, the root, and nothing below the root is listed.

For whom. This is not limited to Bazel workspaces, and it holds no Bazel-specific code. It changes the outcome for any project that has both

  • a workspace pattern that names the root alone: packages: ['.'] in pnpm-workspace.yaml, which is what the first hosted or vendored scan writes for a pnpm project, or "workspaces": ["."] in package.json; and
  • a symlink at the project root that leads to a directory holding a package.json: Bazel's bazel-<directory>, a link left by an earlier build under another directory name or a custom --symlink_prefix, a Nix result, a current -> releases/x, a link to the checkout of another package.

The same inspection gates every patching command (scan in each mode, get, apply, vendor), so all of them stop skipping such a project's npm packages. It also holds where a workspace member's own package.json, or an ancestor's workspace file, lists a pattern that names that directory alone: the links inside that member or ancestor are no longer recorded.

Before, every npm package of such a project was skipped as user_patch_conflict (exit 0) and vex failed. Now the commands report what they report without the link.

Why that is safe.

  • It is what the package managers take for such a pattern. pnpm appends /package.json to each pattern, globs for those files from the workspace root, and adds . itself to every list (findPackages and findWorkspacePackagesNoCheck, which pnpm 11 names findWorkspaceProjectsNoCheck, read in the bundled sources of pnpm 8.15.9, 9.15.9, 10.34.5 and 11.27.0). npm globs for the folder <pattern>/ (@npmcli/map-workspaces 5.0.3). . finds the root and no other directory. The sources were read; pnpm was not run.
  • No directory stops being read. The root is read before any pattern is looked at: its package.json, patches/ and every --patch-dir of its scripts, its pnpm-workspace.yaml, its locks, and the ancestors that hold a workspace file. The listing never produced a member. The unit tests hold the exact list of directories read in each case, and with the old expression put back and only the tests' assertion on links taken out, all of them pass: the old code reads the same directories and finds the same patch, and differs in the link entries alone.
  • A pattern with a segment of its own is walked exactly as before, beside a root pattern too: *, packages/*, ** and a literal path still record a link that is or leads to a member. The test that holds this passes with the old expression as well.
  • Two other entries could come from listing the root for such a pattern and no longer do: "cannot inspect workspace" when the root directory itself cannot be listed, and the limit of 16,384 entries for a root with that many directories and links. Neither stood for a member or a patch that went unread.

The same holds for every spelling that leaves no segment: ./, '', ./., .//, packages/.., and a .. that names an ancestor alone (its children are not listed either; an ancestor outside the repository is refused as before). A ! pattern never reached the walk.

Contents

Three files, +273 −15 against main.

File Lines What
crates/socket-patch-core/src/patch/user_patches.rs +151 −4 The fix: +6 −4 (one line of code for four, five lines of comment). Unit tests: +145.
crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs +113 −10 Both npm flows run their command a second time after the patched build, beside Bazel's four links, then vex.
.github/workflows/bazel-npm.yml +9 −1 patch/user_patches.rs starts the npm runs on a pull request and on a push to main.

Two commits. The first is tests only and asserts what main does (the section above). The second holds the fix, its unit tests, the changed expectations and the workflow line.

What it proves and where

Real Bazel, macOS arm64, run the way the workflow's run step runs them (the ignored tests whose names start with npm::, by exact name, two at a time), with release binaries whose sha256 matches scripts/bazel-pins.txt. Seconds per test:

Commit Bazel hosted vendored
second (the fix) 9.3.2 37.0 37.1
second (the fix) 8.8.2 17.4 17.4
second (the fix) 7.7.2 16.3 16.6
first (main's product code) 9.3.2 88.0 88.1
first (main's product code) 8.8.2 43.8 43.9
first (main's product code) 7.7.2 48.3 41.0

With the fix, both flows expect of the second run exactly what main's code reports in the checkout without Bazel's links: hosted scan applied, vendored scan skipped / already_vendored, scannedPackages: 1, vex exit 0 with a verified event and a document that names the package, no file changed, and the build still the patched one. On 9.3.2 the two outcomes were printed side by side and are identical, down to the size of the documents.

That the tests can fail (9.3.2): with the fix reverted, both flows fail on the scan's events (user_patch_conflict where applied or already_vendored is expected). Thirteen more runs change one assertion of the new step each, and each fails at that assertion: the links, bazel info, the scan's exit code, status, events and count, the document's path, the vex exit code and events, the purl in the document, the unchanged files, the build's marker, and the purl helper.

Unit tests (patch::user_patches, 39 pass), with real links and no Bazel file anywhere:

  • Each spelling of the root, listed in pnpm-workspace.yaml or in package.json, yields no entry, reads the root alone and finds the root's patch. Beside packages/*, that pattern's link is recorded and the root's is not.
  • Seven patterns with a segment (*, packages/*, packages/real/*, a literal path, two literal paths that are links, **) give the directories and links they give on main.
  • The in-memory view is held to the same. A link in memory leads nowhere, so these rows hold before the fix too.
  • Ten mutations of the one expression (the old formula, one level deeper, one shallower, operands swapped, zero for every pattern, and five more) each fail a unit test.

Other checks at the head: cargo fmt --check; clippy -D warnings for the workspace and for the real-Bazel test target; cargo check --all-targets --all-features; the whole socket-patch-core suite (8,499 passed); sixteen socket-patch-cli test targets around the walk and the user-patch gate (1,781 passed); the hermetic tests of the real-Bazel target; the cli and spawn_env_hygiene targets; the script tests (411); actionlint and the tool-pin check on the workflow. No existing test had to change.

On GitHub (Linux x86_64), both commits green. The tests-only commit 9a41046e5 passes on main's product code while asserting today's skip (run 44178278341); the fix commit 8dfd6ead3 passes with the flipped expectations (run 44179780025: both npm flows in 67 s on Bazel 9.3.2, 33 s on 8.8.2, 37 s on 7.7.2). All 51 checks pass on each.

Windows and macOS: the pull-request jobs run on Linux, so the Windows and macOS test jobs were dispatched on the fix commit (Windows run 44181240292, macOS run 44181246907); their results are added here when they finish. The product change is one arithmetic expression with nothing platform-specific; the new unit tests that make links are Unix-only, the in-memory one runs everywhere, and the real-Bazel test target is Unix-only.

Not covered

  • A root-level wildcard at a Bazel root (packages: ['*'] or ['**']). Bazel's links match such a pattern, so after a build a scan still skips every npm package as user_patch_conflict, as it does on main. A safe skip there has to look at where a link leads, and that is a change of its own. The unit tests pin today's behaviour for * and **.
  • The patch-package walk. list_patch_directory follows a linked directory, so a project whose scripts run patch-package --patch-dir . at a Bazel root still lists Bazel's output tree through its links.
  • A .. pattern. A workspace pattern that names an ancestor alone was not read as a member before and is not now: its manifest and patches/ are read only when that ancestor has a workspace file of its own. That gap is older than this change and is not closed here; this change only stops listing the ancestor's children.
  • The in-memory view and a wildcard. For an in-memory project (the hosted service's view) a root link that a wildcard pattern matches is not recorded, while on disk it is. Also older than this change and not touched here.
  • The spelling of a scoped purl. socket-patch writes the scope's @ out in the already_vendored and user_patch_conflict events and in one vex skip event, and as %40 in applied, verified, the other vex skip event and the VEX document. The tests compare purls through a helper that reads both as one package, so they pin neither. Making the product consistent is a follow-up; nothing in the product changes here.
  • The real-Bazel flows of the other ecosystems were not run: their fixtures list no workspace pattern.
  • e2e_vendor_pnpm_build, which drives a real pnpm, was not run locally.

Code review

  • The fix treated the symptom. It passed over links by Bazel's names, so any other root link kept the defect. Done: the root cause is fixed as described above; the name check, the bazel module and its line in lib.rs are gone from this branch.
  • A skip by name gives up a fail-closed guard. Done differently: no skip by name is left, so no guard is given up. The consequence is the wildcard layout under "Not covered".
  • The patch-package walk follows links at a Bazel root. Not changed here; under "Not covered".
  • The workflow started the npm runs for user_patches.rs on a push only. Done: the file is in the pull-request list as well, the comment says why it is there, and the bazel/** entry is gone with the module.
  • bazel info workspace was read without checking that it succeeded. Done: asserted, with the command's output in the message. A run with a wrong key fails there and shows Bazel's error.
  • The vex document went into the machine's shared temp directory. Done: it goes beside the workspace copy, inside the test's own scratch root, at a path that the step first asserts is free. A run that writes it into the checkout fails the unchanged-files check.
  • The tests pinned the two spellings of a scoped purl. Done: one helper reads both spellings as the same package and the second purl constant is deleted. With the helper switched off, the vendored flow fails on the spelling alone.
  • Two findings about the name check and the second purl constant. Both are gone, so neither applies.

A second review, of the reworked change, found no fault in the fix and six things around it:

  • A new unit test compared a path with /, which Windows writes with \. Done: the comparison reads both, as the neighbouring tests do.
  • A comment said a .. pattern is handled like the root. Done: the comment names . and ./ only, and the .. gap is under "Not covered".
  • The in-memory test pinned a wildcard with a root link to "no entry", which differs from the disk view. Done: that row is gone from the test; the difference is under "Not covered".
  • The wildcard layout at a Bazel root is still skipped. Not changed here; under "Not covered", with the direction a fix would take (decide by where the link leads).
  • The test named the harness's directory (bazel-ws) and wrote its own purl helper. Done: the link's name comes from the directory Bazel reports, and events are compared through the crate's canonical_purl. The VEX document is held to the exact purl it writes.
  • An independent comparison ran 67 workspace shapes through main's code and this branch's: the directories read are the same in every one, and the branch adds no entry. Its six wording and fixture notes are applied.

Decisions for the owner

  1. The wildcard layout. Leave packages: ['*'] at a Bazel root skipped after a build, or follow up with a change that decides by where a root link leads?
  2. The purl spelling. Which spelling should every event carry, %40 as the API and the VEX document have it, or @? The tests accept either.
  3. The path filter. patch/user_patches.rs now starts the three npm Bazel runs on a pull request. The file also holds the Composer, Cargo and Go parts of the consent gate, so a change to those starts the npm runs too. Keep it in the pull-request list, or on the push list only?
  4. The two entries that no longer arise for a root pattern (a root that cannot be listed, a root with more than 16,384 entries). I took them as no loss because nothing below the root is a member. Say so if either should stay.

🤖 Generated with Claude Code

The two npm flows of the real-Bazel tests ran each command once. A
pipeline runs it on every run, so both flows now run the command of their
mode a second time, after the patched build, and ask `vex` for its
document.

That second run does not patch. On Bazel 9.3.2, 8.8.2 and 7.7.2, in hosted
and in vendored mode:

- `scan --mode <mode> --yes --json` exits 0 with status `success`, skips
  the package as `user_patch_conflict` and reports `scannedPackages: 0`.
- `vex --output <file>` exits 1 (`no_applicable_patches`, the package
  omitted as `vex_npm_patched_dependency`) and writes no document.
- No file of the checkout changes.

The first run of either mode writes a `pnpm-workspace.yaml` with
`packages: ['.']`. From then on the inspection of the project's own
patches (`UserPatchIndex`, patch/user_patches.rs) walks that pattern, and
for a pattern that names the root it lists the root's children. There it
meets `bazel-<directory>`, the link Bazel keeps to its execution root.
The checkout's `package.json` shows up again behind it, the walk does not
follow links, and it records the link as a workspace member that "cannot
be inspected safely", which protects every npm package. In a checkout
without Bazel's links the second hosted scan reports the patch applied
again, the second vendored scan reports `already_vendored`, and `vex`
writes its document.

This commit holds the tests to what the commands do today, so the fix in
the next commit shows as a changed expectation. The step asks Bazel where
the checkout is (`bazel info workspace`, which must succeed) and asserts
that its four links are there: it cannot pass on a checkout without them.
It asks for the `vex` document beside the workspace copy, inside the
scratch root of the cell, so it writes nothing into the checkout or into
the machine's temp directory.

The fixture's package is scoped, and `socket-patch` spells its purl in
two ways: `pkg:npm/@socketsecurity/...` in the `user_patch_conflict`
event of either scan and in the `vex` event of the hosted flow,
`pkg:npm/%40socketsecurity/...` in the `vex` event of the vendored flow
and in the events of the first scans. The step compares purls through a
helper that reads both as the one package, so it holds no event to a
spelling.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
After a Bazel build, the next `scan --mode hosted` or `scan --mode
vendored` of an npm project skipped every npm package as
`user_patch_conflict`, with exit 0 and status `success`, and `vex` exited
1 without a document. The previous commit holds the tests to that, on
Bazel 9.3.2, 8.8.2 and 7.7.2 with aspect_rules_js 3.6.0.

Bazel is only where it showed. The first scan of either mode writes a
`pnpm-workspace.yaml` with `packages: ['.']`. The inspection of the
project's own patches (`UserPatchIndex::workspace_members`) walks each
pattern to a depth it takes from the pattern's segments. `.` leaves no
segment, but the depth was counted on the segments joined again, and an
empty string split at `/` is one item. So for a pattern that names the
root the walk listed the root's children, one level, as it does for `*`.
No child can match an empty pattern, so no member ever came of that
listing. What came of it: every link at the root that leads to a
directory with a `package.json` was recorded as a workspace member that
"cannot be inspected safely", and one such entry protects every npm
package of the project. Bazel's `bazel-<directory>` is such a link (its
execution root shows the checkout's files again). So is a Nix `result`,
a `current -> releases/x`, or a link to the checkout of another package.

The depth is now the number of segments after the literal prefix,
counted on the segments themselves: none for `.`, and what it was for
every pattern that has a segment. A pattern that names the root then has
one member, the root, and nothing below the root is listed. That is what
the package managers take for it. pnpm appends `/package.json` to each
pattern, globs for those files from the workspace root, and adds `.`
itself to every list (`findPackages` and `findWorkspacePackagesNoCheck`,
which pnpm 11 names `findWorkspaceProjectsNoCheck`,
read in the bundles of pnpm 8.15.9, 9.15.9, 10.34.5 and 11.27.0); npm
globs for the folder `<pattern>/` (`@npmcli/map-workspaces` 5.0.3). `.`
finds the root and no other directory.

What is still read. The root is read before any pattern is looked at:
its `package.json`, `patches/` and every `--patch-dir` of its scripts,
its `pnpm-workspace.yaml`, its locks, and the ancestors that hold a
workspace file. The walk never took a member from the root's children
for such a pattern, so the directories read are the ones read before;
the unit tests hold the list of directories read in each case. A pattern
with a segment of its own is walked exactly as before, beside a root
pattern too: `*`, `packages/*`, `**` and a literal path record a link
that is or leads to a member as they did.

Two other entries could come from listing the root for such a pattern
and no longer do: "cannot inspect workspace" when the root directory
itself cannot be listed, and the limit of 16,384 entries for a root with
that many directories and links. Neither stood for a member or a patch
that went unread.

The same holds for every spelling that leaves no segment: `./`, `''`,
`./.`, `.//`, `packages/..`, and a `..` that names an ancestor alone
(its children are not listed either; an ancestor outside the repository
is refused as before). A `!` pattern never reached the walk. Both
`pnpm-workspace.yaml` and the `workspaces` of a `package.json` go
through this walk.

There is no Bazel-specific code. Not covered:

- A root-level wildcard (`packages: ['*']` or `['**']`) at a Bazel root.
  Bazel's links match such a pattern, so after a build a scan still
  skips every npm package as `user_patch_conflict`. A safe skip there
  has to look at where a link leads, which is a change of its own.
- `list_patch_directory` follows a linked directory, so a project whose
  scripts run `patch-package --patch-dir .` at a Bazel root still lists
  Bazel's output tree through its links.

Unit tests in `user_patches.rs`, with real links and no Bazel file. Each
spelling of the root, in either file, yields no entry, reads the root
alone and finds the root's patch; beside `packages/*` that pattern's
link is recorded and the root's is not. The twin holds seven patterns
with a segment to the directories and the links they give without this
change. The in-memory view is held to the same.

On real Bazel the two npm flows now expect of the second run what the
commands report in a checkout without Bazel's links (measured on 9.3.2
with the walk as it was and a Bazel that makes no links): the hosted
scan reports the patch applied again, the vendored scan reports
`already_vendored`, `vex` exits 0 and its document names the package, no
file changes and the build is still the patched one.

bazel-npm.yml starts the npm legs on a pull request, as on a push to
main, that changes `patch/user_patches.rs`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Changes are limited to ignored real-Bazel e2e tests and test helpers; no product or CI runtime behavior is modified in this diff.

Overview
Extends the hosted and vendored npm real-Bazel e2e flows so that, after the patched bazel build, the suite runs scan and vex again—mirroring a second pipeline pass with bazel-bin, bazel-out, bazel-testlogs, and bazel-<checkout> present.

A new runs_again helper asserts Bazel’s workspace links, then checks that the repeat scan exits successfully but reports user_patch_conflict (linked workspace not safely inspectable) with scannedPackages: 0, and that vex exits with an error, emits the expected skip event, and writes no document, with no checkout file changes. Event comparisons go through reported, which normalizes npm scoped purls via canonical_purl so @ and %40 spellings match.

Reviewed by Cursor Bugbot for commit 9a41046. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant