From fcc477044f821d5f6ebbc59bf396b3cce5e5e9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Fri, 13 Nov 2020 14:40:42 +0100 Subject: [PATCH] Raise consistent errors with or without `bundle exec` --- bundler/lib/bundler/cli/exec.rb | 6 +++--- bundler/lib/bundler/friendly_errors.rb | 15 +++++++++++++++ bundler/spec/commands/exec_spec.rb | 14 +++++++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bundler/lib/bundler/cli/exec.rb b/bundler/lib/bundler/cli/exec.rb index 0412d3adb0b1..318d57fb0694 100644 --- a/bundler/lib/bundler/cli/exec.rb +++ b/bundler/lib/bundler/cli/exec.rb @@ -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) diff --git a/bundler/lib/bundler/friendly_errors.rb b/bundler/lib/bundler/friendly_errors.rb index 827306469988..5d0bb905bca3 100644 --- a/bundler/lib/bundler/friendly_errors.rb +++ b/bundler/lib/bundler/friendly_errors.rb @@ -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 @@ -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 diff --git a/bundler/spec/commands/exec_spec.rb b/bundler/spec/commands/exec_spec.rb index 074eb5ef8353..4051d5cce85f 100644 --- a/bundler/spec/commands/exec_spec.rb +++ b/bundler/spec/commands/exec_spec.rb @@ -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 `'" + "\n#{path}:10:in `': 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"