How not to have to type "bundle exec" using Bundler binstubs & chruby?

1k Views Asked by At

I'm using chruby to manage my Ruby versions, and Bundler's binstubs to avoid having to type "bundle exec". I'm running into issues I don't totally understand. Here's what I get when I try to run the test suite in our (Rails 4, Ruby 2.2.2) application:

$ rspec             
/Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- simplecov (LoadError)
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/ruby_projects/platform/spec/spec_helper.rb:3:in `<top (required)>'
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/.rubies/ruby-2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/duncanmalashock/ruby_projects/platform/spec/controllers/admin/admin_controller_spec.rb:1:in `<top (required)>'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/bin/rspec:23:in `load'
    from /Users/duncanmalashock/.gem/ruby/2.2.2/bin/rspec:23:in `<main>'

When I run $ bundle exec rspec, the suite runs correctly. I've been looking into postmodern's gem_home, and have installed it, but it's not giving me the results I'm looking for. I'm using zsh on OSX Yosemite.

in .zshrc:

source /usr/local/share/chruby/chruby.sh
source '/usr/local/share/chruby/auto.sh'
source /usr/local/share/gem_home/gem_home.sh

export PATH=./.bundle/bin:$PATH
alias b='bundle install --path .bundle/gems --binstubs .bundle/bin'
2

There are 2 best solutions below

0
On BEST ANSWER

Just install the simplecov gem in your system (simply do: gem install simplecov) and run your test again. It will work.

But, it's a better practice to use bundle exec to run the rake or rspec commands. Because that way, you would ensure that the command is running in the context of your Gemfile.

You can add these aliases to your .zshrc file:

alias ber="bundle exec rspec"
alias be="bundle exec"

And, then run your rspec tests like this:

ber

or,

be rspec
0
On

Your problem is that bundler has installed simplecov local to the project and you are trying to run the system global rspec. That is going to look for simplecov in the system ruby library location. Using a Ruby language version manager is not going to change this behaviour. It will just use the versioned ruby's library location instead.

If you wish your versioned Ruby to use rspec and simplecov together then you need to install simplecov in your versioned ruby using whatever tool equivalent to gem that is provided.

I am not sure why you find running bundle exec of great import. One can always create an alias in .bashrc or .bash_profile. I have two for bundle exec, one is be and the other bexec. I also have aliases for bundle exec rails (brails) and bundle exec rake (break). In practice I find that these are really little different than running the customary commands.