Skip to content

Commit 5299488

Browse files
[8.18] Surface failures from nested rake/shell tasks (backport #17310) (#17314)
* 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 * Extend ruby linting tasks to handle file inputs (#16660) This commit extends the gradle and rake tasks to pass through a list of files for rubocop to lint. This allows more specificity and fine grained control for linting when the consumer of the tasks only wishes to lint a select few files. * Ensure shellwords library is loaded Without this depending on task load order `Shellwords` may not be available. --------- Co-authored-by: Cas Donoghue <[email protected]>
1 parent 94cac3f commit 5299488

File tree

4 files changed

+80
-27
lines changed

4 files changed

+80
-27
lines changed

build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,10 +783,16 @@ class JDKDetails {
783783
}
784784

785785
tasks.register("lint") {
786-
// Calls rake's 'lint' task
786+
description = "Lint Ruby source files. Use -PrubySource=file1.rb,file2.rb to specify files"
787787
dependsOn installDevelopmentGems
788788
doLast {
789-
rake(projectDir, buildDir, 'lint:report')
789+
if (project.hasProperty("rubySource")) {
790+
// Split the comma-separated files and pass them as separate arguments
791+
def files = project.property("rubySource").split(",")
792+
rake(projectDir, buildDir, "lint:report", *files)
793+
} else {
794+
rake(projectDir, buildDir, "lint:report")
795+
}
790796
}
791797
}
792798

rakelib/artifacts.rake

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
require 'shellwords'
19+
1820
namespace "artifact" do
1921
SNAPSHOT_BUILD = ENV["RELEASE"] != "1"
2022
VERSION_QUALIFIER = ENV["VERSION_QUALIFIER"].to_s.strip.empty? ? nil : ENV["VERSION_QUALIFIER"]
@@ -127,12 +129,34 @@ namespace "artifact" do
127129
result
128130
end
129131

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

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

rakelib/lint.rake

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
1817
namespace "lint" do
1918
module RuboCLI
2019
def self.run!(*args)
@@ -25,28 +24,44 @@ namespace "lint" do
2524
end
2625
end
2726

28-
# task that runs lint report
29-
desc "Report all Lint Cops"
30-
task "report" do
31-
RuboCLI.run!("--lint")
27+
desc "Report all Lint Cops. Optional: Specify one or more files"
28+
task :report, [:file] do |t, args|
29+
files = [args[:file], *args.extras].compact
30+
31+
if files.empty?
32+
RuboCLI.run!("--lint")
33+
else
34+
puts "Running lint report on specific files: #{files.join(', ')}"
35+
RuboCLI.run!("--lint", *files)
36+
end
3237
end
3338

34-
# Tasks automatically fixes a Cop passed as a parameter (e.g. Lint/DeprecatedClassMethods)
35-
# TODO: Add a way to autocorrect all cops, and not just the one passed as parameter
36-
desc "Automatically fix all instances of a Cop passed as a parameter"
37-
task "correct", [:cop] do |t, args|
39+
# Tasks automatically fixes a Cop passed as a parameter
40+
desc "Automatically fix all instances of a Cop passed as a parameter. Optional: Specify one or more files"
41+
task :correct, [:cop] do |t, args|
3842
if args[:cop].to_s.empty?
3943
puts "No Cop has been provided, aborting..."
4044
exit(0)
4145
else
42-
puts "Attempting to correct Lint issues for: #{args[:cop].to_s}"
43-
RuboCLI.run!("--autocorrect-all", "--only", args[:cop].to_s)
46+
files = args.extras
47+
if files.empty?
48+
puts "Attempting to correct Lint issues for: #{args[:cop]}"
49+
RuboCLI.run!("--autocorrect-all", "--only", args[:cop])
50+
else
51+
puts "Attempting to correct Lint issues for #{args[:cop]} in files: #{files.join(', ')}"
52+
RuboCLI.run!("--autocorrect-all", "--only", args[:cop], *files)
53+
end
4454
end
4555
end
4656

47-
# task that automatically fixes code formatting
48-
desc "Automatically fix Layout Cops"
49-
task "format" do
50-
RuboCLI.run!("--fix-layout")
57+
desc "Automatically fix Layout Cops. Optional: Specify one or more files"
58+
task :format, [:file] do |t, args|
59+
files = [args[:file], *args.extras].compact
60+
if files.empty?
61+
RuboCLI.run!("--fix-layout")
62+
else
63+
puts "Running format fixes on specific files: #{files.join(', ')}"
64+
RuboCLI.run!("--fix-layout", *files)
65+
end
5166
end
52-
end
67+
end

rubyUtils.gradle

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,25 @@ void buildGem(File projectDir, File buildDir, String gemspec) {
151151
* @param projectDir Gradle projectDir
152152
* @param buildDir Gradle buildDir
153153
* @param plugin Plugin to run specs for
154-
* @param args CLI arguments to pass to rspec
154+
* @param args Optional arguments to pass to the rake task
155155
*/
156-
void rake(File projectDir, File buildDir, String task) {
156+
void rake(File projectDir, File buildDir, String task, String... args) {
157157
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
158158
jruby.currentDirectory = projectDir
159159
jruby.runScriptlet("require 'rake'; require 'time'")
160+
def rakeArgs = args ? "'${args.join("','")}'" : ""
160161
jruby.runScriptlet("""
161-
rake = Rake.application
162-
rake.init
163-
rake.load_rakefile
164-
rake['${task}'].invoke
162+
begin
163+
rake = Rake.application
164+
rake.init
165+
rake.load_rakefile
166+
rake['${task}'].invoke(${rakeArgs})
167+
rescue => e
168+
puts "Rake task error: #{e.class}: #{e.message}"
169+
puts "Backtrace: #{e.backtrace.join("\\n")}"
170+
raise e
171+
end
172+
165173
"""
166174
)
167175
}

0 commit comments

Comments
 (0)