[8.16] Surface failures from nested rake/shell tasks (backport #17310) (#17316)

* 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 0d931a502a)

# 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.

* Fix botched merge conflict resolution

---------

Co-authored-by: Cas Donoghue <cas.donoghue@gmail.com>
This commit is contained in:
mergify[bot] 2025-03-20 08:34:31 -07:00 committed by GitHub
parent 9f7429021d
commit cdf6d79e7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 79 additions and 27 deletions

View file

@ -763,10 +763,16 @@ class JDKDetails {
}
tasks.register("lint") {
// Calls rake's 'lint' task
description = "Lint Ruby source files. Use -PrubySource=file1.rb,file2.rb to specify files"
dependsOn installDevelopmentGems
doLast {
rake(projectDir, buildDir, 'lint:report')
if (project.hasProperty("rubySource")) {
// Split the comma-separated files and pass them as separate arguments
def files = project.property("rubySource").split(",")
rake(projectDir, buildDir, "lint:report", *files)
} else {
rake(projectDir, buildDir, "lint:report")
}
}
}

View file

@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.
require 'shellwords'
namespace "artifact" do
SNAPSHOT_BUILD = ENV["RELEASE"] != "1"
VERSION_QUALIFIER = ENV["VERSION_QUALIFIER"].to_s.strip.empty? ? nil : ENV["VERSION_QUALIFIER"]
@ -127,12 +129,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"

View file

@ -14,7 +14,6 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
namespace "lint" do
module RuboCLI
def self.run!(*args)
@ -25,28 +24,44 @@ namespace "lint" do
end
end
# task that runs lint report
desc "Report all Lint Cops"
task "report" do
RuboCLI.run!("--lint")
desc "Report all Lint Cops. Optional: Specify one or more files"
task :report, [:file] do |t, args|
files = [args[:file], *args.extras].compact
if files.empty?
RuboCLI.run!("--lint")
else
puts "Running lint report on specific files: #{files.join(', ')}"
RuboCLI.run!("--lint", *files)
end
end
# Tasks automatically fixes a Cop passed as a parameter (e.g. Lint/DeprecatedClassMethods)
# TODO: Add a way to autocorrect all cops, and not just the one passed as parameter
desc "Automatically fix all instances of a Cop passed as a parameter"
task "correct", [:cop] do |t, args|
# Tasks automatically fixes a Cop passed as a parameter
desc "Automatically fix all instances of a Cop passed as a parameter. Optional: Specify one or more files"
task :correct, [:cop] do |t, args|
if args[:cop].to_s.empty?
puts "No Cop has been provided, aborting..."
exit(0)
else
puts "Attempting to correct Lint issues for: #{args[:cop].to_s}"
RuboCLI.run!("--autocorrect-all", "--only", args[:cop].to_s)
files = args.extras
if files.empty?
puts "Attempting to correct Lint issues for: #{args[:cop]}"
RuboCLI.run!("--autocorrect-all", "--only", args[:cop])
else
puts "Attempting to correct Lint issues for #{args[:cop]} in files: #{files.join(', ')}"
RuboCLI.run!("--autocorrect-all", "--only", args[:cop], *files)
end
end
end
# task that automatically fixes code formatting
desc "Automatically fix Layout Cops"
task "format" do
RuboCLI.run!("--fix-layout")
desc "Automatically fix Layout Cops. Optional: Specify one or more files"
task :format, [:file] do |t, args|
files = [args[:file], *args.extras].compact
if files.empty?
RuboCLI.run!("--fix-layout")
else
puts "Running format fixes on specific files: #{files.join(', ')}"
RuboCLI.run!("--fix-layout", *files)
end
end
end
end

View file

@ -151,17 +151,24 @@ void buildGem(File projectDir, File buildDir, String gemspec) {
* @param projectDir Gradle projectDir
* @param buildDir Gradle buildDir
* @param plugin Plugin to run specs for
* @param args CLI arguments to pass to rspec
* @param args Optional arguments to pass to the rake task
*/
void rake(File projectDir, File buildDir, String task) {
void rake(File projectDir, File buildDir, String task, String... args) {
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
jruby.currentDirectory = projectDir
jruby.runScriptlet("require 'rake'; require 'time'")
def rakeArgs = args ? "'${args.join("','")}'" : ""
jruby.runScriptlet("""
rake = Rake.application
rake.init
rake.load_rakefile
rake['${task}'].invoke
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
"""
)
}