From d7a9b3104c5d07af21ed7163098ddf089119a135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 1 Oct 2020 22:29:33 +0200 Subject: [PATCH] Only reset settings when a gemfile is already set Only thing we need to reset is settings, and only because settings are loaded relative to the `Gemfile`, so they need to be reset if the configured or passed `Gemfile` is not standard and will resolve to a different root. Resetting more stuff causes issues under some circumstances, specifically when dependencies further tweak `Kernel.require`. --- bundler/lib/bundler.rb | 4 +++ bundler/lib/bundler/cli.rb | 2 +- bundler/spec/commands/exec_spec.rb | 43 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/bundler/lib/bundler.rb b/bundler/lib/bundler.rb index f6ad7ccaef84..0ddd2c7f8804 100644 --- a/bundler/lib/bundler.rb +++ b/bundler/lib/bundler.rb @@ -602,6 +602,10 @@ def reset! reset_rubygems! end + def reset_settings! + @settings = nil + end + def reset_paths! @bin_path = nil @bundler_major_version = nil diff --git a/bundler/lib/bundler/cli.rb b/bundler/lib/bundler/cli.rb index c0e3802bc9aa..e2dceb98d5fa 100644 --- a/bundler/lib/bundler/cli.rb +++ b/bundler/lib/bundler/cli.rb @@ -57,7 +57,7 @@ def initialize(*args) custom_gemfile = options[:gemfile] || Bundler.settings[:gemfile] if custom_gemfile && !custom_gemfile.empty? Bundler::SharedHelpers.set_env "BUNDLE_GEMFILE", File.expand_path(custom_gemfile) - Bundler.reset_paths! + Bundler.reset_settings! end Bundler.settings.set_command_option_if_given :retry, options[:retry] diff --git a/bundler/spec/commands/exec_spec.rb b/bundler/spec/commands/exec_spec.rb index 84abacb309c8..5e941b76b711 100644 --- a/bundler/spec/commands/exec_spec.rb +++ b/bundler/spec/commands/exec_spec.rb @@ -887,6 +887,49 @@ def bin_path(a,b,c) end end + context "when Kernel.require uses extra monkeypatches" do + before do + skip "https://github.com/rubygems/rubygems/issues/3351" if Gem.win_platform? + + install_gemfile "" + end + + it "does not undo the monkeypatches" do + karafka = bundled_app("bin/karafka") + create_file(karafka, <<~RUBY) + #!#{Gem.ruby} + + module Kernel + module_function + + alias_method :require_before_extra_monkeypatches, :require + + def require(path) + puts "requiring \#{path} used the monkeypatch" + + require_before_extra_monkeypatches(path) + end + end + + Bundler.setup(:default) + + require "foo" + RUBY + karafka.chmod(0o777) + + foreman = bundled_app("bin/foreman") + create_file(foreman, <<~RUBY) + #!#{Gem.ruby} + + puts `bundle exec ./bin/karafka` + RUBY + foreman.chmod(0o777) + + bundle "exec #{foreman}" + expect(out).to eq("requiring foo used the monkeypatch") + end + end + context "with a system gem that shadows a default gem" do let(:openssl_version) { "99.9.9" } let(:expected) { ruby "gem 'openssl', '< 999999'; require 'openssl'; puts OpenSSL::VERSION", :artifice => nil, :raise_on_error => false }