When using a Gemfile that doesn't list a particular gem (that _is_ however already installed on the system), with 3.1.4, Gem::Specification.find_all_by_name(gem_name) is empty, however, with 3.2.0, the installed version(s) are returned. With both versions, Gem::Specification.all_names does _not_ list the gem.
When using bundler/a Gemfile, should the results of various methods on Gem::Specification respect the Gemfile? My intuition is yes, but maybe that's wrong.
This issue causes a problem with json_schema (https://github.com/ruby-json-schema/json-schema/issues/448), which uses find_all_by_name to determine whether multi_json can be required - because find_all_by_name('multi_json') is non-empty, json_schema assumes it can require multi_json, but the require fails as the gem is not listed in the Gemfile.
To reproduce create repro.rb containing:
puts "rubygems version: #{`gem env version`}"
puts "multi_json versions: #{`gem list multi_json`}"
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'json-schema', '= 2.8.1', require: false
end
puts "all_names: #{Gem::Specification.all_names}"
puts "find_all_by_name: #{Gem::Specification.find_all_by_name('multi_json').map(&:version)}"
require 'json-schema'
puts 'Loaded json-schema successfully!'
For 3.1.4:
$ gem update --system 3.1.4
...
$ ruby repro.rb
rubygems version: 3.1.4
multi_json versions: multi_json (1.15.0, 1.14.1, 1.11.2)
all_names: ["public_suffix-4.0.6", "addressable-2.7.0", "bundler-2.1.4", "json-schema-2.8.1"]
find_all_by_name: []
Loaded json-schema successfully!
For 3.2.0 (also tested with 3.2.4 - same result):
$ gem update --system 3.2.0
...
$ ruby repro.rb
rubygems version: 3.2.0
multi_json versions: multi_json (1.15.0, 1.14.1, 1.11.2)
all_names: ["public_suffix-4.0.6", "addressable-2.7.0", "bundler-2.2.0", "json-schema-2.8.1"]
find_all_by_name: [#<Gem::Version "1.15.0">, #<Gem::Version "1.14.1">, #<Gem::Version "1.11.2">]
Traceback (most recent call last):
3: from repro.rb:15:in `<main>'
2: from repro.rb:15:in `require'
1: from /Users/owen/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/json-schema-2.8.1/lib/json-schema.rb:4:in `<top (required)>'
/Users/owen/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/json-schema-2.8.1/lib/json-schema.rb:4:in `require': cannot load such file -- multi_json (LoadError)
I will abide by the code of conduct.
Hello, thanks for the report.
This was broken by https://github.com/rubygems/rubygems/pull/2711. That PR made sense to me but only brought the benefit of "more correct code", and... it broke things :disappointed:. I'll try to refactor further to unbreak your case, or just revert the offending PR if I can't figure it out.
Yeah, giving it some more thought, that PR was just incorrect.
We only want to lazily load stubs from the standard rubygems locations if we don't yet have a fully loaded "gem set". Once the full world of gems is setup, either by reading available gemspecs from the standard system locations, or by bundler setting them directly, that set of gems should never change again.
I'll revert that PR and add a test making sure that we don't break this again.
Thanks for the quick response @deivid-rodriguez 馃憤
https://github.com/rubygems/rubygems/pull/4262/ should fix this!
Most helpful comment
Thanks for the quick response @deivid-rodriguez 馃憤