Repository navigation
Take the root alone for a pnpm workspace pattern that names the root - #2121
Mikola Lysenko (mikolalysenko) wants to merge 2 commits into
Conversation
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>
PR SummaryLow Risk Overview A new Reviewed by Cursor Bugbot for commit 9a41046. Configure here. |
A second
socket-patch scan(hosted or vendored mode) andsocket-patch vexno 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 apackage.json, which is what Bazel'sbazel-<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:
scan --mode hosted --yesorscan --mode vendored --yespatches the package and writes apnpm-workspace.yamlwithpackages: ['.'].bazel buildleavesbazel-bin,bazel-out,bazel-testlogsandbazel-<directory>in the checkout. The test asks Bazel where the checkout is and asserts that all four are there.success, one eventskipped/user_patch_conflictwhose reason names<checkout>/bazel-ws (linked workspace cannot be inspected safely), andscannedPackages: 0.vex --output <file>: exit 1,no_applicable_patches, one eventskipped/vex_npm_patched_dependency, no document.In the same checkout built by a Bazel that makes no links (
--symlink_prefix=/), main's code reports on the second run: hosted scanapplied, vendored scanskipped/already_vendored,scannedPackages: 1,vexexit 0 with oneverifiedevent and a document that names the package.What changes and for whom
The cause. The inspection of the project's own patches (
UserPatchIndex::workspace_membersincrates/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 apackage.jsonwas 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
packages: ['.']inpnpm-workspace.yaml, which is what the first hosted or vendored scan writes for a pnpm project, or"workspaces": ["."]inpackage.json; andpackage.json: Bazel'sbazel-<directory>, a link left by an earlier build under another directory name or a custom--symlink_prefix, a Nixresult, acurrent -> releases/x, a link to the checkout of another package.The same inspection gates every patching command (
scanin 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 ownpackage.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) andvexfailed. Now the commands report what they report without the link.Why that is safe.
/package.jsonto each pattern, globs for those files from the workspace root, and adds.itself to every list (findPackagesandfindWorkspacePackagesNoCheck, which pnpm 11 namesfindWorkspaceProjectsNoCheck, 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-workspaces5.0.3)..finds the root and no other directory. The sources were read; pnpm was not run.package.json,patches/and every--patch-dirof its scripts, itspnpm-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.*,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.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.
crates/socket-patch-core/src/patch/user_patches.rscrates/socket-patch-cli/tests/e2e_bazel_build/npm.rsvex..github/workflows/bazel-npm.ymlpatch/user_patches.rsstarts 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 matchesscripts/bazel-pins.txt. Seconds per test: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 scanskipped/already_vendored,scannedPackages: 1,vexexit 0 with averifiedevent 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_conflictwhereappliedoralready_vendoredis 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, thevexexit 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:pnpm-workspace.yamlor inpackage.json, yields no entry, reads the root alone and finds the root's patch. Besidepackages/*, that pattern's link is recorded and the root's is not.*,packages/*,packages/real/*, a literal path, two literal paths that are links,**) give the directories and links they give on main.Other checks at the head:
cargo fmt --check;clippy -D warningsfor the workspace and for the real-Bazel test target;cargo check --all-targets --all-features; the wholesocket-patch-coresuite (8,499 passed); sixteensocket-patch-clitest targets around the walk and the user-patch gate (1,781 passed); the hermetic tests of the real-Bazel target; thecliandspawn_env_hygienetargets; 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
9a41046e5passes on main's product code while asserting today's skip (run 44178278341); the fix commit8dfd6ead3passes 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
packages: ['*']or['**']). Bazel's links match such a pattern, so after a build a scan still skips every npm package asuser_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**.list_patch_directoryfollows a linked directory, so a project whose scripts runpatch-package --patch-dir .at a Bazel root still lists Bazel's output tree through its links...pattern. A workspace pattern that names an ancestor alone was not read as a member before and is not now: its manifest andpatches/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.socket-patchwrites the scope's@out in thealready_vendoredanduser_patch_conflictevents and in onevexskip event, and as%40inapplied,verified, the othervexskip 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.e2e_vendor_pnpm_build, which drives a real pnpm, was not run locally.Code review
bazelmodule and its line inlib.rsare gone from this branch.user_patches.rson a push only. Done: the file is in the pull-request list as well, the comment says why it is there, and thebazel/**entry is gone with the module.bazel info workspacewas 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.vexdocument 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.A second review, of the reworked change, found no fault in the fix and six things around it:
/, which Windows writes with\. Done: the comparison reads both, as the neighbouring tests do...pattern is handled like the root. Done: the comment names.and./only, and the..gap is under "Not covered".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'scanonical_purl. The VEX document is held to the exact purl it writes.Decisions for the owner
packages: ['*']at a Bazel root skipped after a build, or follow up with a change that decides by where a root link leads?%40as the API and the VEX document have it, or@? The tests accept either.patch/user_patches.rsnow 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?🤖 Generated with Claude Code