diff --git a/dev/scalastyle b/dev/scalastyle index 3db06ada8a89d..5b3f880b59e37 100755 --- a/dev/scalastyle +++ b/dev/scalastyle @@ -45,9 +45,11 @@ if test ! -z "$ERRORS"; then # break every regex below. Cheap to harden. ERRORS_PLAIN=$(printf '%s' "$ERRORS" | sed -E $'s/\x1b\\[[0-9;]*[A-Za-z]//g') # Helper: emit one `::error` annotation. Centralised so the two regex - # branches below stay short. + # branches below stay short. `col` is optional -- GitHub's annotation + # command accepts a `col=` parameter, but not every scalastyle + # checker reports a column. emit_annotation() { - local file="$1" lineno="$2" msg="$3" + local file="$1" lineno="$2" msg="$3" col="${4:-}" # Strip the GitHub Actions workspace prefix so the annotation # references the path as it appears in the repo. local file_rel="${file#${GITHUB_WORKSPACE:-}/}" @@ -57,8 +59,12 @@ if test ! -z "$ERRORS"; then local msg_escaped="${msg//%/%25}" msg_escaped="${msg_escaped//$'\r'/%0D}" msg_escaped="${msg_escaped//$'\n'/%0A}" - printf '::error file=%s,line=%s,title=Scalastyle::%s\n' \ - "$file_rel" "$lineno" "$msg_escaped" + local col_param="" + if [[ -n "$col" ]]; then + col_param=",col=$col" + fi + printf '::error file=%s,line=%s%s,title=Scalastyle::%s\n' \ + "$file_rel" "$lineno" "$col_param" "$msg_escaped" } printf '%s\n' "$ERRORS_PLAIN" | while IFS= read -r raw; do # Two scalastyle output formats reach us: @@ -75,14 +81,23 @@ if test ! -z "$ERRORS"; then # through `streams.value.log.error(...)` -- which is what the # explicit `scalastyle` / `test:scalastyle` tasks invoked by # this script do, and so this is the format we see in CI: - # [error] :: - # The leading `[error] ` plus a single `::` (with no - # `::` follow-up) is what tells it apart from a regular - # Scala compile error of shape `[error] ::: `. - if [[ "$raw" =~ ^error[[:space:]]+file=([^[:space:]]+)[[:space:]]+message=(.*)[[:space:]]+line=([0-9]+)([[:space:]]+column=[0-9]+)?$ ]]; then - emit_annotation "${BASH_REMATCH[1]}" "${BASH_REMATCH[3]}" "${BASH_REMATCH[2]}" - elif [[ "$raw" =~ ^\[error\][[:space:]]+(/[^:[:space:]]+):([0-9]+):[[:space:]]+(.+)$ ]]; then - emit_annotation "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}" + # [error] :[:]: + # The column is present for some checkers (e.g. `nonascii`) + # and absent for others, so it's optional here too. This + # shape is unambiguously a style violation rather than a + # genuine Scala compile error of the same + # `[error] ::: ` shape: SparkBuild.scala + # deliberately does not attach style checking to + # `(Compile / compile)` (see `enableScalaStyle`), precisely so + # a broken compile elsewhere can't cascade into the style job + # and vice versa -- a real compiler error structurally cannot + # reach this script's input. + if [[ "$raw" =~ ^error[[:space:]]+file=([^[:space:]]+)[[:space:]]+message=(.*)[[:space:]]+line=([0-9]+)([[:space:]]+column=([0-9]+))?$ ]]; then + emit_annotation "${BASH_REMATCH[1]}" "${BASH_REMATCH[3]}" "${BASH_REMATCH[2]}" \ + "${BASH_REMATCH[5]}" + elif [[ "$raw" =~ ^\[error\][[:space:]]+(/[^:[:space:]]+):([0-9]+):(([0-9]+):)?[[:space:]]+(.+)$ ]]; then + emit_annotation "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[5]}" \ + "${BASH_REMATCH[4]}" fi done fi