Skip to content

Commit 34c91c4

Browse files
syedazeez337qmonnet
authored andcommitted
contrib/scripts: fix check-fmt.sh aborting with exit 123 on Go 1.26+
gofmt was changed in Go 1.26 to exit non-zero when -d finds a diff (https://cs.opensource.google/go/go/+/d945600d060e7a0b7c5e72ac606a017d105a17f3). Combined with set -e and set -o pipefail at the top of the script, that exit propagates through xargs as code 123 and kills the script before the "Unformatted Go source code:" branch can run. The contributor sees a cryptic "Error 123" with no diff instead of the helpful diff output the script was designed to produce. Now that go.mod requires Go >= 1.26, every contributor running 'make precheck' on a freshly-bumped toolchain hits this. Capture the gofmt output and exit code explicitly with set +e/-e around the pipeline, then dispatch: - non-empty diff -> exit 1 with the helpful message (existing behavior). - empty diff but non-zero gofmt exit -> surface the real gofmt error on stderr and propagate the exit code. - clean tree -> exit 0. Verified manually with Go 1.26.2 by introducing a formatting issue in a sample file and running the script before and after the fix: Before: exit 123, no output. After: exit 1, prints "Unformatted Go source code:" + the diff. A clean tree still exits 0. Fixes: cilium#45677 Signed-off-by: Azeez Syed <syedazeez337@gmail.com>
1 parent 3aa7e07 commit 34c91c4

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

contrib/scripts/check-fmt.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,32 @@ set -o pipefail
55

66
_goroot=$(${GO:-go} env GOROOT)
77

8+
# Go 1.26 changed `gofmt -d` to exit non-zero when a diff is found
9+
# (https://cs.opensource.google/go/go/+/d945600d060e7a0b7c5e72ac606a017d105a17f3),
10+
# which combined with `set -e -o pipefail` above kills this script before the
11+
# user-friendly "Unformatted Go source code" branch below can run. Capture the
12+
# output and exit code explicitly so we can dispatch on whether a diff was found
13+
# (exit 1 with helpful message) versus a real gofmt failure (propagate the code).
14+
set +e
815
diff="$(find . ! \( -path './contrib' -prune \) \
916
! \( -regex '.*/vendor/.*' -prune \) \
1017
! \( -path './_build' -prune \) \
1118
! \( -path './.git' -prune \) \
1219
! \( -path '*.validate.go' -prune \) \
1320
-type f -name '*.go' | grep -Ev "(pkg/k8s/apis/cilium.io/v2/client/bindata.go)" | \
1421
xargs $_goroot/bin/gofmt -d -l -s )"
22+
gofmt_exit=$?
23+
set -e
1524

1625
if [ -n "$diff" ]; then
1726
echo "Unformatted Go source code:"
1827
echo "$diff"
1928
exit 1
2029
fi
2130

31+
if [ "$gofmt_exit" -ne 0 ]; then
32+
echo "gofmt exited with $gofmt_exit but produced no diff" >&2
33+
exit "$gofmt_exit"
34+
fi
35+
2236
exit 0

0 commit comments

Comments
 (0)