From 0d931a502aca19b74acd3e3375c7ca5d5422ff5d Mon Sep 17 00:00:00 2001 From: Cas Donoghue Date: Thu, 13 Mar 2025 15:20:38 -0700 Subject: [PATCH] Surface failures from nested rake/shell tasks (#17310) Previously when rake would shell out the output would be lost. This made debugging CI logs difficult. This commit updates the stack with improved message surfacing on error. --- rakelib/artifacts.rake | 28 +++++++++++++++++++++++++--- rubyUtils.gradle | 14 ++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/rakelib/artifacts.rake b/rakelib/artifacts.rake index a677fa1f8..12954db5d 100644 --- a/rakelib/artifacts.rake +++ b/rakelib/artifacts.rake @@ -125,12 +125,34 @@ namespace "artifact" do result end - # execute Kernel#system call,checking the exist status of the executed command and eventually reporting as exception + ## + # @override safe_system([env,] command... [,options]) + # execute Kernel#system call,checking the exit status of the executed command and eventually reporting as exception def safe_system(*args) - if !system(*args) - status = $? + command = args.dup # avoid mutating input for reporting + env = command.size > 1 && command.first.kind_of?(Hash) ? command.shift : {} + options = command.size > 1 && command.last.kind_of?(Hash) ? command.pop : {} + fail("unsupported options #{options}") unless options.empty? + + # Normalize command to a single string from either a multi-word string + # or an array of individual words + command = command.size > 1 ? Shellwords.join(command.map(&:to_s)) : command.first.to_s + + # prepend the environment + env.each do |k,v| + command.prepend("#{Shellwords.escape(k.to_s)}=#{Shellwords.escape(v.to_s)} ") + end + + output = `#{command} 2>&1` + status = $? + + if !status.success? + puts "Command failed: #{args.inspect}" + puts "Output: #{output}" raise "Got exit status #{status.exitstatus} attempting to execute #{args.inspect}!" end + + true end desc "Generate rpm, deb, tar and zip artifacts" diff --git a/rubyUtils.gradle b/rubyUtils.gradle index c515ee2df..38633390f 100644 --- a/rubyUtils.gradle +++ b/rubyUtils.gradle @@ -159,10 +159,16 @@ void rake(File projectDir, File buildDir, String task, String... args) { jruby.runScriptlet("require 'rake'; require 'time'") def rakeArgs = args ? "'${args.join("','")}'" : "" jruby.runScriptlet(""" - rake = Rake.application - rake.init - rake.load_rakefile - rake['${task}'].invoke(${rakeArgs}) + begin + rake = Rake.application + rake.init + rake.load_rakefile + rake['${task}'].invoke(${rakeArgs}) + rescue => e + puts "Rake task error: #{e.class}: #{e.message}" + puts "Backtrace: #{e.backtrace.join("\\n")}" + raise e + end """ ) }