Repository navigation
Leave a hosted gem alone while a rules_ruby hub holds it to another sha256 - #2210
Conversation
…cksums
A `ruby.bundle_fetch` call can hold gems to a sha256 of its own:
`gem_checksums = {"<name>-<version>": "<sha256>"}`. rules_ruby gives that
sha256 to Bazel's downloader for the gem, whatever `Gemfile.lock` says, and
a build of the hub prints the dict to add (Bazel's hint for a reproducible
repository). The existing flow `ruby::hosted_bundle_fetch` covers a call
without one.
This adds the flow `ruby::hosted_bundle_fetch_gem_checksums` on real Bazel:
the fixture's workspace with a `gem_checksums` on the call that names the
two gems at the sha256 of their upstream archives. It holds what the
command does there today:
- `scan --mode hosted --yes` exits 0 and reports the gem applied. It
rewrites `Gemfile` and `Gemfile.lock`, with the sha256 of the patched gem
in the lock's `CHECKSUMS` section, and leaves the call as it was.
- The same `bazel build` then fails. Bazel takes the upstream gem from its
repository cache under the sha256 the call names (the patch registry is
not asked once), and Bundler, in the `bundle install` action of the
build, reports mismatched checksums: the lock's sha256 of the patched
gem against the one of the upstream archive.
So the command exits 0 and leaves a build that fails. The next commit
changes the command, and this flow's expectations with it.
The test edits `MODULE.bazel` in its workspace copy, because the sha256 of
a gem is new for every run; the fixture and the existing flow's steps are
as they were. The stand-in setup of that flow moved into a helper that
both flows start with.
Measured on Bazel 9.3.2 with rules_ruby 1.0.0-rc2 on macOS arm64.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ha256 rules_ruby gives Bazel's downloader the sha256 that a `ruby.bundle_fetch` call names for a gem in `gem_checksums`, whatever `Gemfile.lock` says. Hosted mode points the gem at the patch registry and records the sha256 of the patched archive in the lock, and never edits the call. So beside a call that names the upstream archive's sha256, `scan --mode hosted` exited 0, reported the gem applied, and the next `bazel build` failed: Bundler found mismatched checksums, or, with an empty repository cache, Bazel refused the download. With a lock that has no `CHECKSUMS` section the build could also pass on the upstream gem. The previous commit holds that in a test. Hosted mode now reads the calls and writes nothing for such a gem: no `source:` option, no change to the lock. The gem is `skipped` (`redirect_unconfirmed`), the run exits 0 as for any other gem hosted mode does not pin, the other gems of the run are rewritten as before, and one warning per gem, `redirect_bazel_gem_checksums_pinned`, names the call by file and line, the entry, and the way on: - change the entry to the sha256 of the patched archive, which the warning gives; or - where the lock has a `CHECKSUMS` section, delete the gem from the dict: Bundler then holds the gem to that section, as in a workspace without `gem_checksums`. Not offered for a lock without the section, where nothing would hold the gem to a sha256. Both were measured on Bazel 9.3.2 before they were written into the warning, and the flow does both on 9.3.2, 8.8.2 and 7.7.2 (rules_ruby 1.0.0-rc2): after either, the same scan pins the gem, the build is the patched one and it changes no file of the checkout. A dict that names only other gems builds patched, so only an entry for the gem itself counts. An entry is compared with the sha256 of the patched archive without regard to the case of its letters, as Bazel reads one (`Checksum.java`: any hex digits, lowered). What is read. `bazel/ruby.rs` asks the reader of the workspace files for the `bundle_fetch` tags of the rules_ruby `ruby` extension, under whatever name the file gives the proxy, and keeps the calls that may read the project's lock and have a `gem_checksums`. The hosted engine hands them to the gem rewriter under a synthetic key, as it hands the Maven hub's state, and the rewriter asks before it takes the plan of a gem, so a gem that is refused for another reason keeps that refusal alone. What an uncertain reading does. Nothing is evaluated, and one rule decides: what is seen of a call that may hold the gem to another sha256 withholds the redirect, and what is not seen changes nothing. - A call in a comprehension or a condition may run: it counts. - A `gemfile_lock` that is no label in a plain string may be this lock: the call counts. A call that names another file does not. - A `gem_checksums` that is a constant, an expression or a `**` argument may name the gem: the gem is left alone, and the warning says to write the dict out. - Every call that may read the lock counts, whichever hub the built targets use. - The doubts of the reader (an include it cannot follow, a name that is an expression) can hide a call. They are not looked at: where no call that is read may read the lock, hosted mode writes as on main. Withholding there would take hosted mode from every gem of such a workspace, also where no hub reads the lock, and a patch that works today is not to be taken away for a call nobody has seen. - `rb_bundle_fetch` in a `WORKSPACE` file counts where no tag of the module files may read the lock. No test builds such a hub; its failing build was measured by hand on Bazel 7.7.2 with rules_ruby 0.21.1. Beside a tag that may read the lock the file is a leftover of a migration, and the gem stays one that hosted mode patches. Two shapes that built the patched gem before are left alone by this rule (measured on 9.3.2): a constant that does not name the gem, and a second call on the same lock that holds the gem while the built target uses the hub of the call without the entry. No pin is taken out of a checkout, and the way on that the warning names ends, in both, in a shape the flow builds patched. Nothing changes for a project in no Bazel workspace, for one whose lock no call may read, beside a call without `gem_checksums`, and for an entry that holds the patched sha256: in a project without a Bazel workspace, beside a hub that reads another lock and beside one without `gem_checksums`, a dry run, a scan and a rollback of this binary and of one built from the parent of this branch give the same exit codes, output and files, byte for byte. The in-memory engine gives the same answer where it has the files: the project's directory is the Bazel root, and path selection now asks for `MODULE.bazel`, the `*.MODULE.bazel` files it can include and the `WORKSPACE` names beside a gem lock. A parity test holds the checkout and the engine to the same warning and the same files. A project below the Bazel root is not given the workspace's files in memory and is rewritten as before. Not covered, and said in the docs: `rollback` and `remove` do not touch the call either, so an entry that was changed by hand goes back by hand (the build fails on it until then: measured on 9.3.2); a pin that an earlier release wrote beside such an entry stays, under whatever address the patch registry had for the gem then, and is still reported as applied because it is in the lock. Its warning says that the pin is left as it is, in place of a redirect that was skipped, and names `rollback` as the other way out. With `--vex` a run that leaves its only gem alone has nothing to attest and exits 1, like any run that pins nothing. A call of another Bazel module, a macro that makes the call, and the rule loaded under another name are not seen: there the scan pins the gem as before and the build fails as before, which the docs keep as a known problem. docs/ecosystems.md: the RubyGems cell of the Bazel table, the list of what it was tested with, the known problems (now the rollback after a changed entry, and a gem that a call which is not read holds) and a section "Bazel workspaces: RubyGems". CLI_CONTRACT.md: the row of the warning. bazel-ruby.yml: the sentence of its header about the repository cache, which the new flow uses for gems of its own run. scripts/loc-budget.json: the cap of the bazel group follows the count, 2486 to 2626. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…l that holds a gem A review of the change that leaves a hosted gem alone beside a `gem_checksums` entry found two ways a run could still pin a gem that a call holds, and four places where the check did more or less than it should: - `rb_bundle_fetch(gemfile_lock = "@shop//:Gemfile.lock")` in a `WORKSPACE` file that says `workspace(name = "shop")` names the root's lock. It was read as a lock of another repository, so the call did not count, the gem was pinned and the Bazel 7 build failed on the checksum. The name that file gives the main repository is now read; under a name that is not written out any repository may be the main one. - A tag whose `gemfile_lock` is not written out hid every `rb_bundle_fetch` of the `WORKSPACE` file, although it is no sign that the file is a leftover of a migration. Only a tag that names the lock in a plain string does that now. - An entry that is an empty string withheld the gem. rules_ruby gives Bazel no sha256 for it (`if sha256:` in `_download_gem`), so it holds nothing. - The warning named the first call that holds the gem. Where several do, it now names the others too, so one round of edits is enough. - Path selection for the in-memory engine asked for the files of a Bazel workspace beside any gem lock, also in a run that leaves gem out. It asks only when gem is selected. - The gem rewriter parsed the calls it was handed once for every gem. It parses them once. Stated in the docs, not changed: an `rb_bundle_fetch` beside a tag that names the lock is still not looked at, although Bazel 7 reads both files; a project that a call names through a symbolic link, or one that is a git checkout of its own, is not matched; the in-memory engine does not read a workspace file that is a symbolic link; and a pin that an earlier release wrote beside a stale entry is still reported `applied` and attested by `--vex`. The `bazel` line budget goes from 2626 to 2660. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PR SummaryMedium Risk Overview A new Tests and docs add real-Bazel and parity coverage for the withheld-redirect flow, plus updates to Reviewed by Cursor Bugbot for commit 9d8e2fa. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 9d8e2fa. Configure here.
A WORKSPACE file with no `workspace(name = ...)` gives the main repository Bazel's default name `__main__`, so an `rb_bundle_fetch` whose `gemfile_lock` is `@__main__//:Gemfile.lock` reads the root's lock. It was mapped to "not this lock", and hosted mode still pinned a gem that hub holds to another sha256. A `workspace()` call that passes `**` now leaves the name unknown, so any repository may be the main one there. Assisted-by: Claude Code:claude-opus-5-5
|
[agent] PR shepherd: review brief for approval What it does Risk Look here
Verified
Changes I made Open questions
If either default is wrong, say so in the review. Neither blocks merging. Generated by Claude Code |
Wenxin Jiang (Wenxin-Jiang)
left a comment
There was a problem hiding this comment.
[agent] Found a rollback instruction that can unwind unrelated patches.
docs/ecosystems.md: main added the rules_rust clause to the Cargo row and this branch the rules_ruby gem_checksums clause to the RubyGems row; both rows keep their side's addition. scripts/loc-budget.json: main raised the bazel group to 2665 and this branch to 2660 from the same base 2486; the merged tree counts 2844. Assisted-by: Claude Code:claude-opus-5-5
The warning for a pin an earlier run wrote beside a gem_checksums entry told the user to take the pin out with a bare `socket-patch rollback`, which restores every hosted, vendored and in-place patch of the project. It now names the pin as rollback selects it, by the gem's base purl: `socket-patch rollback pkg:gem/<name>@<version>`. The unit test, the Bazel e2e, CLI_CONTRACT.md and docs/ecosystems.md follow. Assisted-by: Claude Code:claude-opus-5-5
main gained the pnpm 11 and hosted NuGet withholds and the vendored crate build-file change. Two conflicts: - docs/ecosystems.md, the list of known problems: this branch's two gem entries take the place of the one gem entry, and main's three npm entries take the place of the one npm entry this branch still had. - scripts/loc-budget.json: the cap of the `bazel` group is the count of the merged tree, 3518. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

