[ACCEPTED]-Ruby Gemspec Dependency: Is possible have a git branch dependency?-gemspecs

Accepted answer
Score: 47

This is not possible, and likely never will 10 be because it would be rather heavy-handed 9 for RubyGems to allow gem developers to 8 require that users have a specific version 7 control system installed to access a gem. Gems 6 should be self-contained with a minimal 5 number of dependencies so that people can 4 use them in as wide an array of application 3 as possible.

If you want to do this for your 2 own internal projects, my suggestion would 1 be to use Bundler which supports this quite well.

Score: 14


According to a commenter, this is no longer 12 true. Prior information retained for historical 11 context.

Duplicating the reference to a gem 10 in Gemfile and .gemspec now appears to raise 9 a warning message in Bundler, so this answer 8 would appear to be no longer true.

Outdated info

This article 7 by Yehuda Katz cleared up similar confusion 6 for me. It says that, for use in development 5 only, it's best to add the git stuff into 4 the gemfile, but that bundler will still 3 use the dependency/version info from the 2 gemspec (seems magical to me, but I trust 1 Yehuda).

Score: 7

I just was trying to figure this problem 14 out as well. And I just came up with the 13 following solution (which I'm not sure if 12 your publishing your gem or have rights 11 to redistribute that oauth2 gem).

In your 10 gem that requires oauth2 gem run this.

git submodule add git@github.com:lgs/oauth2.git lib/oauth2

If 9 you require a different branch than the 8 default

cd lib/oauth2 && git checkout <branchname_or_ref>
cd .. && git add lib/oauth2
git commit -m "adding outh2 submodule"

In your gemspec add this above your 7 require version line

$:.push File.expand_path('../lib/oauth2/lib', __FILE__)

Also you'll need to 6 add all of the oauth2 gem's runtime dependencies 5 to your gemspec. I haven't figured out a 4 way around this yet.

This is what I did, and 3 it works for us because our gem is required 2 via git so I'm not sure if this would work 1 for a rubygems published gem.

Score: 2

I found a work-around pretty straight forward:

Say 8 your are in a project P and you want to use 7 the self made gem tools which itself uses an 6 OS gem oauth2.

If you made a patch within oauth2 and 5 need that patch in your gem tools, you won't 4 be able to fix this issue in the gem according 3 to the accepted answer.

However, you can speficy the version 2 you want within your projet P's Gemfile, and 1 this will be the version used by tools on runtime:

gem 'oauth2', github: 'lgs/oauth2'

Here is a real life example of mine.

Score: 1

I was facing similar issue and here is what 11 I found. You cannot add git branch directly 10 for some other gem, However you can acheive 9 this another way. You can define a private 8 gem with repository link and branch name 7 in gemfile of you custom gem i.e

gem 'gem_name', '>=0.1.1', git: 'repository_link ', branch: 'brnach_name'

and run 6 bundle install

Now you can mention it in gemspec file, no 5 need to add version as it will already pick 4 from Gemfile.lock

spec.add_runtime_dependency 'sms_service'

Note: Make sure you keep gemspec at 3 the bottom in Gemfile. So, it will first 2 install necessary gems and than add them 1 as dependency to your gem.

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'sms_service', '>=0.1.1', git: 'repository link', branch: 'branch_name'


More Related questions