From 928262ffd40bd171da84d83670e9372d91851984 Mon Sep 17 00:00:00 2001 From: Simon Whitty Date: Mon, 16 Nov 2026 10:32:28 +1100 Subject: [PATCH] Report build warnings as annotations in the Xcode 27 job Tee the build log and add a "Find warnings" step that turns each compiler warning or error into a GitHub annotation, and lists them all in the job summary, since GitHub shows at most 10 annotations per step. Warnings inside a macro expansion such as #expect are reported against the expansion, so they are placed at the "expanded code originates here" note's location instead. NO_COLOR=1 stops swift build writing colour and hyperlink escapes into the piped log. --- .github/actions/annotate-warnings/action.yml | 13 +++ .../actions/annotate-warnings/annotate.awk | 81 +++++++++++++++++++ .github/workflows/build.yml | 9 ++- 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 .github/actions/annotate-warnings/action.yml create mode 100644 .github/actions/annotate-warnings/annotate.awk diff --git a/.github/actions/annotate-warnings/action.yml b/.github/actions/annotate-warnings/action.yml new file mode 100644 index 00000000..bd11878e --- /dev/null +++ b/.github/actions/annotate-warnings/action.yml @@ -0,0 +1,13 @@ +name: 'Annotate Warnings' +description: 'Report compiler warnings and errors from a swift build log as GitHub annotations' +inputs: + log: + description: 'path to the build log, written with NO_COLOR=1' + required: true + +runs: + using: "composite" + steps: + - name: 'Annotate' + run: awk -v root="$GITHUB_WORKSPACE/" -v summary="$GITHUB_STEP_SUMMARY" -f ./.github/actions/annotate-warnings/annotate.awk "${{ inputs.log }}" + shell: bash diff --git a/.github/actions/annotate-warnings/annotate.awk b/.github/actions/annotate-warnings/annotate.awk new file mode 100644 index 00000000..c41e7269 --- /dev/null +++ b/.github/actions/annotate-warnings/annotate.awk @@ -0,0 +1,81 @@ +# Turns swift compiler diagnostics into GitHub workflow commands, so they show +# as annotations on the run and on the PR diff. +# +# /path/File.swift:12:5: warning: message +# +# A diagnostic inside a macro expansion such as #expect is reported against the +# expansion, with the source location on the following note: +# +# macro expansion #expect:1:22: warning: message +# `- /path/File.swift:118:10: note: expanded code originates here +# +# Without that note, it is reported without a location. Diagnostics repeat, +# for example when a file is compiled twice or #expect reports one twice, so +# each message is reported once per line. +# +# GitHub shows at most 10 annotations of each type per step, so when `summary` +# names a file (GITHUB_STEP_SUMMARY), every diagnostic is also listed there. + +function escape_data(s) { + gsub(/%/, "%25", s); gsub(/\r/, "%0D", s); gsub(/\n/, "%0A", s) + return s +} + +function escape_property(s) { + s = escape_data(s); gsub(/:/, "%3A", s); gsub(/,/, "%2C", s) + return s +} + +function escape_cell(s) { + gsub(/\|/, "\\|", s) + return s +} + +function annotate(file, line, col, type, message, key) { + if (index(file, root) == 1) file = substr(file, length(root) + 1) + key = file ":" line ":" type ":" message + if (key in seen) return + seen[key] = 1 + count[type]++ + list[++total] = sprintf("| %s | %s | %s |", type, (file == "" ? "" : "`" file ":" line "`"), escape_cell(message)) + if (file == "") + printf "::%s::%s\n", type, escape_data(message) + else + printf "::%s file=%s,line=%s,col=%s::%s\n", type, escape_property(file), line, col, escape_data(message) +} + +# location prefix "path:line:col: " followed by "warning: " or "error: " +function parse(text, out, m) { + if (!match(text, /:[0-9]+:[0-9]+: (warning|error|note): /)) return 0 + out["file"] = substr(text, 1, RSTART - 1) + split(substr(text, RSTART + 1, RLENGTH - 3), m, ":") + out["line"] = m[1]; out["col"] = m[2]; out["type"] = m[3]; sub(/^ /, "", out["type"]) + out["message"] = substr(text, RSTART + RLENGTH) + return 1 +} + +{ + if (pending_type != "") { + if (sub(/^`- /, "") && parse($0, d) && d["type"] == "note" && d["message"] == "expanded code originates here") { + annotate(d["file"], d["line"], d["col"], pending_type, pending_message) + pending_type = "" + next + } + annotate("", "", "", pending_type, pending_message) + pending_type = "" + } + if (/^macro expansion / && parse($0, d) && d["type"] != "note") { + pending_type = d["type"]; pending_message = d["message"] + next + } + if (/^\// && parse($0, d) && d["type"] != "note") { + annotate(d["file"], d["line"], d["col"], d["type"], d["message"]) + } +} + +END { + printf "%d warning(s), %d error(s)\n", count["warning"], count["error"] + if (summary == "" || total == 0) exit + printf "### Build diagnostics\n\n%d warning(s), %d error(s)\n\n| | Location | Message |\n|---|---|---|\n", count["warning"], count["error"] >> summary + for (i = 1; i <= total; i++) print list[i] >> summary +} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9e67475e..e07041a0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,7 +28,14 @@ jobs: - name: Version run: swift --version - name: Build - run: swift build --build-tests --enable-code-coverage + run: | + set -o pipefail + NO_COLOR=1 swift build --build-tests --enable-code-coverage 2>&1 | tee build.log + - name: Find warnings + if: ${{ !cancelled() }} + uses: ./.github/actions/annotate-warnings + with: + log: build.log - name: Test run: swift test --skip-build --enable-code-coverage --xunit-output result.xml timeout-minutes: 1