From 34d896710a9445ebeb793185acf218f369c76db6 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Wed, 3 Feb 2016 22:35:05 -0600 Subject: [Exec] Load instead of exec-ing --- lib/bundler/cli/exec.rb | 53 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'lib/bundler') diff --git a/lib/bundler/cli/exec.rb b/lib/bundler/cli/exec.rb index 77e75580..0caba872 100644 --- a/lib/bundler/cli/exec.rb +++ b/lib/bundler/cli/exec.rb @@ -18,16 +18,30 @@ module Bundler end def run - ui = Bundler.ui - raise ArgumentError if cmd.nil? - + validate_cmd! SharedHelpers.set_bundle_environment - bin_path = Bundler.which(@cmd) + if bin_path = Bundler.which(cmd) + kernel_load(bin_path, *args) && return if ruby_shebang?(bin_path) + # First, try to exec directly to something in PATH + kernel_exec([bin_path, cmd], *args) + else + # Just exec using the given command + kernel_exec(cmd, *args) + end + end + + private + + def validate_cmd! + return unless cmd.nil? + Bundler.ui.error "bundler: exec needs a command to run" + exit 128 + end + + def kernel_exec(*args) + ui = Bundler.ui Bundler.ui = nil - # First, try to exec directly to something in PATH - Kernel.exec([bin_path, @cmd], *args) if bin_path - # Just exec using the given command - Kernel.exec(@cmd, *args) + Kernel.exec(*args) rescue Errno::EACCES, Errno::ENOEXEC Bundler.ui = ui Bundler.ui.error "bundler: not executable: #{cmd}" @@ -37,10 +51,27 @@ module Bundler Bundler.ui.error "bundler: command not found: #{cmd}" Bundler.ui.warn "Install missing gem executables with `bundle install`" exit 127 - rescue ArgumentError + end + + def kernel_load(file, *args) + args.pop if args.last.is_a?(Hash) + ARGV.replace(args) + ui = Bundler.ui + Bundler.ui = nil + require "bundler/setup" + Kernel.load(file) + rescue SystemExit + raise + rescue Exception => e # rubocop:disable Lint/RescueException Bundler.ui = ui - Bundler.ui.error "bundler: exec needs a command to run" - exit 128 + Bundler.ui.error "bundler: failed to load command: #{cmd} (#{file})" + Bundler.ui.trace(e, nil, true) + exit 125 + end + + def ruby_shebang?(file) + first_line = File.open(file, "rb", &:readline) + first_line == "#!/usr/bin/env ruby\n" || first_line == "#!#{Gem.ruby}\n" end end end -- cgit v1.2.3 From 7f2dd5b3baeb8891403bb2496619feeabfb010cd Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Fri, 5 Feb 2016 11:23:15 -0600 Subject: [Exec] Ensure failure loading is printed to STDERR --- lib/bundler/cli/exec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/bundler') diff --git a/lib/bundler/cli/exec.rb b/lib/bundler/cli/exec.rb index 0caba872..c7f8d0ec 100644 --- a/lib/bundler/cli/exec.rb +++ b/lib/bundler/cli/exec.rb @@ -65,8 +65,7 @@ module Bundler rescue Exception => e # rubocop:disable Lint/RescueException Bundler.ui = ui Bundler.ui.error "bundler: failed to load command: #{cmd} (#{file})" - Bundler.ui.trace(e, nil, true) - exit 125 + abort "#{e.class}: #{e.message}\n#{e.backtrace.join("\n ")}" end def ruby_shebang?(file) -- cgit v1.2.3 From 3b5fb4ed22e1c0bf2da07ff5db645c245720de3c Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Mon, 22 Feb 2016 21:08:05 -0600 Subject: [Exec] Set $0 when loading --- lib/bundler/cli/exec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/bundler') diff --git a/lib/bundler/cli/exec.rb b/lib/bundler/cli/exec.rb index c7f8d0ec..7359f998 100644 --- a/lib/bundler/cli/exec.rb +++ b/lib/bundler/cli/exec.rb @@ -56,6 +56,7 @@ module Bundler def kernel_load(file, *args) args.pop if args.last.is_a?(Hash) ARGV.replace(args) + $0 = file ui = Bundler.ui Bundler.ui = nil require "bundler/setup" -- cgit v1.2.3 From a956ebe4c86757441f6767b9e6eb5e728f040f36 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Mon, 22 Feb 2016 21:09:42 -0600 Subject: [Exec] Filter bundler code from backtrace when load fails --- lib/bundler/cli/exec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/bundler') diff --git a/lib/bundler/cli/exec.rb b/lib/bundler/cli/exec.rb index 7359f998..095b7b79 100644 --- a/lib/bundler/cli/exec.rb +++ b/lib/bundler/cli/exec.rb @@ -66,7 +66,8 @@ module Bundler rescue Exception => e # rubocop:disable Lint/RescueException Bundler.ui = ui Bundler.ui.error "bundler: failed to load command: #{cmd} (#{file})" - abort "#{e.class}: #{e.message}\n#{e.backtrace.join("\n ")}" + backtrace = e.backtrace.take_while {|bt| !bt.start_with?(__FILE__) } + abort "#{e.class}: #{e.message}\n #{backtrace.join("\n ")}" end def ruby_shebang?(file) -- cgit v1.2.3 From b12a5a7eebab3fa31d763c77882cda703dc287ce Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Mon, 22 Feb 2016 21:10:00 -0600 Subject: [Exec] Improve performance when checking for a ruby shebang --- lib/bundler/cli/exec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/bundler') diff --git a/lib/bundler/cli/exec.rb b/lib/bundler/cli/exec.rb index 095b7b79..4f304975 100644 --- a/lib/bundler/cli/exec.rb +++ b/lib/bundler/cli/exec.rb @@ -71,8 +71,12 @@ module Bundler end def ruby_shebang?(file) - first_line = File.open(file, "rb", &:readline) - first_line == "#!/usr/bin/env ruby\n" || first_line == "#!#{Gem.ruby}\n" + possibilities = [ + "#!/usr/bin/env ruby\n", + "#!#{Gem.ruby}\n", + ] + first_line = File.read(file, possibilities.map(&:size).max, :mode => "rb") + possibilities.any? {|shebang| first_line.start_with?(shebang) } end end end -- cgit v1.2.3 From 45136c90c3be96107c6a350cff22072cf318bba9 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Tue, 23 Feb 2016 15:20:04 -0600 Subject: [Exec] Fix loading on 1.8.7 --- lib/bundler/cli/exec.rb | 2 +- spec/commands/exec_spec.rb | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'lib/bundler') diff --git a/lib/bundler/cli/exec.rb b/lib/bundler/cli/exec.rb index 4f304975..865ac202 100644 --- a/lib/bundler/cli/exec.rb +++ b/lib/bundler/cli/exec.rb @@ -75,7 +75,7 @@ module Bundler "#!/usr/bin/env ruby\n", "#!#{Gem.ruby}\n", ] - first_line = File.read(file, possibilities.map(&:size).max, :mode => "rb") + first_line = File.open(file, "rb") {|f| f.read(possibilities.map(&:size).max) } possibilities.any? {|shebang| first_line.start_with?(shebang) } end end diff --git a/spec/commands/exec_spec.rb b/spec/commands/exec_spec.rb index e6c22ead..be2f8aa2 100644 --- a/spec/commands/exec_spec.rb +++ b/spec/commands/exec_spec.rb @@ -378,7 +378,7 @@ describe "bundle exec" do before do path.open("w") {|f| f << executable } - FileUtils.chmod("+x", path) + path.chmod(0755) install_gemfile <<-G gem "rack" @@ -422,7 +422,10 @@ describe "bundle exec" do let(:executable) { super() << "\nraise 'ERROR'" } let(:exit_code) { 1 } let(:expected) { super() << "\nbundler: failed to load command: #{path} (#{path})" } - let(:expected_err) { "RuntimeError: ERROR\n #{path}:7:in `'" } + let(:expected_err) do + "RuntimeError: ERROR\n #{path}:7" + + (Bundler.current_ruby.ruby_18? ? "" : ":in `'") + end it_behaves_like "it runs" end -- cgit v1.2.3