Adding binstubs

I want to run specs in vim using the vim-rspec mappings provided in the vimrc from the Thoughtbot dotfiles.

Initially my problem with doing so was that the rspec command wasn’t being recognized in my shell - it required I prefix it with bundle exec.

So then I started investigating binstubs. I ended up running bundle install --binstubs to generate binstubs for all my gems in the project. In order to run it without the /bin prefix I included export PATH="./bin:$PATH" in my .zshrc.local file.

I purposely did not include the ./bin/stubs directory because from what I understood, Thoughbot are moving towards the Rails convention of simply using ./bin?

The problem I’m facing now is that whenever I try to run specs in vim I’m greeted with the error below:

rspec spec/features/user_completes_signup_steps_spec.rb
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- bundler/setup (LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from ./bin/rspec:14

shell returned 1

Any ideas on what might be the issue? It doesn’t seem to be able to require 'bundler/setup' in the .bin file?

It looks like Vim is using the system ruby on OS X. Assuming you are using rbenv or rvm to manage ruby versions this is almost certainly not what you intended.

Do you happen to use zsh as your shell?

Yes, sorry I should have mentioned that I’m using rbenv along with zsh as my shell. I have a .ruby-version file in my project directory which explicitly states the Ruby version so I thought that might solve it but no luck.

I guess I’m wondering how do I get Vim to use the rbenv ruby version?

Do you have something like this in your .zshrc? It should help.

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Or add it to ~/.zshrc.local, because you use thoughtbot dotfiles.

I tried adding the export PATH line you suggested but it reverted back to the original issue which was that the rspec command wasn’t being recognized.

This is what my ~/.zshrc.local file looks like:

# recommended by brew doctor
export PATH='/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin' 

# Invoke commands without the bin/ prefix
export PATH="./bin:$PATH"
# export PATH="$HOME/.rbenv/bin:$PATH" 

eval "$(rbenv init -)"

Try this, if you haven’t already done it: sudo mv /etc/{zshenv,zshrc}

It fixes a load order issue for zsh configuration file on OS X. It’s part of the thoughtbot laptop setup script.

Ha! I’ve forgot to check my notes for rbenv before. I had same issue and if I remember correctly this solved it:

# setup  with non interactive shell (for vim.rails)
# https://github.com/dotphiles/dotzsh#mac-os-x
sudo chmod ugo-x /usr/libexec/path_helper

I hope that it will help.

That disables path_helper across your system by marking it as no longer executable. It’s likely that something like’Repair Permissions’ would restore that, thought I’m not certain.

path_helper may actually be kind of helpful. Doing the move command I listed above solves the path_helper issue in just the instance we care about. This is the best rundown of the issue that I have come across.

@derekprior thanks for explanation. I will update my notes.

@derekprior sorry for the late reply. I just tried that command you suggested. Amazing. It works. And running specs from Vim has changed my life. Well at least in the last ten minutes. Thank you!

I’m not exactly sure what that command really does or what the best way to handle binstubs is, for future projects?

I also saw this plugin which I found through a Thoughtbot blog post - GitHub - ianheggie/rbenv-binstubs: A Bundler binstubs Plugin for rbenv [maintainer wanted]. What do you guys generally do?

@ralphwintle : That command is compensating for an OS X bug. I don’t think it’s critical you know why you need to do it, just that you remember that it needs doing. I believe it’s even the case on Mavericks, but am not certain.

I think if you talked to 5 different thoughtbot developers you’d probably find at least 3 different approaches to running ruby commands. Some people use a gemset for each project (either with RVM or with rbenv + a plugin), others simply bundle exec everything, and others use binstubs. Even among those who use binstubs, you’re likely to find a few different approaches.

The latest we’ve been doing was in our last issue of “the bot cave”. We’re trying to follow the rails 4 convention of generating binstubs to bin/ and then checking them in, but we only generate binstubs we need (rake, rspec, etc). Some people then add ./bin to their path. I’m a little more paranoid and add ./.git/safe/../../bin to my path instead. Then in projects I trust, I run an alias git trust which creates the .git/safe folder and thus adds the binstubs to my path. I just don’t want to be adding every conceivable bin directory to my path.

Thanks @derekprior. Really helpful. I ended up just using the rbenv-binstubs plugin and following their instructions, but I’ve noted your approach. I made sure to only generate important binstubs (rake, rspec etc) and check those in.