Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise consistent errors with or without bundle exec #4063

Merged
merged 1 commit into from Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions bundler/lib/bundler/cli/exec.rb
Expand Up @@ -63,10 +63,10 @@ def kernel_load(file, *args)
Kernel.load(file)
rescue SystemExit, SignalException
raise
rescue Exception => e # rubocop:disable Lint/RescueException
rescue Exception # rubocop:disable Lint/RescueException
Bundler.ui.error "bundler: failed to load command: #{cmd} (#{file})"
backtrace = e.backtrace ? e.backtrace.take_while {|bt| !bt.start_with?(__FILE__) } : []
abort "#{e.class}: #{e.message}\n #{backtrace.join("\n ")}"
Bundler::FriendlyErrors.disable!
raise
end

def process_title(file, args)
Expand Down
15 changes: 15 additions & 0 deletions bundler/lib/bundler/friendly_errors.rb
Expand Up @@ -6,6 +6,18 @@ module Bundler
module FriendlyErrors
module_function

def enable!
@disabled = false
end

def disabled?
@disabled
end

def disable!
@disabled = true
end

def log_error(error)
case error
when YamlSyntaxError
Expand Down Expand Up @@ -114,10 +126,13 @@ def issues_url(exception)
end

def self.with_friendly_errors
FriendlyErrors.enable!
yield
rescue SignalException
raise
rescue Exception => e # rubocop:disable Lint/RescueException
raise if FriendlyErrors.disabled?

FriendlyErrors.log_error(e)
exit FriendlyErrors.exit_status(e)
end
Expand Down
14 changes: 11 additions & 3 deletions bundler/spec/commands/exec_spec.rb
Expand Up @@ -698,15 +698,23 @@ def bin_path(a,b,c)
let(:exit_code) { 1 }
let(:expected_err) do
"bundler: failed to load command: #{path} (#{path})" \
"\nRuntimeError: ERROR\n #{path}:10:in `<top (required)>'"
"\n#{path}:10:in `<top (required)>': ERROR (RuntimeError)"
end

it "runs like a normally executed executable" do
skip "https://github.com/rubygems/rubygems/issues/3351" if Gem.win_platform?

subject
expect(exitstatus).to eq(exit_code)
expect(err).to start_with(expected_err)
expect(out).to eq(expected)
end
it_behaves_like "it runs"
end

context "the executable raises an error without a backtrace" do
let(:executable) { super() << "\nclass Err < Exception\ndef backtrace; end;\nend\nraise Err" }
let(:exit_code) { 1 }
let(:expected_err) { "bundler: failed to load command: #{path} (#{path})\nErr: Err" }
let(:expected_err) { "bundler: failed to load command: #{path} (#{path})\n#{system_gem_path("bin/bundle")}: Err (Err)" }
let(:expected) { super() }

it_behaves_like "it runs"
Expand Down