voxpupuli / json-schema

Ruby JSON Schema Validator
MIT License
1.52k stars 241 forks source link

Failure/Error: require 'json-schema-rspec', cannot load such file -- multi_json #448

Closed nbibler closed 3 years ago

nbibler commented 3 years ago

In Ruby 2.7.2, Rubygems 3.2.0, and Bundler 2.2.0, the multi_json check fails in projects which do not have a multi_json dependency in their dependency tree.

https://github.com/ruby-json-schema/json-schema/blob/2f95d53d62428d603601a1346d6e12a70e7a505d/lib/json-schema.rb#L3-L4

When executed under Bundler, the application Rubygems are isolated to only those in the dependency tree. The Rubygems check performed above (Gem::Specification::find_all_by_name('multi_json').any?) returns true even though the Rubygem is not available to the application to load (requiring it results in a LoadError).

Rather than asking Rubygems if the library exists, it is more common to simply attempt to load the library and handle a LoadError. See Rails, for examples.

obfuscoder commented 3 years ago

This problem has popped up in one of our projects as well since yesterday. We use ruby 2.5.5 in that project. Not sure yet why it started appearing.

lionelperrin commented 3 years ago

It is likely that this issue happens because the gem multi_json is present on your system. The test Gem::Specification::find_all_by_name('multi_json').any? thus returns true but the gem cannot be loaded by bundler because the dependency to this gem is not declared.

nbibler commented 3 years ago

@lionelperrin: Agreed.

Ultimately, this library is using Gem::Specification to determine if multi_json is available to be used. That check is incorrect, as you've noted. While the gem may be installed, Bundler may not make it available to the running application, based on the application's Gemfile. Because this check is wrong, the application errors when multi_json is not available via the Gemfile (not checked here).

owst commented 3 years ago

We've encountered the same issue when upgrading rubygems to 3.2.X. I have (several versions of) multi_json installed, but my Gemfile does not list it. Rubygems 3.1.4 works, but 3.2.0 and 3.2.4 both error failing to require multi_json. I'll raise an issue to report the bug (or at least request clarification) with rubygems and link to this issue.

owst commented 3 years ago

FYI @nbibler this was caused by a bug in rubygems - the fix has been released in 3.2.5 (and 3.2.6 is now also available) upgrade with gem update --system 3.2.6

nbibler commented 3 years ago

Thanks @owst!