diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2012-05-16 13:34:28 -0700 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2012-06-07 17:44:49 +0200 |
commit | f4dc6c5d4d636c270df8e5423bce6d9b1f08a715 (patch) | |
tree | 23096cff3014337bcbd57033167309c2fbf2d413 | |
parent | a2671929b8c9175df48c905052b8fc4b7572f899 (diff) | |
download | bundler-f4dc6c5d4d636c270df8e5423bce6d9b1f08a715.tar.gz |
Always try to keep original GEM_PATH
This fixes a problem which occures when you want to run `bundle` command
inside code that already loads `Gemfile`. When you start a command with
`bundle exec` it sets `RUBYOPT` to
`-I$PATH_TO_BUNDLER -r"bundler/setup"`. Because of that, when you run
the actual command it already requires bundler, which is fine, but since
`GEM_PATH` was cleared on the first run `ORIGINAL_ENV` will include
empty `GEM_PATH`. Now when you run `Bundler.with_clean_env`, you will
not have any bundler specific env variables, but you will also not have
original gem path, which will make it impossible to run any gem.
To fix this I try to always keep a reference to original gem path, so
it's not emptied along the way.
-rw-r--r-- | lib/bundler.rb | 2 | ||||
-rw-r--r-- | lib/bundler/gem_path_manipulation.rb | 8 | ||||
-rw-r--r-- | spec/runtime/with_clean_env_spec.rb | 14 |
3 files changed, 24 insertions, 0 deletions
diff --git a/lib/bundler.rb b/lib/bundler.rb index 8c325dac93..833b110c29 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -1,12 +1,14 @@ require 'rbconfig' require 'fileutils' require 'pathname' +require 'bundler/gem_path_manipulation' require 'bundler/psyched_yaml' require 'bundler/rubygems_ext' require 'bundler/rubygems_integration' require 'bundler/version' module Bundler + preserve_gem_path ORIGINAL_ENV = ENV.to_hash autoload :Definition, 'bundler/definition' diff --git a/lib/bundler/gem_path_manipulation.rb b/lib/bundler/gem_path_manipulation.rb new file mode 100644 index 0000000000..628d954c35 --- /dev/null +++ b/lib/bundler/gem_path_manipulation.rb @@ -0,0 +1,8 @@ +module Bundler + def self.preserve_gem_path + original_gem_path = ENV["_ORIGINAL_GEM_PATH"] + gem_path = ENV["GEM_PATH"] + ENV["_ORIGINAL_GEM_PATH"] = gem_path if original_gem_path.nil? || original_gem_path == "" + ENV["GEM_PATH"] = original_gem_path if gem_path.nil? || gem_path == "" + end +end diff --git a/spec/runtime/with_clean_env_spec.rb b/spec/runtime/with_clean_env_spec.rb index d317ad5eaf..c325fe85dc 100644 --- a/spec/runtime/with_clean_env_spec.rb +++ b/spec/runtime/with_clean_env_spec.rb @@ -25,6 +25,20 @@ describe "Bundler.with_env helpers" do it_should_behave_like "Bundler.with_*_env" + it "should keep the original GEM_PATH even in sub processes" do + gemfile "" + bundle "install --path vendor/bundle" + + gem_path = ENV['GEM_PATH'] + + code = "Bundler.with_clean_env do;" + + " print ENV['GEM_PATH'] != '';" + + "end" + + result = bundle "exec ruby -e #{code.inspect}" + result.should == "true" + end + it "should not pass any bundler environment variables" do Bundler.with_clean_env do `echo $BUNDLE_PATH`.strip.should_not == './Gemfile' |