diff --git a/Library/Homebrew/dev-cmd/vendor-gems.rb b/Library/Homebrew/dev-cmd/vendor-gems.rb index 56e1de3a4..bae2c0a6e 100644 --- a/Library/Homebrew/dev-cmd/vendor-gems.rb +++ b/Library/Homebrew/dev-cmd/vendor-gems.rb @@ -40,7 +40,7 @@ module Homebrew end ohai "bundle install --standalone" - safe_system "bundle", "install", "--standalone" + safe_system_redirect_stdout_to_stderr "bundle", "install", "--standalone" ohai "bundle pristine" safe_system "bundle", "pristine" diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index b041cfee9..52e931306 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -332,6 +332,19 @@ module Kernel end end + # Redirects stdout to stderr, throws exception on command failure. + def safe_system_redirect_stdout_to_stderr(cmd, *args) + return if Homebrew._system(cmd, *args) do + # Redirect stdout stream to stderr stream. This is useful to prevent + # subprocesses from writing to stdout and interfering with the intended + # output, e.g. when running a brew command with `--json` for programs + # automating brew commands. + $stdout.reopen($stderr) + end + + raise ErrorDuringExecution.new([cmd, *args], status: $CHILD_STATUS) + end + def which(cmd, path = ENV["PATH"]) PATH.new(path).each do |p| begin diff --git a/Library/Homebrew/utils/gems.rb b/Library/Homebrew/utils/gems.rb index bc2337834..a616477d8 100644 --- a/Library/Homebrew/utils/gems.rb +++ b/Library/Homebrew/utils/gems.rb @@ -144,7 +144,9 @@ module Homebrew # for some reason sometimes the exit code lies so check the output too. if bundle_check_failed || bundle_check_output.include?("Install missing gems") - unless system bundle, "install" + begin + safe_system_redirect_stdout_to_stderr bundle, "install" + rescue ErrorDuringExecution message = <<~EOS failed to run `#{bundle} install`! EOS