bundler and spring31 Mar 2016
some background information about bundler and spring as well as some
intricacies of using
bundle exec, bundler and spring binstubs.
- okay, wtf to do with all this stuff about bundler and spring?
bundle exec command executes the command, making all gems specified in the Gemfile(5) available to require in Ruby programs.
bundler setup for Rails application takes place in config/boot.rb:
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) require 'bundler/setup' # Set up gems listed in the Gemfile.
also bundler is used to manage application dependencies (gemsets) - RVM provides functionality to do it as well but bundler is a better way. rbenv doesn’t manage gemsets at all in contrast to RVM.
there might be multiple versions of the same gem installed for particular ruby version but project uses gem versions from bundle (specified in Gemfile.lock).
when running commands (
rake, etc.) from within project directory
it’s necessary to use bundle gem versions - this can be done in 2 ways:
- run commands with
- use bundler binstubs
bundle exec is okay except that it’s necessary to type it whenever
you run gem commands:
alias common commands to be run with
alias rails='bundle exec rails' alias rake='bundle exec rake' alias rspec='bundle exec rspec' # alias definitions are recursive: # other aliases will expand `rspec` to `bundle exec rspec` # (previously I thought that they are not) alias cap='bundle exec cap' alias guard='bundle exec guard' alias sidekiq='bundle exec sidekiq --config ./config/sidekiq.yml'
bundle execin Guardfile
guard :rspec, cmd: 'bundle exec rspec -f doc', failed_mode: :none do ... end
alternatively generate bundler binstubs:
$ bundle install --binstubs
to make rbenv is aware of bundler binstubs use rbenv-binstubs rbenv plugin.
$ rbenv which rspec /Users/tap/.rbenv/versions/2.3.0/bin/rspec
bundle exec or bundler binstubs commands are not run with spring!
(well, except for
bundle exec rails console but this is just a weird exception)
NOTE: don’t use spring in production!
to use spring preloader:
run commands with
bundle exec springor just
in previous releases of spring there was a warning about significant performance penalty when running spring with bundler (
bundle exec spring) - probably this is not the case now.
[RECOMMENDED] use spring binstubs
spring binstub --allgenerates spring binstubs for 3 commands:
also it generates bin/spring which is loaded in each of these binstubs. in fact
bin/railsis equivalent to
NOTE: it’s not possible to run other commands with spring.
spring binstubs are run in context of a bundle - there is no need to use
bundle execor bundler binstubs.
don’t use bundler and spring binstubs at the same time since they are both stored in bin/ directory by default and might overwrite each other.
okay, wtf to do with all this stuff about bundler and spring?
- generate spring binstubs for
rspecspring binstub in Guardfile
guard :rspec, cmd: 'bin/rspec -f doc', failed_mode: :none do ... end
add these (or similar) aliases in ~/.zshrc:
alias rails='bin/rails' alias rake='bin/rake' alias rspec='bin/rspec' # other aliases with rails, rake and rspec inside # must use their spring binstub counterparts as well - # alias definitions are recursive alias cap='bundle exec cap' alias guard='bundle exec guard' alias sidekiq='bundle exec sidekiq --config ./config/sidekiq.yml'
sidekiq and Guard are run with
bundle execbecause there are no spring binstubs for them (and it doesn’t make sense to make one for Guard since it’s using
rspecspring binstub inside) - still we need to run their bundle versions.