Skip to content

Rolling back a precompiled platform gem (nokogiri) that went from hosted to vendored leaves the patched build in vendor/bundle, and bundle pristine then fails on the restored checksum #2174

Closed

Description

[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).

Summary

A gem locked as precompiled platform gems (nokogiri) that was patched in hosted mode and then moved to vendored mode keeps its patched installed copy and its patched cache/*.gem in the project's own vendor/bundle after rollback. Gemfile and Gemfile.lock come back byte for byte, but:

  • the app keeps loading the patched code after rollback and bundle install, and
  • on a lock with CHECKSUMS (Bundler 2.6+ / 4.x) the next bundle pristine fails with "Bundler found mismatched checksums", after it has already removed the installed nokogiri, so the app no longer loads at all.

This is #2083 again for the platform-gem shape that #2150 / #2154 added. The same sequence with a ruby-platform gem (rack 2.2.8) is fine on this commit: rollback prints "Replaced the installed copy of 1 gem with the upstream build" and pristine passes. A plain hosted rollback of the same nokogiri (no vendored step) also restores the upstream build.

Impact

"Do no harm" is broken on rollback: a project that installed cleanly before socket-patch touched it fails bundle pristine (and any reinstall from the gem home's cached archive) afterwards, and until then runs code the user asked to have removed. The rollback output only says "1 unwired package keeps its patched bytes in installed trees until the next package-manager install", which is not true here: bundle install keeps the installed gem.

Repro

Linux x86_64, Ruby 3.3.6, Bundler 4.0.18. The patch service is the repo's own stand-in, tests/nokogiri/patch_service.rb (PatchService.new("nokogiri", "1.18.10", %w[x86_64-linux-gnu aarch64-linux-gnu arm64-darwin x86_64-darwin x86_64-linux-musl], …), mark SOCKET_PATCHED = true appended to lib/nokogiri.rb); $URL is its address.

sp() { socket-patch "$@" --api-url $URL --patch-server-url $URL --api-token fake --org nokogiri-e2e; }
mkdir app && cd app && git init -q .
printf 'source "https://rubygems.org"\n\ngem "nokogiri", "1.18.10"\n' > Gemfile
printf '**/vendor/bundle/\n' > .gitignore
bundle config set --local path vendor/bundle
bundle install
# keep only the platforms the patch has a prebuild for
bundle lock --remove-platform aarch64-linux-musl arm-linux-gnu arm-linux-musl
cp Gemfile G0; cp Gemfile.lock L0

sp scan --mode hosted --yes      # "Replaced the installed copy of 1 gem with the patched build"
sp scan --mode vendored --yes    # "was hosted; restored its upstream registry entry … (mode takeover)", "Vendored 1 package."
sp rollback --yes
cmp Gemfile G0 && cmp Gemfile.lock L0      # identical
bundle install
bundle exec ruby -e 'require "nokogiri"; p defined?(SOCKET_PATCHED)'
bundle pristine
bundle exec ruby -e 'require "nokogiri"'

Actual

$ sp rollback --yes
Reverted vendoring for pkg:gem/nokogiri@1.18.10?platform=ruby

Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.
$ bundle install
Bundled gems are installed into `./vendor/bundle`
$ bundle exec ruby -e 'require "nokogiri"; p defined?(SOCKET_PATCHED)'
"constant"
$ bundle pristine
Installing racc 1.8.1 with native extensions
Bundler found mismatched checksums. This is a potential security risk.
nokogiri (1.18.10-x86_64-linux-gnu) sha256=ff5ba26b…   from the lockfile CHECKSUMS at Gemfile.lock
nokogiri (1.18.10-x86_64-linux-gnu) sha256=60e711cd…   from the gem at …/vendor/bundle/ruby/3.3.0/cache/nokogiri-1.18.10-x86_64-linux-gnu.gem
$ bundle exec ruby -e 'require "nokogiri"'
…: cannot load such file -- nokogiri (LoadError)

ff5ba26b… is the upstream gem, 60e711cd… the patched prebuild the hosted scan put in the gem home.

Expected

What the same sequence does for rack: the rollback replaces the project's installed copy (and its gem-home archive) with the upstream build, or names what is left and the command that fixes it. docs/ecosystems.md ("Ruby gems: gems already installed") and the "Gem stale-install guard" section of crates/socket-patch-cli/CLI_CONTRACT.md describe the vendored unwind bringing a copy that a hosted scan had replaced back to the upstream gem (#2083).

Versions

Bundler Ruby Gem Result
4.0.18 3.3.6 nokogiri 1.18.10 (platform gems, CHECKSUMS) reproduces (twice): patched code after rollback, pristine fails and removes nokogiri
2.6.9 3.1.6 nokogiri 1.18.10 (platform gems, bundle lock --add-checksums) reproduces
4.0.18 3.3.6 rack 2.2.8 (ruby platform) does not reproduce: upstream build restored
4.0.18 3.3.6 nokogiri 1.18.10, hosted then rollback (no vendored step) does not reproduce

Tested on main 6579162. Linux only; nothing here looks platform-specific, so macOS should behave the same with its arm64-darwin build. No first bad release: vendoring a platform gem is new on main (#2154) and not in 5.1.0.

Suspect

crates/socket-patch-cli/src/commands/gem_refresh.rs:1239 (after_vendored_unwind): it judges only the ruby-platform leaf of each unwound gem (GemBuild::ruby(purl), with the comment "A vendored gem is the ruby-platform gem: a platform build installed beside it was never the vendored copy's stand-in"). Since #2154 a vendored gem can be a set of platform builds, and the installed copy is nokogiri-1.18.10-x86_64-linux-gnu, so it is never looked at. The takeover itself (scan --mode vendored over the hosted pin) also leaves the patched copy in place.


Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    on Nov 20, 2026
  2. mikolalysenko commented on Nov 20, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Two more ways into the same state, both on Bundler 4.0.18 / Ruby 3.3.6 at 6579162: after scan --mode hosted then scan --mode vendored, socket-patch remove pkg:gem/nokogiri@1.18.10 and socket-patch vendor --revert also restore the Gemfile pair byte for byte and leave the patched nokogiri-1.18.10-x86_64-linux-gnu in vendor/bundle (the app still prints defined?(SOCKET_PATCHED) as "constant" after bundle install).


    Generated by Claude Code

  3. mikolalysenko commented on Nov 20, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-gem-vendored-unwind-platform-builds.


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedbugbughuntpm:bundlerpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions