Using bundler with "--path vendor", why are gems specified with ":git" not locally vendored? - ruby

I'm using bundler and have a Gemfile that looks like this:
source 'http://rubygems.org'
gem 'sinatra', '1.3.1'
gem 'httparty'
# ...etc...
gem 'my_custom_gem', :git => 'git#github.com:me/my_custom_gem.git'
When I run bundle install it fetches the necessary gems, including my custom gem, and installs them in the system gem directory. So far, so good. However, an issue arises when I try to vendor them into a project-local directory. When I run
bundle install --path vendor
It creates the "vendor" directory in my project root and installs all the regular gems there. So I see directories like
vendor/ruby/1.8/gems/sinatra-1.3.1
vendor/ruby/1.8/gems/httparty-0.8.1
...etc...
But it does not vendor the gem specified with a 'git' parameter. I expect to see but do not see anything like
vendor/ruby/1.8/gems/my_custom_gem-1.0.0
It continues to use the system-installed version of this gem. Any explanation for this? Any clean way to get this custom gem vendored as well?

Not supported right now, hopefully coming in Bundler 1.1:
https://github.com/carlhuda/bundler/issues/67
For now you'll have to do:
cd vendor/ruby/1.8/gems/
git clone git://github.com/foo/foo.git
or similar

Related

Can't install gem using Bundler's Rakefile install task when developing a custom gem

I'm developing a couple of private gems and I think I don't understand correctly the PATH/GEM_PATH and/or Bundler/RVM installation flow, would love if someone could chip in.
I have a repository with two gems (A & B for simplicity sake). I've developed the gems using the scaffolding + following the guidelines provided by this bundler tutorial.
Thanks to the Bundler project I have a few Rakefile tasks like rake build, rake install, rake install:local and rake release. Because of the private nature of these gems I can't release them to RubyGems (and we haven't looked into hosting our rubygems).
My machines are using RVM to manage ruby versions and Bundler version 1.15.1
What I want to do: Assuming a new machine/developer trying out the project, ideally we would cd into each of the subfolders (currently 2, gem A and gem B), run rake install and after that we should have the gems available system wide for the current user.
What is happening: The gems are built and work properly, but they are only available inside the subfolder of each gem i.e. gem A is only available inside the subfolder A and gem B is only available inside subfolder B.
What I've tried: So, after rake build/install/install:local a new .gem file is generated under pkg. I've tried to manually install the "compiled" file using gem install pkg/A.gem, gem install --local pkg/A.gem and gem install --local --user-install pkg/A.gem without success. (there are plenty of SO questions/answers about this)
I believe this has something to do with the PATH variables, but like I said before I don't fully understand the way they are managed. I get the following results from these commands:
# Our gem
> gem which A
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/A-0.1.8/lib/A.rb
# Pry, available globally
> gem which pry
/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/pry-0.11.1/lib/pry.rb
I've been lost and frustrated for far too long now, any help is appreciated. Also open to hear suggestions of better private gem installation flows :)
Yes, it has something to do with your PATH variables. Your installation seems to be good.
I advise you to first affirm your gems installation path with:
echo $GEM_HOME
The double check your PATH to ensure its present and also confirm that the GEM home is also where the gem got installed into from the rake install
echo $PATH
If not, put it in your path and you should be fine with something like this:
echo PATH=$PATH:$GEM_HOME >> ~/.bashrc
source ~/.bashrc
Build your gem as per that guide you linked. You should end up with a gem file. Distribute this as you see fit (I use rsync/crontab to download newer gem versions but anything goes). User can install the gem as follows:
gem install --user-install /path/to/your/file.gem
This will install the gem in the user's ~/.gem/ruby/<version>/gems/<your-gem-name> directory.
Tried it with an empty gem (foodie, as in that example guide) and it works fine. But if you don't specify the --user-install parameter it will try to install in the system ruby dir (/usr/lib/ruby/gems...)

Bundler: using shared gem when it exists, rather than downloading from gem server

Say, I have Gemfile like following.
source "GEM_REPOSITORY"
gem 'gem_A'
# gem_A has no additional dependency
gem 'gem_B'
# gem_B depends on gem_B_1 and gem_B_2
When I run bundle install, I want Bundler to do the following.
If a gem already exists in "local system-wide gems", it copies the gem from local.
If a gem doesn't exist in local, it looks for GEM_REPOSITORY.
I looked for some related articles, and found some of likely-answers like
Ruby Bundler multiple sources in Gemfile
SOURCE PRIORITY
But none of the above looks like the answer for me.
Using source repository priority does't work. Because in the example above, if dependent gem (say, gem_B_1) exits in local but the target gem (gem_B) doesn't exist in local, it'll download both of above from the remote repository.
Are there any work around for doing this?
If not, don't you guys think it's necessary considering the cost of the implementation and the effect?
This is the current behavior.
When running gem install, directly or via bundle install, gem will first build a dependency graph with all the needed gems. If the gem is found locally it will use it, otherwise it will try to download it from the specified source.
If you want, try it yourself.
bundle gem gem_a
bundle gem gem_b
cd gem_a
vim gem_a.gemspec
add
spec.add_dependency 'multi_json', '~> 1.10.1'
or any dependency you want to the gem and run bundle install.
cd ../gem_b
vim Gemfile
and add
gem 'gem_a', path: '../gem_a'
then run
bundle install --verbose
you will see that the multi_json or whatever dependency of gem_a uses the local version and does not download anything.
This is of course also true for gems from remote sources.

How could I install Gems from git repositories?

I need to install gem form git repository. repository contains .gemspec file. In my gem file i have following code:
gem 'echo_server', :git => 'http://127.0.0.1/org/echo_server.git'
When i am running bundle install gems are installer in .bundeler and not showing in gem list.
my question is:
How the gem will be available in system, so that i can use in require ?
There are some similar SOQ but it does't help me.
It is not showing up when you type gem list because it is not being installed like a regular gem. You can require it like you would any other library because Bundler knows about it and will set it up for you. You should see it in your $LOAD_PATH:
$LOAD_PATH.grep(/nameofgem/)
See the Bundler documentation on this for more information.
If instead you want to install it as a regular gem from the Git repository, you can clone the repository and then build and install the generated gem. For example:
gem build echo_server.gemspec
gem install echo_server-X.Y.Z.gem
There is also specific_install.

Bundler does not install from stash private repo, but reports that it does

My bundle file doesn't appear to be pulling down a gem from a private repository properly.
Inside of my Gemfile, I have:
group :internal do
gem 'private', git: 'ssh://git#internalserver.org:<port>/gems/private.git'
end
This runs, and verbose logging produces:
Updating ssh://git#internalserver.org:<port>/gems/private.git
Cloning into '/Users/<username>/.rvm/gems/ruby-2.0.0-p247/bundler/gems/private-ddec73caf50f'...
done.
When I navigate to /Users/<username>/.rvm/gems/ruby-2.0.0-p247/bundler/gems/, I see the correct repository cloned properly, with a gemspec with the correct name.
When bundler is finished running, gem list does not show the private gem. It produces an error when I attempt to require it.
I tried deleting the Gemfile.lock file in the repository and rerunning, and that did not work. All of the public gems in the Gemfile install correctly.
Relevant version numbers / software:
Bundler version 1.3.5
rvm 1.23.14
ruby 2.0.0p247
Atlassan Stash
Git gems are a Bundler-specific extension to Rubygems. The gem command does not know about these, so they're not listed by gem list. You can run bundle show to see the list of gems that are recognised by Bundler, which will include git gems.
To require the gem, you'll need to be sure that the load path is set up correctly by Bundler. There are three ways to do this:
Call require 'bundler/setup' in your app. This is typical for Rails apps. More on Bundler.setup
Call bundle exec <command> to run the command. This is more common when running commands from a gem, such as rake or rspec. More on bundle exec
Create binstubs for commands that you run frequently.
See http://bundler.io/v1.5/git.html for more information on git gems.

Bundling local gem (that I'm developing) does not seem to include lib directory (using rvm)

I'm trying to develop a gem locally, and have installed it with Bundler.
My Gemfile looks like this:
source "http://rubygems.org"
gemspec
And my gemspec is a standard gemspec file.
I can install the gem with 'bundle install' in the directory, and i see the local gem and all it's dependencies install:
bundle install
Using rack (1.3.4)
Using tilt (1.3.3)
Using sinatra (1.3.1)
Using {my gem} (0.0.2) from source at .
Using bundler (1.0.21)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
However, when I do a 'gem list', my gem is not included in the list of gems - which is my guess as to why my bin directory does not appear in the path. Is there a way to test a local gem and include it in the list of installed gems using bundler, so that the bin directory properly works?
Easiest way to get rid of bundler: command not found: {your bin executable}:
git add bin/* # git-ls-files will now list your bin executables.
bundle install
# No git-commit necessary.
bundle exec <MY_BIN_EXECUTABLE>
gem list shows your system installed gems, not the gems in your Bundle (this are often the same but not always--as in this case). When you're using Bundler, you should always execute gem executables with bundle exec so that Bundler can set up the environment for you. So, if you have a binary called, for example, mygem, you should use bundle exec mygem.
See more info at Bundler's site or in the manpage.
[Edit]
Also be sure that your gemspec includes a bin directory! Common convention is to create a directory called bin at the same level as your lib directory, put your binaries in there, and then add this as the directory in your gemspec. If you don't do this, Bundler won't expose your binaries!
I had this problem too.
Make sure the executables and default_executable lines don't contain 'bin/'. Then:
git add add . # You can be more precice if you want.
git commit -m "My lousy commit message."
bundle install
bundle exec <binaryname>

Resources