Skip to content

Commit de9068e

Browse files
donoghucmergify[bot]
authored andcommitted
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. (cherry picked from commit 0d931a5) # Conflicts: # rubyUtils.gradle
1 parent 74b1265 commit de9068e

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

rakelib/artifacts.rake

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,34 @@ namespace "artifact" do
127127
result
128128
end
129129

130-
# execute Kernel#system call,checking the exist status of the executed command and eventually reporting as exception
130+
##
131+
# @override safe_system([env,] command... [,options])
132+
# execute Kernel#system call,checking the exit status of the executed command and eventually reporting as exception
131133
def safe_system(*args)
132-
if !system(*args)
133-
status = $?
134+
command = args.dup # avoid mutating input for reporting
135+
env = command.size > 1 && command.first.kind_of?(Hash) ? command.shift : {}
136+
options = command.size > 1 && command.last.kind_of?(Hash) ? command.pop : {}
137+
fail("unsupported options #{options}") unless options.empty?
138+
139+
# Normalize command to a single string from either a multi-word string
140+
# or an array of individual words
141+
command = command.size > 1 ? Shellwords.join(command.map(&:to_s)) : command.first.to_s
142+
143+
# prepend the environment
144+
env.each do |k,v|
145+
command.prepend("#{Shellwords.escape(k.to_s)}=#{Shellwords.escape(v.to_s)} ")
146+
end
147+
148+
output = `#{command} 2>&1`
149+
status = $?
150+
151+
if !status.success?
152+
puts "Command failed: #{args.inspect}"
153+
puts "Output: #{output}"
134154
raise "Got exit status #{status.exitstatus} attempting to execute #{args.inspect}!"
135155
end
156+
157+
true
136158
end
137159

138160
desc "Generate rpm, deb, tar and zip artifacts"

rubyUtils.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,23 @@ void rake(File projectDir, File buildDir, String task) {
158158
jruby.currentDirectory = projectDir
159159
jruby.runScriptlet("require 'rake'; require 'time'")
160160
jruby.runScriptlet("""
161+
<<<<<<< HEAD
161162
rake = Rake.application
162163
rake.init
163164
rake.load_rakefile
164165
rake['${task}'].invoke
166+
=======
167+
begin
168+
rake = Rake.application
169+
rake.init
170+
rake.load_rakefile
171+
rake['${task}'].invoke(${rakeArgs})
172+
rescue => e
173+
puts "Rake task error: #{e.class}: #{e.message}"
174+
puts "Backtrace: #{e.backtrace.join("\\n")}"
175+
raise e
176+
end
177+
>>>>>>> 0d931a50 (Surface failures from nested rake/shell tasks (#17310))
165178
"""
166179
)
167180
}

0 commit comments

Comments
 (0)