Files
2026-12-02 11:36:42 +00:00

69 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run a cargo command so that what goes wrong can be read from the run's annotations, which
# need no login, as well as from its log, which does.
#
# llembas/ci/cargo-annotated.sh build --release --locked -p wezterm-gui
# llembas/ci/cargo-annotated.sh test --release --locked -p config
#
# cargo is asked for its messages as JSON (the option is put right after the subcommand, so
# no `--` with arguments for the tests may be given). Then:
# - every compiler error is printed as the compiler rendered it, and becomes an annotation at
# its file and line. A step shows ten annotations at most, so the first nine stand alone and
# the next thirty share the tenth;
# - what the tests printed is shown as it was, and when they failed the lines that say which
# and why become one annotation;
# - a failure that is neither (a build script, the linker, cargo itself) is annotated with the
# end of what cargo wrote.
# The exit status is cargo's.
set -uo pipefail
[ "$#" -ge 1 ] || { echo "usage: cargo-annotated.sh <subcommand> [arguments]" >&2; exit 2; }
work="$(mktemp -d)"
trap 'rm -rf "${work:?}"' EXIT
sub="$1"
shift
"${CARGO:-cargo}" "$sub" --message-format=json "$@" >"$work/out" 2> >(tee "$work/err" >&2)
rc=$?
# the tee above is another process: let it finish writing
sleep 1
# What is not a JSON message is what a test binary printed.
grep -v '^{' "$work/out" >"$work/plain" || true
cat "$work/plain"
# One line per error: the annotation command, with the message escaped for it.
jq -rR '
def esc: gsub("%"; "%25") | gsub("\r"; "%0D") | gsub("\n"; "%0A");
fromjson?
| select(type == "object" and .reason == "compiler-message")
| .message
| select(.level == "error" or .level == "error: internal compiler error")
| select((.message // "") | startswith("aborting due to") | not)
| ((.spans // []) | map(select(.is_primary)) | .[0]) as $s
| ((.rendered // .message // "") | .[0:1800] | esc) as $text
| if $s then "::error file=\($s.file_name),line=\($s.line_start),title=rustc::\($text)"
else "::error title=rustc::\($text)" end
' "$work/out" >"$work/errors" 2>/dev/null || true
# In the log: every error as rendered, whether cargo failed or not.
jq -rR 'fromjson? | select(type == "object" and .reason == "compiler-message") | .message
| select(.level | startswith("error")) | .rendered // .message' "$work/out" 2>/dev/null || true
if [ "$rc" -ne 0 ] && [ -n "${GITHUB_ACTIONS:-}" ]; then
count="$(wc -l <"$work/errors")"
head -n 9 "$work/errors"
if [ "$count" -gt 9 ]; then
rest="$(sed -n '10,39p' "$work/errors" | sed 's/^::error [^:]*title=rustc:://; s/^::error title=rustc:://' | awk 'BEGIN{ORS="%0A%0A"} {print substr($0, 1, 700)}')"
printf '::error title=rustc (%s errors in all, from the tenth)::%s\n' "$count" "$rest"
fi
failing="$(grep -E -A6 'panicked at|^---- .* ----$|^test .* FAILED$|^failures:$|test result: FAILED' "$work/plain" | head -n 80 | head -c 6000)"
if [ -n "$failing" ]; then
printf '::error title=cargo %s: tests::%s\n' "$sub" "$(printf '%s' "$failing" | sed 's/%/%25/g' | awk 'BEGIN{ORS="%0A"} {print}')"
fi
if [ "$count" -eq 0 ] && [ -z "$failing" ]; then
printf '::error title=cargo %s failed::%s\n' "$sub" "$(tail -n 40 "$work/err" | head -c 6000 | sed 's/%/%25/g' | awk 'BEGIN{ORS="%0A"} {print}')"
fi
fi
exit "$rc"