Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions bundler/lib/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
# of loaded and required modules.
#
module Bundler
environment_preserver = EnvironmentPreserver.from_env
ORIGINAL_ENV = environment_preserver.restore
environment_preserver.replace_with_backup
SUDO_MUTEX = Thread::Mutex.new

autoload :Definition, File.expand_path("bundler/definition", __dir__)
Expand Down Expand Up @@ -78,6 +75,8 @@ module Bundler
autoload :VersionRanges, File.expand_path("bundler/version_ranges", __dir__)

class << self
include EnvironmentPreserver

def configure
@configured ||= configure_gem_home_and_path
end
Expand Down Expand Up @@ -344,11 +343,6 @@ def settings
@settings = Settings.new(Pathname.new(".bundle").expand_path)
end

# @return [Hash] Environment present before Bundler was activated
def original_env
ORIGINAL_ENV.clone
end

# @deprecated Use `unbundled_env` instead
def clean_env
Bundler::SharedHelpers.major_deprecation(
Expand Down
76 changes: 5 additions & 71 deletions bundler/lib/bundler/environment_preserver.rb
Original file line number Diff line number Diff line change
@@ -1,85 +1,19 @@
# frozen_string_literal: true

module Bundler
class EnvironmentPreserver
INTENTIONALLY_NIL = "BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL".freeze
BUNDLER_KEYS = %w[
BUNDLE_BIN_PATH
BUNDLE_GEMFILE
BUNDLER_VERSION
GEM_HOME
GEM_PATH
MANPATH
PATH
RB_USER_INSTALL
RUBYLIB
RUBYOPT
].map(&:freeze).freeze
BUNDLER_PREFIX = "BUNDLER_ORIG_".freeze

def self.from_env
new(env_to_hash(ENV), BUNDLER_KEYS)
end

module EnvironmentPreserver
def self.env_to_hash(env)
to_hash = env.to_hash
return to_hash unless Gem.win_platform?

to_hash.each_with_object({}) {|(k,v), a| a[k.upcase] = v }
end

# @param env [Hash]
# @param keys [Array<String>]
def initialize(env, keys)
@original = env
@keys = keys
@prefix = BUNDLER_PREFIX
end

# Replaces `ENV` with the bundler environment variables backed up
def replace_with_backup
unless Gem.win_platform?
ENV.replace(backup)
return
end

# Fallback logic for Windows below to workaround
# https://bugs.ruby-lang.org/issues/16798. Can be dropped once all
# supported rubies include the fix for that.

ENV.clear

backup.each {|k, v| ENV[k] = v }
end

# @return [Hash]
def backup
env = @original.clone
@keys.each do |key|
value = env[key]
if !value.nil? && !value.empty?
env[@prefix + key] ||= value
elsif value.nil?
env[@prefix + key] ||= INTENTIONALLY_NIL
end
end
env
end
ORIGINAL_ENV = env_to_hash(ENV)

# @return [Hash]
def restore
env = @original.clone
@keys.each do |key|
value_original = env[@prefix + key]
next if value_original.nil? || value_original.empty?
if value_original == INTENTIONALLY_NIL
env.delete(key)
else
env[key] = value_original
end
env.delete(@prefix + key)
end
env
# @return [Hash] Environment present before Bundler was activated
def original_env
ORIGINAL_ENV.clone
end
end
end
6 changes: 0 additions & 6 deletions bundler/lib/bundler/shared_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,6 @@ def search_up(*names)
end

def set_env(key, value)
raise ArgumentError, "new key #{key}" unless EnvironmentPreserver::BUNDLER_KEYS.include?(key)
orig_key = "#{EnvironmentPreserver::BUNDLER_PREFIX}#{key}"
orig = ENV[key]
orig ||= EnvironmentPreserver::INTENTIONALLY_NIL
ENV[orig_key] ||= orig

ENV[key] = value
end
public :set_env
Expand Down
79 changes: 0 additions & 79 deletions bundler/spec/bundler/environment_preserver_spec.rb

This file was deleted.

3 changes: 0 additions & 3 deletions bundler/spec/runtime/with_unbundled_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ def run_bundler_script(env, script)
end

it "removes variables that bundler added", :ruby_repo do
# Simulate bundler has not yet been loaded
ENV.replace(ENV.to_hash.delete_if {|k, _v| k.start_with?(Bundler::EnvironmentPreserver::BUNDLER_PREFIX) })

original = ruby('puts ENV.to_a.map {|e| e.join("=") }.sort.join("\n")')
create_file("source.rb", <<-RUBY)
puts Bundler.original_env.to_a.map {|e| e.join("=") }.sort.join("\n")
Expand Down