Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bundle package fails when run inside rake task

My Environment

Vanilla Ubuntu 12.10, no rvm or renv.

> gem --version
1.8.23

> ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]

> bundle --version
Bundler version 1.2.1

My problem

I have a rake task to package my gems and upload them to my development and production servers. The problem is that the rake task fails when the Gemfile includes git or path gems. Bundler already supports packaging of these type of gems and it works great on my terminal but it fails when running withing a rake task and I cannot find out why.

My rake task

> cat lib/tasks/upload.rake

namespace :deploy do
  desc "Package all gems and upload to remote server"
  task :upload => [:environment] do |t, args|
    if ! system("bundle package --all")
      raise "TOTAL FAIL"
    end

    # Magic method to upload vendor/cache to remote server
  end
end

My tries

Running bundle package in the terminal works:

> bundle package --all
....
Using bson (1.7.0) 
Using bson_ext (1.7.0) 
Using cancan (1.6.8) from git://github.com/ryanb/cancan.git (at /home/ryujin/Projects/rails/Encluster4/vendor/cache/cancan-4dcd54459482) 
Using carrierwave (0.7.0) 
Using coffee-script-source (1.4.0) 
....
Updating files in vendor/cache
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Updating files in vendor/cache

Running bundle package withing irb also works:

> irb
irb> system("bundle package --all")
...
Using ansi (1.4.3) 
Using bson (1.7.0) 
Using bson_ext (1.7.0) 
Using cancan (1.6.8) from git://github.com/ryanb/cancan.git (at /home/ryujin/Projects/rails/Encluster4/vendor/cache/cancan-4dcd54459482) 
Using carrierwave (0.7.0) 
Using coffee-script-source (1.4.0) 
...
Updating files in vendor/cache
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Updating files in vendor/cache
=> true

But bundle package does not work when executed within my simple rake task:

> bundle exec rake deploy:upload
Updating files in vendor/cache
Could not find cancan-1.6.8.gem for installation
rake aborted!
TOTAL FAIL

Tasks: TOP => deploy:upload
(See full trace by running task with --trace)

I find not reason why this could fail. I am running in the same environment all the time. I already checked the bundle exec file is the same (/usr/local/bin/bundle) on all three cases. I have no traces or rvm or renv or anything like that. Also tried running the task without bundle exec and the same problem.

Thanks in advance for any tips on why this happens.

like image 927
Horacio Avatar asked Nov 07 '12 03:11

Horacio


2 Answers

I had the same trouble. I checked the environment variables and find out that the bundler modifies RUBYOPT:

$ bundle exec env > benv.txt
$ env > env.txt
$ diff -u env.txt benv.txt
--- env.txt 2012-12-05 17:13:10.000000000 +0400
+++ benv.txt  2012-12-05 17:13:07.000000000 +0400
@@ -72,4 +72,8 @@
 CDPATH=.
 install_flag=1
 rvm_hook=
-_=/usr/bin/env
+_=/Users/denis/.rvm/gems/ruby-1.9.3-p327@global/bin/bundle
+_ORIGINAL_GEM_PATH=/Users/denis/.rvm/gems/ruby-1.9.3-p327:/Users/denis/.rvm/gems/ruby-1.9.3-p327@global
+BUNDLE_BIN_PATH=/Users/denis/.rvm/gems/ruby-1.9.3-p327@global/gems/bundler-1.2.3/bin/bundle
+BUNDLE_GEMFILE=/Users/denis/src/my-project/Gemfile
+RUBYOPT=-I/Users/denis/.rvm/gems/ruby-1.9.3-p327@global/gems/bundler-1.2.3/lib -rbundler/setup

So, to workaround I do this:

system %Q(/usr/bin/env RUBYOPT= bundle package)

Hope this helps!

like image 158
Dení Avatar answered Oct 03 '22 23:10

Dení


One can also prefer to use

Bundler.with_clean_env do
   sh "bundle update --source .."
end

and indeed it is an RUBYOPT problem as mentioned by Denis

like image 43
Pandya M. Nandan Avatar answered Oct 03 '22 22:10

Pandya M. Nandan