Skip to content

Commit b2177cd

Browse files
hsbtclaude
andcommitted
Do not restore Bundler root when it was never saved
`defined?(previous_root)` is true even when the assignment never ran, so a failure before the root was saved reset Bundler's root to nil. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent da017b9 commit b2177cd

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/rubygems/request_set.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ def load_lockfile(lock_file) # :nodoc:
346346
# `gem install -g` lockfile can be parsed without a Bundler environment.
347347
previous_root = Bundler.instance_variable_get(:@root)
348348
Bundler.instance_variable_set(:@root, Pathname.new(File.expand_path(File.dirname(lock_file))))
349+
root_swapped = true
349350

350351
parser = Bundler::LockfileParser.new(File.read(lock_file), lockfile_path: lock_file)
351352

@@ -408,7 +409,7 @@ def load_lockfile(lock_file) # :nodoc:
408409
gem dep.name, *requirements
409410
end
410411
ensure
411-
Bundler.instance_variable_set(:@root, previous_root) if defined?(previous_root)
412+
Bundler.instance_variable_set(:@root, previous_root) if root_swapped
412413
end
413414

414415
def pretty_print(q) # :nodoc:

test/rubygems/test_gem_request_set.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,46 @@ def test_load_gemdeps_with_lockfile_path_section_newer_than_lockfile
549549
assert_equal %w[a-2], vendor_set.find_all(dep("a", "= 2")).map(&:full_name)
550550
end
551551

552+
def test_load_lockfile_keeps_bundler_root_when_it_cannot_be_swapped
553+
require "bundler"
554+
555+
previous_root = Bundler.instance_variable_get(:@root)
556+
Bundler.instance_variable_set(:@root, Pathname.new(@tempdir))
557+
558+
rs = Gem::RequestSet.new
559+
def rs.require(*)
560+
raise LoadError
561+
end
562+
563+
assert_raise LoadError do
564+
rs.load_lockfile "gem.deps.rb.lock"
565+
end
566+
567+
assert_equal Pathname.new(@tempdir), Bundler.instance_variable_get(:@root)
568+
ensure
569+
Bundler.instance_variable_set(:@root, previous_root)
570+
end
571+
572+
def test_load_lockfile_restores_bundler_root_when_parsing_fails
573+
require "bundler"
574+
575+
previous_root = Bundler.instance_variable_get(:@root)
576+
Bundler.instance_variable_set(:@root, Pathname.new(File.join(@tempdir, "elsewhere")))
577+
578+
File.open "gem.deps.rb.lock", "w" do |io|
579+
io.puts "<<<<<<< HEAD"
580+
end
581+
582+
assert_raise Bundler::LockfileError do
583+
Gem::RequestSet.new.load_lockfile "gem.deps.rb.lock"
584+
end
585+
586+
assert_equal Pathname.new(File.join(@tempdir, "elsewhere")),
587+
Bundler.instance_variable_get(:@root)
588+
ensure
589+
Bundler.instance_variable_set(:@root, previous_root)
590+
end
591+
552592
def test_load_gemdeps_with_missing_lockfile
553593
rs = Gem::RequestSet.new
554594

0 commit comments

Comments
 (0)