Skip to content

Commit 5c40e06

Browse files
hsbtclaude
andcommitted
Don't mask install errors when no Gemfile can be located
require_tree_for_spec calls SharedHelpers.default_gemfile to build the "In Gemfile:" header of the error report. When BUNDLE_GEMFILE is unset and no Gemfile is findable from the current directory, that raises GemfileNotFound from inside the error-reporting path, hiding the original install errors behind "Could not locate Gemfile". Fall back to a generic header so the real errors always surface. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 6a42307 commit 5c40e06

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/bundler/installer/parallel_installer.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,15 @@ def handle_error
255255

256256
def require_tree_for_spec(spec)
257257
tree = @spec_set.what_required(spec)
258-
t = String.new("In #{File.basename(SharedHelpers.default_gemfile)}:\n")
258+
gemfile_name = begin
259+
File.basename(SharedHelpers.default_gemfile)
260+
rescue GemfileNotFound
261+
# This runs while reporting an install error. When no Gemfile can be
262+
# located (e.g. Bundler used as a library), raising here would mask
263+
# the original error, so fall back to a generic header instead.
264+
"Gemfile"
265+
end
266+
t = String.new("In #{gemfile_name}:\n")
259267
tree.each_with_index do |s, depth|
260268
t << " " * depth.succ << s.name
261269
unless tree.last == s

spec/bundler/installer/parallel_installer_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,26 @@ def redefine_build_jobs
227227
end
228228
end
229229

230+
describe "require tree in error reports" do
231+
# require_tree_for_spec runs while reporting an install error. When no
232+
# Gemfile can be located, default_gemfile raises GemfileNotFound, which
233+
# would mask the original install error entirely.
234+
it "falls back to a generic header when no Gemfile can be located" do
235+
parallel_installer = Bundler::ParallelInstaller.new(nil, [], 1, false, false)
236+
237+
spec = double("spec", name: "mygem", version: Gem::Version.new("1.0"))
238+
spec_set = double("spec_set", what_required: [spec])
239+
parallel_installer.instance_variable_set(:@spec_set, spec_set)
240+
241+
allow(Bundler::SharedHelpers).to receive(:default_gemfile).
242+
and_raise(Bundler::GemfileNotFound, "Could not locate Gemfile")
243+
244+
tree = parallel_installer.send(:require_tree_for_spec, spec)
245+
expect(tree).to start_with("In Gemfile:\n")
246+
expect(tree).to include("mygem")
247+
end
248+
end
249+
230250
describe "make jobserver with nmake" do
231251
# nmake reads MAKEFLAGS from the environment and treats its contents as
232252
# bare option letters, so a GNU make `--jobserver-auth` aborts the build

0 commit comments

Comments
 (0)