Skip to content

Hosted gem rollback / remove leave the patched gem installed when the gem home has no cache/ dir (Bundler 4.1 prune, rm -rf ruby/*/cache), and the "next package-manager install" note does not fix it #2092

Closed

Description

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

Summary

When the gem home's download cache is missing (vendor/bundle/ruby/<abi>/cache/), a hosted scan still replaces the installed gem with the patched build, but rollback / remove do not put the upstream build back. They exit 0, restore Gemfile and Gemfile.lock, and print:

Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.

That remedy does not work for Bundler. The next bundle install (plain or frozen) prints "Bundle complete!" and keeps the patched copy, and bundle doctor reports no issues. The app keeps loading patched code under a lock that pins the upstream checksum. No upstream_gem_stale_install warning is raised, and remove --json has no warnings at all.

The cache dir is missing in ordinary setups:

  • Bundler 4.1.0 (released 2026-11-16) adds the prune setting (bundle config set prune true, BUNDLE_PRUNE=1), which deletes that dir after every install.
  • The Dockerfile Rails generates runs rm -rf "${BUNDLE_PATH}"/ruby/*/cache.
  • gem cleanup, or a CI cache that leaves the dir out.

Impact

rollback is supposed to undo the scan. Here it restores the two files but leaves every hosted-patched gem installed, and names a remedy that changes nothing. A user who rolls back because a patch broke the app still runs the patched code, and nothing they are told to do fixes it. vex correctly says there is nothing to attest, so the project is in a state no command reports.

Repro

Needs a patch for rack 2.2.8 (the run used a mock patch API and registry: the upstream .gem with one line appended to lib/rack/version.rb that defines Rack::SOCKET_PATCHED).

mkdir repro && cd repro
printf 'source "https://rubygems.org"\ngem "rack", "2.2.8"\n' > Gemfile
bundle config set --local path vendor/bundle
bundle config set --local prune true     # Bundler 4.1.0; on older Bundlers: rm -r vendor/bundle/ruby/*/cache after the install
bundle install
git init -q . && git add -A && git commit -qm init

socket-patch scan --mode hosted --yes
#   Replaced the installed copy of 1 gem with the patched build: rack 2.2.8.
bundle exec ruby -e 'require "rack"; p defined?(Rack::SOCKET_PATCHED)'   # "constant"

socket-patch rollback --yes
#   Restored pkg:gem/rack@2.2.8 to its upstream registry entry
#   Note: 1 unwired package keeps its patched bytes in installed trees until the next package-manager install.
git status --short                        # clean: the pair is restored byte for byte
bundle install                            # Bundle complete! 1 Gemfile dependency, 1 gem now installed.
bundle exec ruby -e 'require "rack"; p defined?(Rack::SOCKET_PATCHED)'   # "constant"  <- still patched

Removing the cache dir after the scan instead of before it gives the same result.

What does bring the upstream gem back: bundle install --redownload (Bundler 4.1.0 with prune), or rm vendor/bundle/ruby/*/specifications/rack-2.2.8.gemspec && bundle install. bundle pristine fails ("Cached gem … does not exist").

Expected

docs/ecosystems.md, "Ruby gems: gems already installed": after the scan replaces the installed copies, "rollback and remove do the same with the upstream gem." CLI_CONTRACT.md, "Gem stale-install guard", says the unwind "does the same in the other direction" and that an installed copy left on the patched build is reported as upstream_gem_stale_install. The same section says the scan direction handles "a pruned cache/" by judging the gem dir against the patch record, which is why the scan replaces the copy here.

So either the unwind replaces the copy as the scan did, or it warns upstream_gem_stale_install with a remedy that works. The "until the next package-manager install" note should not be printed for a gem, since Bundler keeps a gem it has already installed.

Actual

The unwind judges an installed copy only by <home>/cache/<leaf>.gem. With no archive the copy is skipped without a warning, and the generic reinstall_required note is all the user sees.

Versions

Bundler Ruby Setup Result
4.1.0 3.3.6 prune true reproduces
4.0.18 3.3.6 cache dir removed before the scan reproduces
4.0.18 3.3.6 cache dir removed after the scan reproduces
4.0.18 3.3.6 remove <purl> instead of rollback reproduces
2.6.9 3.3.6 cache dir removed before the scan reproduces
4.1.0, 4.0.18 3.3.6 cache dir present does not reproduce ("Replaced the installed copy of … with the upstream build")

Tested on main 63f791d (5.1.0), Linux. Nothing here is platform-specific, so macOS and Windows should behave the same.

Suspect code

  • crates/socket-patch-cli/src/commands/gem_refresh.rs:645 (stale_after_restore): let Some(archive) = home_cache_archive(&pkg.path) else { continue; } skips a copy with no archive. The scan-side probe falls back to the patch record's afterHash map in that case; the unwind has no fallback (the record's beforeHash map, or the restored lock's checksum against a downloaded upstream gem, could judge it).
  • crates/socket-patch-cli/src/commands/rollback.rs:2573: the reinstall_required note says "until the next package-manager install", which is not true for Bundler.

Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    ux
    on Nov 16, 2026
  2. mikolalysenko commented on Nov 18, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Not claimed this run, on purpose. The fix belongs in stale_after_restore (gem_refresh.rs): judge an installed copy with no cached archive by the patch record's afterHash map. #2122 (open, awaiting review) adds exactly that mechanism to the same function for the vendored unwind (records + holds_patch). A second PR from main would duplicate it and conflict, so this should be built on top of #2122 once it merges, by passing the hosted unwind's patch records to after_restore.


    Generated by Claude Code

  3. mikolalysenko commented on Nov 18, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-hosted-gem-unwind-pruned-cache. #2122 has merged, so this builds on its records / holds_patch mechanism as planned above.


    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:p1ux

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions