3

I am creating a gem, which has a dependency on another published gem. In my_gem.gemspec, I have added the dependency to the other gem:

gem.add_dependency "other_gem", "~> 1.0.0"

Now, I found a feature that can be tweaked in other_gem, so I forked the repository, made my changes and committed it to the fork (It has not been pulled into the original repository).

My question is how do I tell my_gem to lookup other_gem locally? The below code snippet is not valid, as :path is not an option in add_dependency call, as mentioned in Gem Specification Reference:

gem.add_dependency "other_gem", "~> 1.0.0", :path => '/path/to/local/other_gem

Subhash
  • 3,121
  • 1
  • 19
  • 25
  • This post clarified a lot of my questions: http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/ – Subhash Jan 16 '13 at 01:51

3 Answers3

2

Locally it's much easier: while you're doing development, you can include:

gem "other_gem", :path => '/path/to/local/other_gem'

or

gem "other_gem", :git => "git@github.com:/your_github/other_gem.git"

in your gemfile, as this should override the gemspec

Max
  • 2,082
  • 19
  • 25
  • Nope, :git options falls as well. If I try to include the local other_gem in my Gemfile, gem fails with message: Bundler could not find compatible versions for gem "other_gem": In Gemfile: my_gem (>= 0) ruby depends on other_gem (~> 1.0.0) ruby – Subhash Jan 11 '13 at 07:27
  • Did you change the name or version of the other_gem? Try removing the version of the other gem – Max Jan 11 '13 at 07:34
  • Sorry. There was a mismatch between versions of gem as available on RubyGems and on its Github master branch. After correction, the approach works. I could follow the same approach in production as well, but the project will then get locked to the forked repository as a side-effect, which is not desirable. Isn't there an option to link just the gem to a forked or local repository? – Subhash Jan 11 '13 at 08:19
  • I'm sorry I don't understand what you're asking: you said that getting locked to the forked repository was bad, then asked if it was possible to link to a forked repository? – Max Jan 11 '13 at 08:58
  • Also have you considered coding it to actively override the other files, i.e http://stackoverflow.com/questions/8736451/override-a-method-inside-a-gem-from-another-gem – Max Jan 11 '13 at 09:01
  • My comment was about linking my "rails projects" to a forked repository, vs. linking "my_gem" to a forked reponsitory. The other link seems interesting; will check it out and revert back. – Subhash Jan 11 '13 at 09:09
  • Accepted, after your edit to locate the dependency in the gemfile, and override the gemspec. – Subhash Jan 16 '13 at 01:34
0

Locally it is not likely possible to give path to the gem dependency because if you are doing so that means you are imposing a restriction to the self made gem that it is depending locally to any other gem.

This is not desirable as when you will upload it, this will not work. So the solution is to add remote dependency in your own plugin's gemspec.

See my SO post for the same here.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • A small difference between my requirement and your post is that I want "my_plugin" to consider 'will_paginate', '~> 3.0.pre2.patch', which is a forked repository of will_paginate and is local. – Subhash Jan 11 '13 at 08:42
0

I would create and install a new other_gem-version, e.g. '1.0.0.Subash_fix' and use it as

gem.add_dependency "other_gem", "= 1.0.0.Subash_fix"

When there is a new official version of the gem with your patch you switch back to the official one:

gem.add_dependency "other_gem", "~> 1.0.1"
knut
  • 27,320
  • 6
  • 84
  • 112
  • Does this mean I will have to add other_gem.1.0.0.Subash_fix to rubygems.org? How will my_gem's bundle find this patched gem? – Subhash Jan 11 '13 at 08:40
  • Normally you have no authorization for rubygems.org. Your original plan was to use a local path, so it's only on your computer. You can install the gem from a local source: `gem install other_gem.1.0.0.Subash_fix.gem --local`. Then it is available. If you want to give the gem to other persons, they also need your patched version of other_gem. – knut Jan 11 '13 at 08:43
  • Correction: My idea works with require, but you already need it in the gemspec of your gem. Give me some time to think about it, but perhaps I will delete the answer if it is a blind alley. – knut Jan 11 '13 at 08:48