Skip to content

Commit 9534df1

Browse files
Stop cleaning up $LOAD_PATH on bundler/setup
If the `$LOAD_PATH` has been modified prior to bundler being loaded, don't mess with that, just put the paths of the gems resolved by the Gemfile{,.lock} file in front of it. This way, if some default gem has been required before bundler, and rubygems has enhanced the `$LOAD_PATH` to use the latest version in the system, further requires of that default gem after bundler has been activated will use the same version. That, unless the gem is a Gemfile dependency. In that case, we'll get a mismatch error anyways as we do now.
1 parent 7818aaf commit 9534df1

File tree

4 files changed

+36
-46
lines changed

4 files changed

+36
-46
lines changed

bundler/lib/bundler.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def setup(*groups)
149149
# Load all groups, but only once
150150
@setup = load.setup
151151
else
152+
SharedHelpers.clean_load_path
152153
load.setup(*groups)
153154
end
154155
end

bundler/lib/bundler/runtime.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ def initialize(root, definition)
1212
def setup(*groups)
1313
@definition.ensure_equivalent_gemfile_and_lockfile if Bundler.frozen_bundle?
1414

15-
# Has to happen first
16-
clean_load_path
17-
1815
specs = @definition.specs_for(groups)
1916

2017
SharedHelpers.set_bundle_environment

bundler/lib/bundler/shared_helpers.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ def write_to_gemfile(gemfile_path, contents)
198198
filesystem_access(gemfile_path) {|g| File.open(g, "w") {|file| file.puts contents } }
199199
end
200200

201+
def clean_load_path
202+
bundler_lib = bundler_ruby_lib
203+
204+
loaded_gem_paths = Bundler.rubygems.loaded_gem_paths
205+
206+
$LOAD_PATH.reject! do |p|
207+
next if resolve_path(p).start_with?(bundler_lib)
208+
loaded_gem_paths.delete(p)
209+
end
210+
$LOAD_PATH.uniq!
211+
end
212+
201213
private
202214

203215
def validate_bundle_path
@@ -312,18 +324,6 @@ def bundler_ruby_lib
312324
resolve_path File.expand_path("../..", __FILE__)
313325
end
314326

315-
def clean_load_path
316-
bundler_lib = bundler_ruby_lib
317-
318-
loaded_gem_paths = Bundler.rubygems.loaded_gem_paths
319-
320-
$LOAD_PATH.reject! do |p|
321-
next if resolve_path(p).start_with?(bundler_lib)
322-
loaded_gem_paths.delete(p)
323-
end
324-
$LOAD_PATH.uniq!
325-
end
326-
327327
def resolve_path(path)
328328
expanded = File.expand_path(path)
329329
return expanded unless File.respond_to?(:realpath) && File.exist?(expanded)

bundler/spec/runtime/setup_spec.rb

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -828,37 +828,6 @@ def clean_load_path(lp)
828828
expect(out).to eq("yay")
829829
end
830830

831-
it "should clean $LOAD_PATH properly", :ruby_repo do
832-
gem_name = "very_simple_binary"
833-
full_gem_name = gem_name + "-1.0"
834-
ext_dir = File.join(tmp("extensions", full_gem_name))
835-
836-
system_gems full_gem_name
837-
838-
install_gemfile <<-G
839-
source "#{file_uri_for(gem_repo1)}"
840-
G
841-
842-
ruby <<-R
843-
s = Gem::Specification.find_by_name '#{gem_name}'
844-
s.extension_dir = '#{ext_dir}'
845-
846-
# Don't build extensions.
847-
s.class.send(:define_method, :build_extensions) { nil }
848-
849-
require 'bundler'
850-
gem '#{gem_name}'
851-
852-
puts $LOAD_PATH.count {|path| path =~ /#{gem_name}/} >= 2
853-
854-
Bundler.setup
855-
856-
puts $LOAD_PATH.count {|path| path =~ /#{gem_name}/} == 0
857-
R
858-
859-
expect(out).to eq("true\ntrue")
860-
end
861-
862831
context "with bundler is located in symlinked GEM_HOME" do
863832
let(:gem_home) { Dir.mktmpdir }
864833
let(:symlinked_gem_home) { Tempfile.new("gem_home").path }
@@ -1511,5 +1480,28 @@ def lock_with(ruby_version = nil)
15111480

15121481
expect(out).to include("rack, yard")
15131482
end
1483+
1484+
it "does not cause double loads when higher versions of default gems are activated before bundler" do
1485+
build_repo2 do
1486+
build_gem "json", "999.999.999" do |s|
1487+
s.write "lib/json.rb", <<~RUBY
1488+
module JSON
1489+
VERSION = "999.999.999"
1490+
end
1491+
RUBY
1492+
end
1493+
end
1494+
1495+
system_gems "json-999.999.999", :gem_repo => gem_repo2
1496+
1497+
install_gemfile "source \"#{file_uri_for(gem_repo1)}\""
1498+
ruby <<-RUBY
1499+
require "json"
1500+
require "bundler/setup"
1501+
require "json"
1502+
RUBY
1503+
1504+
expect(err).to be_empty
1505+
end
15141506
end
15151507
end

0 commit comments

Comments
 (0)