How to override gem dependency?

2019-04-30 00:25发布

问题:

I have 2 gems that depend on conflicting versions of hashie (one requires ~> 1.2.0 and the other requires 3.3.1):

Bundler could not find compatible versions for gem "hashie":
  In Gemfile:
    restforce (>= 0) ruby depends on
      hashie (~> 1.2.0) ruby

    omniauth (>= 0) ruby depends on
      hashie (3.3.1)

I'd like to keep both gems and use the higher version of hashie. Is there a way for me override one of the gem dependencies?

回答1:

The standard advise is to use the higher version compatible.

I do this way, but I think bundler has a defined command for it.

First remove the line of gem 'omniauth' at your Gemfile. Run bundle install, then you must add again the line with gem 'omniauth' to your Gemfile, run bundle install again.

If you look at Gemfile.lock, this install hashie 2.0.5, the higher compatible.

edited: why it works?

First I check omniauth dependencies with hashie: hashie < 4, >= 1.2, then the same with restforce: hashie < 2.1, >= 1.2.0. At this point, I know that any version of hashie between 1.2 and 2.0.x must work. Then we must to remove the constraint at Gemfile.lock of continue using hassie 3.3.1, removing 'omniauth' from Gemfile it's done. After that, when install restforce, the bundler find the new version compatible with restforce '2.0.5'. And when you add again omniauth bundler don't update dependencies that are accomplished.



回答2:

I think bundle update is actually what you were after. That sorts out dependencies and installs different versions of gems if required, giving you the most up-to-date gems possible.

Be careful however, as updating gems can introduce compatibility issues.