15

I have created a gem(X) and it's not published. Now I am creating an another gem which will add the gem X as a dependency like this :-

s.add_dependency "X"

in gemspec file.

since the gem(X) is not on rubygem or git or rubyforge, the bundle install command throws the error:

could not find the gem X**

I think specifying the path for X will do the trick, but how?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
vikram
  • 423
  • 4
  • 19

2 Answers2

22

I think I see what @bor1s was pointing to in the SO post about git references.

The gemspec file is about dependencies for publishing your gem. Your Gemfile sets up your local environment for development, usually by calling gemspec.

In your case your gemspec should have this

s.add_dependency "X"

And then your Gemfile would look something like this (make sure the local reference is after the gemspec call):

source "http://www.rubygems.org"
gemspec
gem "X", :path => "/Users/vikram/source/X"

Quoting from Yahuda Katz's blog post about gemspec and Bundler

If you find that you need to develop against a gem that hasn’t yet been released (for instance, Rails often develops against unreleased Rack, Arel, or Mail gems), you can add individual lines in the Gemfile that tell bundler where to find the gems. It will still use the dependencies listed in the .gemspec for dependency resolution, but it now knows exactly where to find the dependency during gem development.

source "http://www.rubygems.org"

gemspec

# if the .gemspec in this git repo doesn't match the version required by this
# gem's .gemspec, bundler will print an error
gem "rack", :git => "git://github.com/rack/rack.git"

You wouldn’t want to include this information in the .gemspec, which will eventually be released to Rubygems after the in-development gem has also been released. Again, this makes the overall system more resilient, as it doesn’t depend on transient external URLs. This information is purely something used to set up a complete environment during development, which requires some precision.

jwadsack
  • 5,708
  • 2
  • 40
  • 50
  • This answer was very helpful. Thank you. In my situation I had gem (Y), which depends on gem (X), however, I wanted to take this a step further and put the local development version of gem Y into my Rails application as well. I set things up as suggested and I was able to bundle gem (Y) with no problems. However, simply adding gem 'y', path: "path_to_y" to the Rails Gemfile was not enough. Bundling the Rails app would fail due to bundler not being able to find gem (x). I had to also include gem 'x', path: "path_to_x" even though it's not a direct dependent. – WebDev Nov 22 '14 at 03:28
  • @CorlewSolutions that is correct. _Any_ local dependencies need to be specified in the `Gemfile` read by `Bundler`, in this case your Rails application `Gemfile`. `Bundler` does not read a dependent gem's `Gemfile`, it only looks at `.gemspec` files within referenced gems. – jwadsack Nov 22 '14 at 17:19
  • I don't understand how this is supposed to work. Bundler errors out during the gemspec call when it can't find the gem and doesn't get to the part where the github definition is placed. – Peter P. Jan 21 '16 at 02:46
  • @PeterP. It should load all the definitions, merge them, then try to find the gems. Is the error that it can't find the gem in any of the sources? – jwadsack Jan 22 '16 at 18:57
  • Have to agree with @PeterP.: This solution does NOT work. Using the solution from this answer I get: `Could not find gem 'my_required_gem', which is required by gem 'my_gem', in any of the sources.` – Nico Feb 01 '19 at 13:14
  • @Nico sorry to hear that this didn't work. I know we've used it many times. I wonder if you get that error when you are trying to specify a dependent gem that has not yet had any version pushed to a gem repository. That is, for a newly created dependent gem that only exists on your system. – jwadsack Feb 01 '19 at 17:59
  • Ah, seems my case is a slightly different one. In my case the dependent gem is in a private repository, using an oauth github url. Wonder how to get that setup to work. – Nico Feb 02 '19 at 21:59
  • @nico then you'll need to include a source for that private repo. Maybe put the `gem` [inside a `source` block](https://bundler.io/v2.0/guides/using_bundler_in_applications.html#sources)? – jwadsack Feb 03 '19 at 03:53
  • Unfortunately using source doesn't work, I need to use the git parameter with an oauth token url. – Nico Feb 04 '19 at 07:44
0

I think you can find an answer for your question here: Gem dependency

Hope it will help.

Community
  • 1
  • 1
bor1s
  • 4,081
  • 18
  • 25