Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the "gemspec" rule in Bundler, while still using a local checkout of a gem?

Tags:

Scenario: I am developing a gem gem-foo, using Bundler. I have a dependency on gem-bar, which I have checked out locally for development. I can declare this dependency in the Gemfile as:

gem "gem-bar", :path => "/path/to/local/gem-bar"

However, when developing a gem the "best practice" seems to be to define the required gems in the gem-foo.gemspec via:

s.add_runtime_dependency 'gem-bar'

and then simply include the dependencies in the Gemfile using:

gemspec

What I would like to do is to add the runtime dependency on the gem in the gemspec, but make some local modifications to gem-bar and have a bundle install use the local development checkout. Assume the case where I control both gems, and am hacking on them somewhat concurrently, and I'll be pushing the new versions simultaneously. I could just comment out the s.add_runtime_dependency in the gemspec and add it with the local path in the Gemfile, but that feels... error-prone? The gemspec should always declare this dependency, and the Gemfile should be able to override the location Bundler should use for the gem. However, if you actually do this, you get the following error:

You cannot specify the same gem twice coming from different sources. You specified that gem-bar (>= 0) should come from an unspecfied source and source at /path/to/local/gem-bar

Ideally, I'd just have some sort of local override, since I never want the case where I accidentally push the gem with the requirement of a local gem-bar at a specific path. That would be dumb.

Does anyone have a good solution to this use case?

like image 881
agnoster Avatar asked Dec 14 '10 14:12

agnoster


People also ask

How does Gemfile work?

A Gemfile is a file we create which is used for describing gem dependencies for Ruby programs. A gem is a collection of Ruby code that we can extract into a “collection” which we can call later. It lets you specify which gems you want to use, and which versions of these gems to use.

How to use Gemfile in Ruby?

run the command bundle install in your shell, once you have your Gemfile created. This command will look your Gemfile and install the relevant Gems on the indicated versions. The Gemfiles are installed because in your Gemfile you are pointing out the source where the gems can be downloaded from.

What's Gemfile?

A Gemfile is a file that is created to describe the gem dependencies required to run a Ruby program. A Gemfile should always be placed in the root of the project directory.


2 Answers

A few people have found this problem, Yehuda Katz has said he would happily accept a patch: http://groups.google.com/group/ruby-bundler/browse_thread/thread/d4215c4930a63ffc?pli=1

As the best workaround, comment out the gemspec line in your gemfile and suffer some duplication?

Update:

It looks like you don't have to wait - https://github.com/carlhuda/bundler/commit/03378109d

The commit message: "Make it possible to override a .gemspec dependency's source in the Gemfile"

hooray!

like image 186
gunn Avatar answered Sep 16 '22 13:09

gunn


For now, what I've done is to disable gem requirements in the gemspec when a given environment variable is set, then in the Gemfile, add a gem definition that points to my local filesystem.

I wrote about my whole workflow here: http://numbers.brighterplanet.com/2010/07/28/bundler-to-the-max/ Note: I also use jeweler, which complicates things greatly.

like image 27
dkastner Avatar answered Sep 17 '22 13:09

dkastner