[bundler-checksum] Prevent Gem::LoadError by lazy loading json
What does this MR do and why?
- JSON is lazy loaded because when
require 'json'is top-of-file, it executes while the Gemfile is still being evaluated. - That eagerly activates whatever
jsongem RubyGems finds first (e.g.,2.15.2), and when multiplejsonversions are installed, it may choose a version that conflicts withGemfile.lock. - When Bundler later tries to activate the locked
jsonversion (e.g.,2.13.1)check_for_activated_spec!raisesGem::LoadError, because a different version is already activated. - Mitigated by postponing
require 'json'until after Bundler finishes setup (i.e., right before parsing the checksum file).
References
Fixes this error encountered while attempting to re-checksum for a new gem addition:
Before
➜ bundle exec bundler-checksum init
bundler: failed to load command: bundler-checksum (/var/home/pboling/.local/share/mise/installs/ruby/3.3.9/lib/ruby/gems/3.3.0/bin/bundler-checksum)
/var/home/pboling/.local/share/mise/installs/ruby/3.3.9/lib/ruby/gems/3.3.0/gems/bundler-2.7.1/lib/bundler/runtime.rb:317:in `check_for_activated_spec!': You have already activated json 2.15.2, but your Gemfile requires json 2.13.1. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
from /var/home/pboling/.local/share/mise/installs/ruby/3.3.9/lib/ruby/gems/3.3.0/gems/bundler-2.7.1/lib/bundler/runtime.rb:25:in `block in setup'
# ...
After
➜ bundle exec bundler-checksum init
Initializing checksum file /var/home/pboling/src/forks/gitlab/Gemfile.checksum
Using CFPropertyList==3.0.7
Using RedCloth==4.3.4
Using acme-client==2.0.26
# ...
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Edited by Peter Boling