diff --git a/bundler/lib/bundler.rb b/bundler/lib/bundler.rb index 1a94e0c963e6..bb1d17dfb94f 100644 --- a/bundler/lib/bundler.rb +++ b/bundler/lib/bundler.rb @@ -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__) @@ -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 @@ -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( diff --git a/bundler/lib/bundler/environment_preserver.rb b/bundler/lib/bundler/environment_preserver.rb index 0f08e049d8d4..cc8bdefbfb0d 100644 --- a/bundler/lib/bundler/environment_preserver.rb +++ b/bundler/lib/bundler/environment_preserver.rb @@ -1,26 +1,7 @@ # 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? @@ -28,58 +9,11 @@ def self.env_to_hash(env) to_hash.each_with_object({}) {|(k,v), a| a[k.upcase] = v } end - # @param env [Hash] - # @param keys [Array] - 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 diff --git a/bundler/lib/bundler/shared_helpers.rb b/bundler/lib/bundler/shared_helpers.rb index 899eb68e0a86..a4f1992f2f1e 100644 --- a/bundler/lib/bundler/shared_helpers.rb +++ b/bundler/lib/bundler/shared_helpers.rb @@ -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 diff --git a/bundler/spec/bundler/environment_preserver_spec.rb b/bundler/spec/bundler/environment_preserver_spec.rb deleted file mode 100644 index 530ca6f8356d..000000000000 --- a/bundler/spec/bundler/environment_preserver_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe Bundler::EnvironmentPreserver do - let(:preserver) { described_class.new(env, ["foo"]) } - - describe "#backup" do - let(:env) { { "foo" => "my-foo", "bar" => "my-bar" } } - subject { preserver.backup } - - it "should create backup entries" do - expect(subject["BUNDLER_ORIG_foo"]).to eq("my-foo") - end - - it "should keep the original entry" do - expect(subject["foo"]).to eq("my-foo") - end - - it "should not create backup entries for unspecified keys" do - expect(subject.key?("BUNDLER_ORIG_bar")).to eq(false) - end - - it "should not affect the original env" do - subject - expect(env.keys.sort).to eq(%w[bar foo]) - end - - context "when a key is empty" do - let(:env) { { "foo" => "" } } - - it "should not create backup entries" do - expect(subject).not_to have_key "BUNDLER_ORIG_foo" - end - end - - context "when an original key is set" do - let(:env) { { "foo" => "my-foo", "BUNDLER_ORIG_foo" => "orig-foo" } } - - it "should keep the original value in the BUNDLER_ORIG_ variable" do - expect(subject["BUNDLER_ORIG_foo"]).to eq("orig-foo") - end - - it "should keep the variable" do - expect(subject["foo"]).to eq("my-foo") - end - end - end - - describe "#restore" do - subject { preserver.restore } - - context "when an original key is set" do - let(:env) { { "foo" => "my-foo", "BUNDLER_ORIG_foo" => "orig-foo" } } - - it "should restore the original value" do - expect(subject["foo"]).to eq("orig-foo") - end - - it "should delete the backup value" do - expect(subject.key?("BUNDLER_ORIG_foo")).to eq(false) - end - end - - context "when no original key is set" do - let(:env) { { "foo" => "my-foo" } } - - it "should keep the current value" do - expect(subject["foo"]).to eq("my-foo") - end - end - - context "when the original key is empty" do - let(:env) { { "foo" => "my-foo", "BUNDLER_ORIG_foo" => "" } } - - it "should keep the current value" do - expect(subject["foo"]).to eq("my-foo") - end - end - end -end diff --git a/bundler/spec/runtime/with_unbundled_env_spec.rb b/bundler/spec/runtime/with_unbundled_env_spec.rb index 731a9921a278..6e22f6b13ad2 100644 --- a/bundler/spec/runtime/with_unbundled_env_spec.rb +++ b/bundler/spec/runtime/with_unbundled_env_spec.rb @@ -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")