Class: Vagrant::Util::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/env.rb

Class Method Summary collapse

Class Method Details

.with_clean_envObject

Execute the given command, removing any Ruby-specific environment variables. This is an "enhanced" version of Bundler.with_clean_env, which only removes Bundler-specific values. We need to remove all values, specifically:

  • _ORIGINAL_GEM_PATH
  • GEM_PATH
  • GEM_HOME
  • GEM_ROOT
  • BUNDLE_BIN_PATH
  • BUNDLE_GEMFILE
  • RUBYLIB
  • RUBYOPT
  • RUBY_ENGINE
  • RUBY_ROOT
  • RUBY_VERSION

This will escape Vagrant's environment entirely, which is required if calling an executable that lives in another Ruby environment. The original environment restored at the end of this call.

Parameters:

  • block (Proc)

    the block to execute with the cleaned environment


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant/util/env.rb', line 38

def self.with_clean_env
  with_original_env do
    if ENV["BUNDLE_ORIG_MANPATH"]
      ENV["MANPATH"] = ENV["BUNDLE_ORIG_MANPATH"]
    end
    ENV.delete_if { |k,_| k[0,7] == "BUNDLE_" }
    if ENV.has_key? "RUBYOPT"
      ENV["RUBYOPT"] = ENV["RUBYOPT"].sub("-rbundler/setup", "")
      ENV["RUBYOPT"] = ENV["RUBYOPT"].sub("-I#{File.expand_path('..', __FILE__)}", "")
    end
    yield
  end
end

.with_original_envObject


4
5
6
7
8
9
10
11
12
13
# File 'lib/vagrant/util/env.rb', line 4

def self.with_original_env
  original_env = ENV.to_hash
  if defined?(::Bundler) && defined?(::Bundler::ORIGINAL_ENV)
    ENV.replace(::Bundler::ORIGINAL_ENV)
  end
  ENV.update(Vagrant.original_env)
  yield
ensure
  ENV.replace(original_env.to_hash)
end