Double loading issue with Ruby default gems

Updated . Posted . Visible to the public.

Ruby includes many standard gems Show archive.org snapshot that are bundled into the Ruby installation. Here is an example for the gem strscan that will be displayed as default:

gem list strscan     

*** LOCAL GEMS ***

strscan (default: 3.0.1)

It is still possible to have newer version of a gem installed beside the default version:

gem install strscan  
Fetching strscan-3.0.3.gem
Building native extensions. This could take a while...
Successfully installed strscan-3.0.3
1 gem installed
gem list strscan   

*** LOCAL GEMS ***

strscan (3.0.3, default: 3.0.1)

Therefore is may happen that your Gemfile.lock contains gem versions of default gems that have a newer version than the default version of your Ruby version. This might lead to double loading issues with the an error like below:

You have already activated strscan 3.0.1, but your Gemfile requires strscan 3.0.3. Since strscan is a default gem, you can either remove your dependency on it or try updating to a newer version of bundler that supports strscan as a default gem. (Gem::LoadError)

This is what happens:

  1. bundle install loads bundler before evaluating the Gemfile.lock
  2. bundler requires the newest version of a gem installed e.g. strscan 3.0.1
  3. Then bundler evaluates the Gemfile.lock and tries to install strscan 3.0.3 and load it
  4. Since strscan 3.0.1 was already loaded by bundler itself, it fails

The error message above recommends to update to a newer version of bundler, but is seems to be a generic error message and there is no fix for strscan in the upstream yet. The main discussions Show archive.org snapshot on how to handle standard gems in gemspecs in the future seems not to be finished yet. Until this issue and all the PRs in the gems that depend on standard gems is resolved, I used the following workaround in my Gemfile:

gem 'strscan', '=3.0.1' # Depend on the standard version of the current Ruby version 3.1.2 (required to be changed once Ruby is upgraded)

Alternative workarounds

Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2022-06-21 09:35)