From 1ab91b12fa5f7e98bba2e59e2f40b57004d9acf1 Mon Sep 17 00:00:00 2001 From: Daisuke Aritomo Date: Sat, 9 Dec 2023 13:13:53 +0900 Subject: [PATCH] [rubygems/rubygems] Stream output from ext builds when --verbose Uses Open3.popen2e in place of Open3.capture2e in Gem::Ext::Builder. This change aims to stream stdout/stderr of ext builds when in verbose mode, instead of printing everything at once when the build completes. Nice for debugging gem builds that consumes longer times. https://github.com/rubygems/rubygems/commit/dcdcb5adda --- lib/rubygems/ext/builder.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/rubygems/ext/builder.rb b/lib/rubygems/ext/builder.rb index ccd0853fff5200..be1ba3031cc9ee 100644 --- a/lib/rubygems/ext/builder.rb +++ b/lib/rubygems/ext/builder.rb @@ -88,13 +88,20 @@ def self.run(command, results, command_name = nil, dir = Dir.pwd, env = {}) # Set $SOURCE_DATE_EPOCH for the subprocess. build_env = { "SOURCE_DATE_EPOCH" => Gem.source_date_epoch_string }.merge(env) output, status = begin - Open3.capture2e(build_env, *command, chdir: dir) + Open3.popen2e(build_env, *command, chdir: dir) do |_stdin, stdouterr, wait_thread| + output = String.new + while line = stdouterr.gets + output << line + if verbose + print line + end + end + [output, wait_thread.value] + end rescue StandardError => error raise Gem::InstallError, "#{command_name || class_name} failed#{error.message}" end - if verbose - puts output - else + unless verbose results << output end ensure