0
votes

from my research, bundle exec is responsible for executing a command in the context of a bundle.

anyway, I don't quite understand yet for what it does differently for just the command rake db:migrate comparing with bundle exec rake db:migrate.

for example in my case, I executed the first command, and what I got was the following errors:

$ rake db:migrate
(in c:/rails/rails_projects/soccerweb)
rake aborted!
uninitialized constant Rake::DSL
c:/Ruby192/lib/ruby/1.9.1/rake.rb:2482:in `const_missing'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/tasklib.rb:8:in `<clas
s:TaskLib>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/tasklib.rb:6:in `<modu
le:Rake>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/tasklib.rb:3:in `<top
(required)>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/testtask.rb:4:in `requ
ire'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.9.2.2/lib/rake/testtask.rb:4:in `<top
 (required)>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/test_unit/testing.r
ake:2:in `require'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/test_unit/testing.r
ake:2:in `<top (required)>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/test_unit/railtie.r
b:12:in `load'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/test_unit/railtie.r
b:12:in `block in <class:TestUnitRailtie>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `
call'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `
block in load_tasks'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `
each'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/railtie.rb:183:in `
load_tasks'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/engine.rb:396:in `b
lock in load_tasks'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/application/railtie
s.rb:8:in `each'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/application/railtie
s.rb:8:in `all'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/engine.rb:396:in `l
oad_tasks'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/application.rb:103:
in `load_tasks'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/railtie/configurabl
e.rb:30:in `method_missing'
c:/rails/rails_projects/soccerweb/Rakefile:7:in `<top (required)>'
c:/Ruby192/lib/ruby/1.9.1/rake.rb:2373:in `load'
c:/Ruby192/lib/ruby/1.9.1/rake.rb:2373:in `raw_load_rakefile'
c:/Ruby192/lib/ruby/1.9.1/rake.rb:2007:in `block in load_rakefile'
c:/Ruby192/lib/ruby/1.9.1/rake.rb:2058:in `standard_exception_handling'
c:/Ruby192/lib/ruby/1.9.1/rake.rb:2006:in `load_rakefile'
c:/Ruby192/lib/ruby/1.9.1/rake.rb:1991:in `run'
c:/Ruby192/bin/rake:31:in `<main>'

but when I used "bundle exec" preceding that "rake db:migrate" command, everything just worked for me gracefully!

anyone can shed some light on me?

2

2 Answers

6
votes

bundle exec rake runs the version of rake which you've specified in your Gemfile. The default version of rake, the one which can be found in your $PATH, might be different than one executed by bundle exec rake.

The problem you've experienced has been covered in a separate question. Your bundle exec rake most probably runs rake 0.8.7 and rake from your $PATH is a newer version which is affected by this problem.

Quoting from man bundle exec:

bundle exec makes a number of changes to the shell environment, then executes the command you specify in full.

  • (...)
  • put the directory containing executables (like rails, rspec, rackup) for your bundle on $PATH
0
votes

Bundler solves gem dependencies for you. You can find read their rationale page for an explanation about their approach.