2

I want to update the json gem to version 2.0.2 in my app. It is currently loaded as a dependency at version 1.8.3.

So, I added

gem 'json', '>= 2.0.2'

to my Gemfile and I type

bundle update json

and I get

Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Resolving dependencies...
Bundler could not find compatible versions for gem "json":
In Gemfile:
json (>= 2.0.2)

paperclip (~> 4.2.0) was resolved to 4.2.4, which depends on
  activesupport (>= 3.2.0) was resolved to 4.2.7, which depends on
    json (>= 1.7.7, ~> 1.7)

Which doesn't make sense to me because I thought the json (>= 1.7.7, ~> 1.7) is saying that activesupport 4.2.7 is dependent on at least version 1.7.7 of the json gem, so 2.0.2 should not be a conflict, no?

What is the correct interpretation here?

What steps can I take so that I can add json v2.0.2 gem to my rails v4.2.7 app successfully?

Streamline
  • 2,040
  • 4
  • 37
  • 56

2 Answers2

4

The json requirement has two parts. The first is >= 1.7.7 meaning the Gem must be greater than or equal to 1.7.7. 2.0.2 is, in fact, greater than 1.7.7, so you satisfy that requirement. However, the second part of that is that it must also be ~> 1.7 (pronounced "tiddle-wakka") is what's called a Pessimistic Constraint. It required that the first elements of the version number match but the last can be greater than or equal to the number given. So, for example, 1.7.0 would satisfy the constraint. As would 1.7.9 or 1.9.9, or even 1.423.8. However, 2.0.0 would fail that constraint because the major version number doesn't match the given value of "1".

Evan Keller
  • 101
  • 3
  • Thanks for the explanation, now the conflict message makes sense. So this is saying it will never be possible to use json v2.0.2 with rails v4.2.7? – Streamline Sep 27 '16 at 22:40
0

The ~> is what is called a pessimistic version constraint. Check out here:

http://guides.rubygems.org/patterns/#pessimistic-version-constraint

You'll find a section there on compound requirements which is basically what it looks like activesupport is doing for its json requirement.

Long story short, the ~> basically makes the json version match only minor versions of json 1.7 where the patch version is 7 or higher.

There is some discussion on whether or not to implement a bundler option for overriding gem versions specified in gemspecs here: https://github.com/bundler/bundler/issues/4552, but for now, there is no such option...

So, it looks like the only thing you could possibly try is upgrading activesupport to activesupport 5 or higher (where they got rid of the json dependency), which could very well break your dependencies much in this same way if Rails has minor version pinning for activesupport and the rest of its gem dependencies.

photoionized
  • 5,092
  • 20
  • 23