Repository navigation
Test npm hosted and vendored modes under rules_js on real Bazel - #2109
Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
Conversation
The real-Bazel suite held one flow, hosted mode for a Go module. This
adds the two modes that reach an npm package Bazel builds:
aspect_rules_js reads pnpm-lock.yaml itself (npm.npm_translate_lock
under Bzlmod), so what a rewritten lock entry names is what the next
`bazel build` fetches. No file under crates/*/src changes.
Two tests, `npm::hosted_npm_translate_lock` and
`npm::vendored_npm_translate_lock`, on Bazel 9.3.2, 8.8.2 and 7.7.2,
with aspect_rules_js 3.6.0 and a lock of version 9.0. Each one, in a
fresh output base:
- builds the fixture workspace and reads "OUT: PRISTINE" from
`bazel run //:consumer`;
- runs the one command of its mode and holds the changed files to its
list. `scan --mode hosted --yes` rewrites the package's entry of
pnpm-lock.yaml (integrity and tarball URL) and creates
pnpm-workspace.yaml. `scan --mode vendored --yes` commits the patched
tarball and its ledger under .socket/vendor and points pnpm-lock.yaml,
package.json and a new pnpm-workspace.yaml at it;
- runs the same two Bazel commands and reads the patched marker, which
holds an id made for the run. Hosted: Bazel fetched the tarball from
the stand-in patch host. Vendored: Bazel read the committed file and
asked the stand-in for nothing. Bazel changed no file of the checkout,
and a `--lockfile_mode=error` build passes;
- runs the undo (`rollback --yes`, or `vendor --revert`): every file of
the checkout is the one of before, byte for byte, and the build prints
"OUT: PRISTINE" again;
- runs the command on a second machine (a new output base) against a
second patch, with other bytes at the pinned place: served by the
patch host, or written over the committed tarball. The build fails on
the sha512 and does not take the registry's tarball in its place.
The checksum guards a fetch, and an output base fetches a repository
once. The vendored test holds that as well: when the committed tarball
changes after the build, that output base goes on building the bytes it
fetched, with no error. That is how Bazel and the ruleset behave, not
socket-patch; the step is there because it is the caveat of the mode,
and it goes when a release reads the file again.
The package is the tests' own, and no registry holds it. Its tarballs
are built in the test (tar under a gzip layer that stores, so the bytes
follow from the file contents alone), the fixture's lock pins the sha512
of the upstream one, and a hermetic test holds the two together: no
binary is committed. One loopback wiremock server per checkout plays the
Socket API, the patch host and registry.npmjs.org. The lock entry names
no URL, as pnpm writes it, so rules_js asks for the tarball at the
public registry's URL, and a downloader config in the workspace copy
rewrites that URL to the stand-in. The repository cache the tests of a
leg share holds the upstream tarball after the first fetch, so no test
counts the requests for it; a patched tarball is new for every run. For
the same reason a rewrite that stopped applying would show on an empty
cache only: Bazel then asks the public registry for the name, and the
first build fails on the answer or on the lock's sha512. The Bazel
registry, the ruleset archives and Node come from the network. No pnpm
runs, and no Node of the machine is used.
- crates/socket-patch-cli/tests/e2e_bazel_build/npm.rs: the tarballs,
the stand-in, the two flows and two hermetic tests. harness.rs is
unchanged.
- crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/{common,e9}:
the workspace, with one MODULE.bazel for the three releases.
- .github/workflows/bazel-compatibility.yml: one Linux leg per Bazel
release and one nightly macOS leg for `npm::`; the npm and pnpm
source files and formats/package_json.rs in both path filters, and
vendor/ in the push filter.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…-rules-js The harness now holds the stand-in server, the shared steps of a flow and the downloader config, and bazel-compatibility.yml is a reusable workflow with one caller per ecosystem. The conflict in bazel-compatibility.yml is resolved to the harness branch's file: the npm legs move to a caller of their own in the next commit, which also ports npm.rs to the harness. Until then the npm module does not build against the merged harness. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
npm.rs now takes the stand-in server, the steps every flow is made of and the downloader config from harness.rs. Its own copies go, and with them its parsing of the Bazel release for the downloader option. The npm legs get a caller of their own, bazel-npm.yml, which calls the reusable bazel-compatibility.yml as bazel-go.yml does. Its pull request path list names the npm-family files of socket-patch-core that the two flows run through, and no longer a `*npm*` glob over every crate, which also matched the bench fixtures. The upstream tarball is new for every run. Its entry file holds an id made for the run, the test writes the sum of those bytes into the lock of its workspace copy, and the fixture's own lock holds a placeholder. Until now the fixture pinned one fixed sha512: - Bazel's downloader took that tarball from the repository cache on every run but the first, so a downloader rewrite or a registry route that had stopped working went unnoticed. Step (a) now has to fetch the tarball from the stand-in registry and asserts that it did. - The sum followed from the exact bytes flate2 and tar emit, so a bump of either could fail a hermetic test of an unrelated change. The hermetic test now holds what is still fixed: the one entry of the fixture's lock that a test pins, the version the fixture depends on, the names of the package, and that two runs serve two tarballs. The vendored flow no longer overwrites the committed tarball between steps (c) and (d). That build could not fail for the reason the step gave: the repository cache holds the patched tarball under its checksum, so a Bazel that fetched again would not read the file either. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The rules_js fixture depended on `fixture-upstream`, an unscoped name that anyone can register on the public npm registry. The integrity a test pins keeps Bazel from building foreign bytes, but a downloader rewrite that stopped applying, or a pnpm run in the fixture directory, would have asked the registry for a stranger's package. The package is now `@socketsecurity/bazel-fixture-upstream`, under the scope this repository publishes its own packages in. The rename goes through the fixture's package.json, lock, BUILD file and consumer, and through the test: the registry's tarball path, the purl (`%40socketsecurity/...`, as this crate's tests spell the API's purl of a scoped package), the quoted keys of the lock, the vendored tarball's path under a directory of its scope, and the `%2f` name with which `rollback` asks for the registry's version document. The hermetic test holds the name to the scope. In npm.rs: - The hosted flow no longer compares the number of upstream fetches before and after the patched build. That comparison could never be the assertion that fails: after step (a) Bazel's repository cache holds the upstream tarball, and a wrong fetch fails inside the build before it. The assertion that the patch route served the patched tarball stays. - The vendored flow compares the whole `pnpm-workspace.yaml` the command creates, as the hosted flow does for its own. rules_js reads that file, and until now only its presence was held. - One helper, `second_machine`, sets up the second machine of step (e) for both flows. The two copies differed only in the mode and the bytes served, and a copy without the twin's downloader rewrite would have sent the twin's Bazel to the first stand-in unnoticed. - The comment at the patch record of `grant` says why it is mounted ahead of the shared API mock's. In bazel-npm.yml: - The glob `crates/socket-patch-core/src/vendor/npm_*/**` leaves both path lists: no directory matches it. - The nightly run has a concurrency group of its own, as in bazel-go.yml: in the group of main's pushes a second push would replace a nightly that waits, and the nightly is the only run with the macOS leg. Tests, their fixture and their workflow only: no file under crates/*/src changes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PR SummaryLow Risk Overview A new
Reviewed by Cursor Bugbot for commit 9194943. Configure here. |
|
[agent] PR shepherd: review brief for What it does: On a pnpm-lock workspace under Risk: low. It adds only tests and an optional, non-required workflow. Look here
Verified: merged onto its base in a scratch tree, Changes I made: none. Open questions (minor)
Stacking and merge order: this PR's base is Shared notes (#2109–#2111): none of these PRs touches Generated by Claude Code |
No user-visible change.
What this is
Tests of what the released socket-patch already does for an npm package in a Bazel workspace that aspect_rules_js builds from a pnpm lock: hosted mode and vendored mode, on a real Bazel, in GitHub Actions.
Tests only. No file under
crates/*/srcchanges. The shared harness (harness.rs), the Go flow (go.rs), the reusable workflow (bazel-compatibility.yml) and the Go caller (bazel-go.yml) are the files of #2107, unchanged.Contents
Against #2107's branch at
f1aa02b79: 9 files changed, 793 lines added, none removed. No implementation line and no binary file. Tests and workflow are 735 lines, the fixture 58.crates/socket-patch-cli/tests/e2e_bazel_build/npm.rsnpmof the real-Bazel test target. Two#[ignore]tests, one per flow (npm::hosted_npm_translate_lock,npm::vendored_npm_translate_lock), and one test that needs no Bazel (npm::a_test_pins_the_one_entry_of_the_fixture_lock).crates/socket-patch-cli/tests/e2e_bazel_build/main.rsmod npm;.github/workflows/bazel-npm.ymlbazel-compatibility.yml.crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/common/pnpm-lock.yaml(22),BUILD.bazel(12),package.json(11),consumer.js(1),.bazelignore(1)crates/socket-patch-cli/tests/fixtures/bazel/rules_js/bzlmod/e9/MODULE.bazelnpm.npm_translate_lock(pnpm_lock = "//:pnpm-lock.yaml")The fixture is an aspect_rules_js 3.6.0 workspace under Bzlmod with a lock of
lockfileVersion: '9.0', one direct dependency, and ajs_binarythat printsOUT:and what the package exports. OneMODULE.bazelserves Bazel 7.6.0 through 9.The package is the tests' own,
@socketsecurity/bazel-fixture-upstream@1.0.0. The scope is the one this repository publishes its own packages in, so nobody else can publish the name. A test builds its tarballs, new for every run: the entry file holds an id of the run. The fixture's lock holds a placeholder integrity (the sha512 of sixty-four zero bytes), and a test writes the real one into its workspace copy. No tarball and no sum is committed.One loopback server per checkout (two per test: the second machine has its own) plays the Socket API, the patch host and
registry.npmjs.org. The lock entry names no URL, as pnpm writes one, so rules_js asks for the tarball at the public registry's URL. The harness names a downloader config outside the checkout on every Bazel command, and that config rewrites the URL to the stand-in. No pnpm runs, and the machine needs no Node: the ruleset downloads the one that runs the consumer.What it proves, and where
Each flow is one test, on the Bazel its leg installed, in a fresh output base. As asserted now:
bazel build //:consumerpasses,bazel run //:consumerprintsOUT: PRISTINE <id of the run>, Bazel wroteMODULE.bazel.lock, and the stand-in registry was asked for the upstream tarball.scan --mode hosted --yesexits 0 with oneappliedevent for the package and the patch. It namespnpm-lock.yamlandpnpm-workspace.yamlas rewritten, and those two are the only files that differ. The lock's entry of the package holds the integrity of the patched tarball and its URL on the patch host.pnpm-workspace.yamlis, whole, the root as the one package andtrustLockfile: true.scan --mode vendored --yesexits 0 with one package downloaded and applied. Eight files differ and no other: four in.socket/vendor/npm/<uuid>/(.gitattributes,.gitignore,socket-patch.vendor.jsonand the tarball, under a directory of its scope),.socket/vendor/state.json,package.json,pnpm-lock.yamland a newpnpm-workspace.yaml. The committed tarball is byte for byte the one the patch host served. The lock's entry names it by path and by sum,pnpm.overridesofpackage.jsonsends the package's one version to it, andpnpm-workspace.yamlis, whole, the root as the one package and that same override.OUT: PATCHED <id of the patch>. Bazel changed no file of the checkout,MODULE.bazel.lockincluded, and a--lockfile_mode=errorbuild passes. Hosted: the stand-in's patch route was asked during that build. Vendored: the stand-in received no request during it.rollback --yes, orvendor --revert) exits 0 and counts one package. Every file of the checkout is the one of before, byte for byte, with no file added or left, and the build printsOUT: PRISTINE <id>again without changing a file.Checksum was sha512-... but wanted sha512-...and the registry is not asked for the upstream tarball instead. Hosted: the patch host was asked. Vendored: the error names the committed file.The third test needs no Bazel and runs wherever
cargo testruns the target, on Unix. It holds the fixture's directories and the caller's legs to the test's table of releases, the one placeholder entry of the fixture's lock, the version the fixture depends on, that two runs build two different upstream tarballs, and the package's names, the@socketsecurity/scope among them.Runs
macOS arm64, this head (
9194943ca), the release binaries thatscripts/bazel-pins.txtpins, the command of the reusable workflow's run step (list the ignored tests, keep the names that start withnpm::, run them by exact name),SOCKET_PATCH_BAZEL_E2E_REQUIRED=1, the release held bySOCKET_PATCH_BAZEL_E2E_VERSION, a fresh output root per release, and one repository cache for the three, empty before the first.The leg time is the two cargo commands with the test target already built. In every run each stand-in answered one request for the upstream tarball and one for the patched one.
Each assertion that this revision adds or changes was shown to fail, on 9.3.2, with a temporary edit that was undone again:
pnpm-workspace.yamlexpected without itspackages:list: the vendored test fails on the comparison and prints the file as written.pkg:npm/@socketsecurity/...while the API stand-in serves%40: the hosted test fails withno event for ....the build did not fetch the upstream tarball from the stand-in registry./: the hosted test fails at step 4 (see known behaviour).other bytes built.@, the fixture lock's key unquoted): each fails it.Earlier revisions of this branch, with the package still unscoped, showed on 9.3.2 that both tests fail without the socket-patch step and with the upstream bytes served as the patched tarball.
Linux x86_64 (the CI runner): both flows passed on the first run there, on all three releases (run 44062752971; checks
npm / bazel (9.3.2, e9),(8.8.2, e9),(7.7.2, e9)). Test time on the runner, with no cache saved yet: 64 s on 9.3.2, 31 s on 8.8.2, 39 s on 7.7.2.What starts on main
.github/workflows/bazel-npm.yml. It runs three Linux legs (Bazel 9.3.2, 8.8.2 and 7.7.2, all on the oneMODULE.bazel) and one macOS leg (9.3.2).npm.rsorharness.rs, the rules_js fixture, or the npm-family files of socket-patch-core that the two flows run through. Those are every file withpnpmin its name or directory,vendor/npm_*,vendor/lock_inventory/npm_family.rs,patch/redirect/npmrc.rs,patch/redirect/upstream/npm.rs,vex/discover/npm.rs,crawlers/npm_crawler.rsandformats/package_json.rs.main.rsof the test target, the shared engine code (patch/redirect/**,vendor/**,hosted/**, the CLI'scommands/**), the shared test helpers and the toolchain files. A main push outside the list waits for the nightly.workflow_dispatch. The macOS leg runs only then. The nightly has a concurrency group of its own, so a push to main cannot replace a nightly that waits.main.rsis in the Go caller's push list.bazel-repos-npm-e9-<os>-<arch>-<hash of the fixture>: 102 MB after one release here, 122 MB after all three. No tarball of a run can come from it, because every run builds its own.merge_grouptrigger, and no job is aneedsofci-ok. The repository variableBAZEL_COMPAT_DISABLEDturns every leg off.Not claimed
update_pnpm_lock = True, rules_nodejsnpm_install, agent mode.SOCKET_PATCH_SERVER_URLandSOCKET_NPM_REGISTRYso thatrollbackrecognises the pin and finds the upstream entry. Production pinshttps://patch.socket.devand needs neither.%40socketsecurity/...) and the URL shape (/patch/npm/@scope/name/<version>/<token>/<uuid>/<file>) are those of this repository's other tests and fixtures.overrides:block or importer, and the four ledger files are held by name, the tarball by content..socketdirectory: the tests compare files, not directories.@socketsecurity/bazel-fixture-upstreamis registered. It was not looked up.Known behaviour the flow records
pnpm-workspace.yamlwithtrustLockfile: true, to be committed with the lock. The scan's own warning (redirect_pnpm_trust_lockfile) says why: pnpm 11 and later reject the rewritten lock without it, and the setting makes pnpm skip its re-verification of every lock entry, while the sha512 pins stay enforced. The test holds the file's content; it does not hold the warning, and no pnpm ran.rollbackneeds the registry. It reads the upstream entry from the registry's version document, which for a scoped name it asks for as<registry>/@scope%2fname/<version>. With that document missing,rollbackexits 1 withhosted_restore_refusedand tells the user to restore the lock from version control. Seen in one run with the stand-in's document moved; in the suite the registry is the stand-in.scantakes a scoped purl in either spelling and names the patch in its events as the API spelled it. With@in place of%40everywhere, both flows also pass.rollback --yesfor hosted,vendor --revertfor vendored.Code review
First round
no_proxyand removesNODE_OPTIONSandNODE_PATHfrom every Bazel child. A deadHTTPS_PROXYstill fails any flow, by design: the machine's proxy stays the way to the Bazel registry.*npm*matched the bench fixtures. The npm legs have their own caller, with a list scoped to socket-patch-core and read from a coverage build of the binary under both flows.npm.rsuses the harness's.npm.rsparsed the Bazel release itself to pick the downloader option. The harness names the downloader config.Second round
fixture-upstream, an unscoped name anyone can register. A downloader rewrite that stopped applying, or a pnpm run in the fixture directory, would have asked the public registry for a stranger's package. The package is now@socketsecurity/bazel-fixture-upstream, in the fixture and in the test, and both flows pass on the three releases.pnpm-workspace.yamlit creates, although rules_js reads that file. It now compares the whole file.second_machine, serves both.crates/socket-patch-core/src/vendor/npm_*/**). Removed from both lists; every remaining glob matches a tracked file, and the pull request list is a subset of the push list.grantmounts its own patch record ahead of the shared API mock's, because that mock's file hashes are fixed and vendored mode checks the downloaded file against them. The fix is in a helper that other test targets share. A comment at the route now says why it is there.text, and the wrapper around the harness'ssocket_patch). They belong inharness.rswith its next change.Decisions for the owner
@socketsecurity/bazel-fixture-upstreamis not published by this pull request and was not looked up. Say if another name under the scope is preferred; the rename is mechanical.🤖 Generated with Claude Code