How to use different versions of a gem at the same

2020-03-08 08:20发布

Can I use two versions of a gem in one application?

I'm trying to use two Rails plugins to work together:

  • Pengwynn's LinkedIn for LinkedIn API calls, has an dependency on OAuth (~> 0.3.5)
  • OmniAuth for user login via multiple well-known websites, has an dependency on OAuth 0.4.0

When I try something in Rails I get this error message:

$ rails server
c:/Ruby187/lib/ruby/gems/1.8/gems/bundler-1.0.7/lib/bundler/resolver.rb:129:in `resolve': Bundler could not find compatible versions for gem "oauth": (Bundler::VersionConflict)
  In snapshot (Gemfile.lock):
    oauth (0.4.4)

  In Gemfile:
    linkedin depends on
      oauth (~> 0.3.5)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

Trying bundle update freezes my terminal.

Is it possible to use both plugins at the same time?

2条回答
Melony?
2楼-- · 2020-03-08 08:57

Using two versions of a single gem usually means: use two versions of the same class.

It's not possible without making modifications to these gems. You may try to place created classes in some module, resolve conflicts in methods imported into other classes, and so on. In general, it is not easy task, and usually the effect is not worth it.

What you should do in such cases is to ask the gem maintainers to update the dependencies, or try to do it yourself.

Maybe you can downgrade (use older version of) one of these gems, to the version in which the dependencies were the same.

查看更多
▲ chillily
3楼-- · 2020-03-08 09:08

Unfortunatelly, in Ruby the only way is to fix (sic!) those gems, so they use compatible dependencies.

In general, when building complex software in Ruby and having such a situation we - Ruby developers - can't do much. And this is really bad, as development in Ruby has to focus on this issue as well.

Instead of providing your customer new features or change requests, one has to live with Ruby so called: "gem hell".

Another major characteristic of "gem hell" is that not always the latest gem release is the good one.

  • Often, a gem does not follow semantic versioning policy, and can introduce major API modifications.
  • Often, a new gem release may introduce new bugs to features previously working.

Other programming languages have an option of handling these types of problems. Just search for "java multiple versions of same class" and you'll find lots of resources.

What I can suggest for smaller kind of applications is to:

  • be up-to-date with all latest gems' releases

What I can suggest for larger kind of applications, when above is not an option:

  • split your application into several smaller applications, services. This will separate them making risks of having "gem hell" smaller. If it happens, chances are it happens not to all of them. Also, different apps can use different gem versions.

  • switch to JRuby where those issues can be - in theory - solved via Java capabilities.

查看更多
登录 后发表回答