Skip to content

Commit

Permalink
Only reset settings when a gemfile is already set
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
deivid-rodriguez committed Nov 16, 2020
1 parent 6b0a1aa commit ee83fbc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bundler/lib/bundler.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/cli.rb
Expand Up @@ -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]
Expand Down
58 changes: 58 additions & 0 deletions bundler/spec/commands/exec_spec.rb
Expand Up @@ -887,6 +887,64 @@ def bin_path(a,b,c)
end
end

context "when a gem monkey patches require" do
before do
skip "https://github.com/rubygems/rubygems/issues/3351" if Gem.win_platform?

build_repo2 do
build_gem "zeitwerk" do |s|
s.write("lib/zeitwerk.rb", <<~'RUBY')
module Kernel
module_function
alias_method :zeitwerk_original_require, :require
def require(path)
puts "#{path} used further decorated require"
zeitwerk_original_require(path)
end
end
RUBY
end
end

install_gemfile <<-G
source "#{file_uri_for(gem_repo2)}"
gem "zeitwerk"
G
end

it "does not undo the monkeypatch when `bundle exec`'ing again" do
karafka = bundled_app("bin/karafka")
create_file(karafka, <<~RUBY)
#!#{Gem.ruby}
# to setup require decorator
require "zeitwerk"
# to setup bundler
Bundler.setup(:default)
# to make sure decorator is still used
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("foo used further decorated require")
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 }
Expand Down

0 comments on commit ee83fbc

Please sign in to comment.