In a Bazel workspace,
scan --mode hostedandget --mode hostednow leave a gem alone while a rules_rubyruby.bundle_fetchcall holds it to another sha256 ingem_checksums, and say how to go on, where they used to exit 0 and break the nextbazel build.Based on main, in three commits: tests that record what main does today, the change, and the fixes of a code review (the last section).
Decisions for the maintainer
MODULE.bazeldoes not withhold. Only a call that is read can. The alternative, to withhold every gem of a workspace with an unreadable part, is safer for the build and takes hosted mode from workspaces it patches today.gem_checksumsthat is not written out withholds, also when it does not name the gem. That shape builds patched today; it loses the redirect until the dict is written out in the call.WORKSPACEcall counts on every Bazel release, also where Bazel no longer reads the file, unless a tag ofMODULE.bazelnames the lock in a plain string. Beside such a tag the file is taken for a leftover of a migration and is not looked at, although Bazel 7 reads both files: a workspace whose targets use the hub of theWORKSPACEfile, with an upstream entry there, is still pinned and its build still fails. Counting the call there would withhold the gem in a workspace that only kept the file through a migration, which builds patched today.rollbackafter a changed entry. This change only says so, in the warning and in the docs.rollbackcould warn, or put the old sha256 back; both mean reading, or editing, the call fromrollback.CHECKSUMS. It trades Bazel's check of the gem for Bundler's. Dropping the offer leaves one way on.applied, and--vexstill attests it. Only the warning tells the truth about that build. Reporting it as broken, in the event or in VEX, means reading the call where the pin is confirmed and where the document is written, which this change does not do.What the command does today
rules_ruby's
ruby.bundle_fetchdownloads every gem of itsgemfile_lockwith Bazel's downloader. Itsgem_checksums = {"<name>-<version>": "<sha256>"}gives Bazel a sha256 for each gem it names, whatever the lock says. A build of the hub prints the dict to add, so many workspaces have one that names every gem.Hosted mode points a gem at the patch registry in
GemfileandGemfile.lockand puts the sha256 of the patched gem in the lock'sCHECKSUMSsection. It does not touchMODULE.bazel. Beside a call that names the upstream gem's sha256:socket-patch scan --mode hosted --yesexits 0 and reports the gemapplied.bazel buildfails. Bazel takes the upstream gem from its repository cache under the sha256 of the call, and Bundler refuses it (Bundler found mismatched checksums). With an empty repository cache Bazel refuses the patched gem itself (Checksum was ... but wanted ...). With a lock that has noCHECKSUMSsection the build can also pass, on the upstream gem.The first commit holds 1 and 2 in a test on real Bazel.
What changes
Hosted mode writes nothing for a gem when a
ruby.bundle_fetchcall that may read the project's lock<name>-<version>, or<name>-<version>-<platform>) ingem_checksumsto another sha256 than the patched archive has, orgem_checksumsthat is not written out as a dict of strings (a constant, an expression, a**argument).No
source:option, no change to the lock. The gem isskipped(redirect_unconfirmed), the run exits 0 as for any other gem hosted mode does not pin, the other gems of the run are rewritten as before, and one warning per gem,redirect_bazel_gem_checksums_pinned, names the call by file and line, the entry, and the way on (and, where more than one call holds the gem, the others):The two ways on were measured on Bazel 9.3.2 before they went into the warning:
CHECKSUMSline (other bytes at the patch registry fail the build). Offered only for a lock that has the section: without it nothing would hold the gem to a sha256.The build after either changes no file of the checkout, Bazel's own lock included. A dict that names only other gems builds patched, so only an entry for the gem itself counts. An entry is compared with the patched sha256 without regard to case, as Bazel reads one. socket-patch does not edit the call.
A pin that an earlier release wrote beside such an entry, which is the state of a checkout this defect already broke, stays as it is, under whatever address the patch registry had for the gem then. The gem is still reported
applied, because the pin is in the lock, and the warning says what is true there:Nothing changes for a project in no Bazel workspace, for one whose lock no call may read, beside a call without
gem_checksums, for a dict that does not name the gem, and for an entry that already holds the patched sha256 or is an empty string. In a checkout without a Bazel workspace, beside a hub that reads another lock, beside a hub withoutgem_checksums, beside a dict that names only the other gem, and beside an entry that holds the patched sha256 in lower or upper case, a dry run, a scan and a rollback of this binary and of one built from the base gave the same exit codes, output and files, byte for byte.What an uncertain reading does
Nothing is evaluated, and one rule decides: what is seen of a call that may hold the gem to another sha256 withholds the redirect, because a build that fails outranks a patch that is missed and explained; what is not seen changes nothing.
gemfile_lockthat is no label in a plain string may be this lock: the call counts. A call that names another file does not.gem_checksumsthat is not written out may name the gem: the gem is left alone, and the warning says to write the dict out.MODULE.bazelthat cannot be read (anincludethat cannot be followed, abazel_depwhose name is an expression) can hide a call. It is not looked at: where no call that is read may read the lock, hosted mode writes as before. Withholding there would take hosted mode from every gem of such a workspace, also where no rules_ruby hub reads the lock.rb_bundle_fetchin aWORKSPACEfile counts like the tag unless a tag of the module files names the lock in a plain string. Beside such a tag the file is taken for a leftover of a migration, so that shape stays one hosted mode patches. In that file@<name>//is the main repository for the nameworkspace(name = ...)gives.Two shapes that built the patched gem before are left alone by this rule (measured on Bazel 9.3.2): a constant that does not name the gem, and a second call on the same lock that holds the gem while the built target uses the hub of the call without the entry. No pin is taken out of a checkout, and the way on that the warning names ends, in both, in a shape the test builds patched.
Contents
crates/socket-patch-core/src/bazel/ruby.rs(new): the calls that may read the lock, and whether one stands against a gemhosted/engine.rs: hands the calls to the gem rewriter, from the checkout and from files in memoryhosted/memory/select.rs: asks for the workspace's files beside a gem lockpatch/redirect/mod.rs: the gem rewriter asks before it takes a gem's plan, and says whether an earlier run pinned the gembazel/mod.rs,bazel/workspace.rstests/e2e_bazel_build/ruby.rs: the real-Bazel flow and two tests without Bazeltests/hosted_memory_parity.rs: the checkout against the in-memory enginedocs/ecosystems.md,CLI_CONTRACT.md.github/workflows/bazel-ruby.yml(one comment),scripts/loc-budget.jsonNo fixture file is added: the flow edits
MODULE.bazelin its workspace copy, because the sha256 of a gem is new for every run. The table is the first two commits; the third (the review's fixes) is +229 −76 in six files, most of it unit tests ofbazel/ruby.rs. The cap of thebazelgroup inscripts/loc-budget.jsonfollows the count, 2,486 to 2,660.What it proves, and where
ruby::hosted_bundle_fetch_gem_checksums, in the legs ofbazel-ruby.yml: Bazel 9.3.2, 8.8.2 and 7.7.2, rules_ruby 1.0.0-rc2): the scan leaves the gem alone and changes no file; the same build passes on the upstream gem and nothing asks the patch registry; with the sha256 of the warning in the entry the same scan pins the gem, the build prints the patched marker and changes no file, andvexattests it;rollbackrestores the two files and leaves the call, the one file that still differs, and with the old sha256 back everything is as before; with the gem deleted from the dict the same scan pins it and the build prints the patched marker and changes no file. Locally both ruby flows passed on the three releases on macOS arm64.--dry-run,--vex, a lock withoutCHECKSUMS, a dict that is not written out, and the calls that stand against nothing, whose rewritten files equal those of a checkout withoutMODULE.bazel. Over a pin left by an earlier release: the gem isapplied, the warning has the text for a pin that stays, nothing is changed, androllbacktakes the pin out.parity_gem_checksums_of_a_bazel_hub_through_selection): the same warning and the same files, with the engine given what path selection asks for.WORKSPACEcases, an unreadable part), of the warning's texts, of a sha256 in upper case, of a pin under an older address of the patch registry, and of the rewriter and the engine.Not covered
.bzlfile that makes the call,rb_bundle_fetchloaded under another name, anrb_bundle_fetchof theWORKSPACEfile beside a tag that names the lock (decision 4), a part ofMODULE.bazelthat cannot be read, a label that spells the lock's name in another case, and what the command line or an rc file tells Bazel. The docs keep it in the list of known problems, with the way out.rollbackandremovedo not touch the call either. After one of them an entry that was changed by hand still holds the sha256 of the patched gem, and the build fails until the old value is back (measured on 9.3.2). The docs list this as a known problem too.appliedand still attested by--vex, because the pin is in the lock; only the warning tells the truth about the build.--vex, a run that leaves its only gem alone has nothing to attest and exits 1 (manifest_not_found), like any run that pins nothing.MODULE.bazelorWORKSPACEthat is a symbolic link is not read. Both are rewritten as before. On disk, three projects are not covered either: one below a workspace root that is in no git checkout, one that is a git checkout of its own below the Bazel root (a submodule), and one whose directory the call names through a symbolic link. The first two are the bounds of the lookup every Bazel lane of main uses for the workspace's root.WORKSPACEhub (its failing build was measured by hand on Bazel 7.7.2 with rules_ruby 0.21.1), a gem that is only a dependency of another gem, or a platform gem under Bazel; those have unit tests of the same check.Code review
Ten findings, each read against the code. Six are fixed in the third commit, and a unit test fails without each of the first five (four checked by taking the fix out again):
WORKSPACElabel through the workspace's own name.rb_bundle_fetch(gemfile_lock = "@shop//:Gemfile.lock")besideworkspace(name = "shop")was read as a lock of another repository, so the call did not count, the gem was pinned and the Bazel 7 build failed. The name that file gives the main repository is now read; under a name that is not written out any repository may be the main one.WORKSPACEfile. A tag whosegemfile_lockis not written out is no sign of a leftover. Only a tag that names the lock in a plain string hides the file now.if sha256:in_download_gem), so it holds nothing.Not fixed, with the reason:
rb_bundle_fetchbeside a tag that names the lock is still not looked at. It is decision 4 above, and the docs list it under what is not seen.appliedand attested by--vex. It is decision 7: judging it is the VEX step's, where a hosted gem would be held to itsgem_checksumsentry.bazel/ruby.rsrepeats ofbazel/built.rs(the hub's identity, a label's path, the project's path from the root). Another open pull request, Decline PyPI and RubyGems vendored mode where a Bazel hub reads the lock #2208, rewrites those parts ofbuilt.rsand adds the path helper; sharing them here would only make the two conflict, so it is left to whichever lands second.At the head of the third commit:
cargo checkon all targets, both clippy runs with-D warnings, the whole core unit suite (8,380), the golden targets, the CLI targets that exercise the gem rewriter and the in-memory engine, the script tests and the line budget pass, and bothruby::flows pass on real Bazel 9.3.2. Bazel 8.8.2 and 7.7.2 were not rerun after the review; the legs ofbazel-ruby.ymlrun them. Two tests ofe2e_redirect_gem_stale_installfail under aBUNDLE_USER_HOMEof the environment, as they do on main, and pass without it.Generated by Claude Code