* fix(ci): resolve gofmt from go.mod's toolchain, not PATH (be-gx8) gofmt's output is not stable across Go releases, so whichever binary runs decides the formatting verdict. Three sites reached for a bare `gofmt` on PATH -- scripts/ci/fmt-check.sh, the Makefile fmt target, and .githooks/pre-commit -- and PATH belongs to whatever Go is installed locally. CI has no such freedom: actions/setup-go installs exactly the go.mod version via go-version-file. GOTOOLCHAIN=auto does not correct for this, which is why `go env GOROOT` or `go fmt` would not have been a fix either. The toolchain switch only ever moves UP to satisfy the go directive, never down, so a host running a newer Go keeps formatting with the newer gofmt. Measured against go.mod's go 1.26.5 on a go1.27.0 host: 1.27 flags three role_fixture_kit_test.go files that 1.26.5 calls clean, and no branch touches them. The failure ran in the dangerous direction. The required lint gate went red on untouched code, which reads as "main's lint is broken" and poisons the pre-existing-on-main judgement every PR author uses to decide whether a red gate is theirs. Its own advice -- "Run 'make fmt' to fix formatting" -- then rewrote those files into a form CI's pinned gofmt rejects. The pre-commit hook did that same rewrite and re-staged it silently on every Go commit, with nobody choosing it. Route all three through one resolver rather than patch three sites, so the next gofmt call site cannot drift. scripts/ci/gofmt-bin.sh names go.mod's toolchain explicitly and reads GOROOT from it, short-circuiting to a plain `go env GOROOT` when the host go already is the pinned version (the CI case, so nothing is fetched where nothing needs fetching). A fallback to the PATH binary stays available but warns loudly on stderr: silently falling back is indistinguishable from resolving correctly, and reintroduces exactly this skew. fmt-check.sh now prints the gofmt version it used, so a skew presents as a skew instead of as a repo defect. The new tests pin the resolution, not the reporting, because the resolution is where the bug lived: every output assertion around the bare gofmt stayed green. Reverting gofmt-bin.sh to `command -v gofmt` fails all three. The pre-existing fmt-check tests move their shim from PATH to GOFMT -- a PATH shim is now correctly ignored, so leaving them there would have made them grade the real toolchain instead of the case they name. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PoN7f9TMmGTCLRpDPzSH4y Executed-By: beadsrig/polecats/nux * fix(ci): resolve gofmt from go.mod's toolchain directive, and declare it to Bazel Maintainer review fixes for #6294 (adoption iterations 1 and 2, one squashed commit). Sections 1-6 address all six findings in the attempt-1 synthesis; the two majors are the substantive ones. Sections 7-8 address the two minors the attempt-2 synthesis left (it decided approve, 1000/1000, no blocker or major). 1. Contract & Interface Fidelity (major, the scorecard's only Required Change). gofmt-bin.sh read the `go` directive (1.26.5) while go.mod also carries `toolchain go1.26.7`, and toolchain-first is the repo's own declared precedence -- Makefile:85-90 exports GOTOOLCHAIN from the same pair for every make target, and scripts/bazel_policy_test.go's goModToolchainVersion is the same rule in Go. The consequence was not cosmetic: `go env GOVERSION` at the repo root is go1.26.7 (the directive auto-switches), so the fast-path test compared go1.26.7 against go1.26.5 and was false by construction on every route, sending each required fmt-check run down the elif arm to fetch a second, older toolchain. Parse `toolchain go<version>` first, fall back to the `go` directive. Measured after the change: pinned=1.26.7, the fast path fires (`go env GOVERSION` then `go env GOROOT`, no GOTOOLCHAIN= prefix and nothing fetched), and `make fmt-check` now reports `Using gofmt go1.26.7 (.../toolchain@v0.0.1-go1.26.7.../bin/gofmt)`. .githooks/pre-commit:24's `go`-directive read is deliberately left alone -- its comment explains it is a golangci-lint pin for a different reason. gofmtbin_test.go's helper now implements the same precedence, and TestGofmtBinMatchesGoModDirective is renamed TestGofmtBinMatchesGoModToolchain so the pinned contract is legible from the test name. Deviation from the prescription, recorded deliberately: the review asked for a helper "name" change as well. The name goModToolchainVersion already described toolchain-first semantics; it was the implementation that did not. Correcting the semantics is what removes the collision with scripts/bazel_policy_test.go's identically-named, opposite-behaving helper, so the name is kept and the doc comment now states the precedence and cites both in-repo sources of it. 2. Release Safety (major). fmt-check.sh now execs a sibling script unconditionally under `set -euo pipefail`. Live origin/main gives //scripts/prlintmake a `data` list containing exactly ci/fmt-check.sh, so on merge all three TestFmtCheck* cases would have died at rc 127 in the Bazel runfiles sandbox -- the GOFMT short-circuit cannot rescue them because it lives inside the missing script. Export ci/gofmt-bin.sh to //scripts/prlintmake and put it in the target's `data`, and add gofmtbin_test.go to `srcs` (it carries no build tag, so its omission was not explained away). Only ci/gofmt-bin.sh is exported here, not ci/fmt-check.sh: main already exports that one for this same package, and a second exports_files call naming the same file in one package is an error. This base predates both of main's additions, which is also why editing the three-line go_test rule conflicts with main's independent edit of it. Verified by materialising the merge: every other file auto-merges and the resolution is the union of the two attribute sets (srcs = both test files; data = both scripts plus //:go.mod per section 7; keep main's deps on //internal/testutil/bazeltest). That deps edge is load-bearing at main even though it was stale at this base: main rewrote sourceRepoRoot to bazeltest.CallerDir, so the union must keep it. 3. Test Evidence Quality (minor). environment() starts from os.Environ(), so deleting GOFMT from the override map did not unset it. Export it empty instead -- gofmt-bin.sh reads `[[ -n "${GOFMT:-}" ]]`, so empty already is its "not set" case, and the override test stays distinguishable by setting a non-empty value. 4. Architectural Consistency (minor). "Every gofmt invocation in this repo goes through here" was false at authoring: cmd/bd/preflight.go's doctor check is a fourth front door on bare PATH gofmt. Scope the claim to the repo's shell-side call sites (re-censused: fmt-check.sh, the Makefile fmt target, and .githooks/pre-commit are the complete set; the two workflow "Check gofmt" steps and scripts/ci/pr-lint.sh reach them through make fmt-check), and name the in-binary exception with the reason it is one -- bd runs against whatever project the user points it at, so it has no scripts/ci/ to call. preflight.go is byte-identical at base, at HEAD and at origin/main, so nothing in this PR widens it; converting it is a separate change. 5. Test Evidence Quality (minor). describe_gofmt's version-bearing arm had zero coverage -- all three existing cases inject a bash script, for which `go version` errors and only the bare-path arm can run. Add TestFmtCheckReportsGofmtVersion against a real Go-built no-op binary, which is the only shim shape `go version` can report on. 6. Error Handling & Resilience (nit). Warn (without refusing) when GOFMT names something non-executable, so a typo is reported by the resolver that accepted it rather than as a "not found" from whichever call site ran it; and guard the fields index in the resolver test. Mutation proofs, each red isolated to the intended test with the other six green (7/7 pass unmutated, `go test ./scripts/prlintmake/`): - revert the toolchain-first parse -> resolver returns the 1.26.5 gofmt and TestGofmtBinMatchesGoModToolchain alone fails. - delete describe_gofmt's `if command -v go` block (codex's own mutation, previously all-green) -> TestFmtCheckReportsGofmtVersion alone fails, the three incumbents stay green. That asymmetry is both the coverage-hole proof and the proof the new case is not a duplicate. - ambient GOFMT, two values x two arms: with the fix both /nonexistent and a correctly-pinned gofmt leave all three resolver tests passing; with the delete restored, /nonexistent fails TestGofmtBinMatchesGoModToolchain with a message blaming the script, and a correctly-pinned value turns all three green vacuously without the resolver ever running. 7. Release Safety (minor, iteration 2). The target now reads go.mod at run time -- the resolver opens "$REPO_ROOT/go.mod" and the test helper opens filepath.Join(sourceRepoRoot(), "go.mod"), where the base test read it not at all -- but go.mod appeared in no `data` list, so the rule declared strictly less than it uses. Add //:go.mod to //scripts/prlintmake's `data`. No new export is needed: //:BUILD.bazel already exports go.mod publicly, and //internal/storage/dolt and //internal/testutil/integration already consume it as "//:go.mod" in `data`, so this is the sanctioned shape rather than a new one. 8. Contract & Interface Fidelity (minor, iteration 2). `toolchain default` is a legal go.mod directive value, and the parse matched on the field name alone, so sub(/^go/, "", "default") no-opped and left pinned=default. Because that is non-empty, neither the `go`-directive fallback nor the "no toolchain or go directive" guard could fire, and the script degraded to the bare PATH gofmt it exists to eliminate while a usable `go 1.26.5` sat two lines above. The mangled "godefault" in the warning was the tell. Require the literal `go` prefix, which is exactly what the repo's two other readers of this pair already do (Makefile:85's `s/^toolchain go//p` and bazel_policy_test.go's `^toolchain\s+go(\S+)\s*$`), so `default` falls through to the `go` directive. The same guard is added to gofmtbin_test.go's goModToolchainVersion, and that is not redundant: strings.TrimPrefix("default", "go") returns "default" unchanged and withPatchComponent leaves a dotless string alone, so the helper mirrored the bug exactly and the test could never have caught it. Guarding only the script would have left the test agreeing with a buggy resolver. Measured on a synthetic tree, five arms (the script resolves REPO_ROOT from its own location, so each arm is a copied script beside a written go.mod): - `toolchain default` + `go 1.26.5`: before, /usr/local/go/bin/gofmt with the "godefault" warning; after, the go1.26.5 toolchain gofmt -- i.e. the `go` directive, identical to the arm where no toolchain line exists. - `toolchain go1.26.7`: unchanged before and after (no regression). - `go 1.26.5` alone: unchanged before and after (no regression). - `toolchain default` with no `go` directive: now reaches the previously bypassed "no toolchain or go directive" guard and falls back loudly, with the correct message rather than the mangled one. - `toolchain default` written before `go`: also resolves 1.26.5, so the fallthrough is order-independent. The Go helper's parse was checked over the same six inputs and now agrees with the shell on every one. No regression test is added for this case. Doing so would need a new synthetic-go.mod fixture -- every existing case runs the real script against the real repo go.mod through sourceRepoRoot() -- which is a new test instrument rather than the prescribed remedy, on a finding whose gate impact is none (go.mod carries `toolchain go1.26.7`, so nothing can fire today). Iteration 2 validation: go test ./scripts/prlintmake/ -count=1 -v 7/7 PASS (same roster as iteration 1, no test added or removed), go vet rc 0, go test ./scripts/ -run 'Bazel|BUILD' 11/11 PASS (the BUILD-policy gate, run because this touches a BUILD file), python3 tools/bazel/go_srcs.py --check rc 0 (`srcs` is unchanged; only `data` moved), make fmt-check rc 0 still reporting go1.26.7, shellcheck clean, git diff --check clean. Also run: go vet ./scripts/prlintmake, go test ./scripts/... (all packages ok), make fmt-check (rc 0), python3 tools/bazel/go_srcs.py --check (rc 0), shellcheck on both changed scripts. --------- Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
47 lines
1.3 KiB
Bash
Executable file
47 lines
1.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Shared Go formatting check for Make and PR lint wrappers.
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
|
|
|
cd "$REPO_ROOT"
|
|
|
|
# Which gofmt runs decides the verdict, so resolve it from go.mod instead of
|
|
# PATH and report it. Naming the binary here is what makes a toolchain skew
|
|
# legible as a skew, rather than as unformatted files in code no branch
|
|
# touched. See scripts/ci/gofmt-bin.sh.
|
|
GOFMT_BIN="$("$SCRIPT_DIR/gofmt-bin.sh")"
|
|
|
|
describe_gofmt() {
|
|
local bin="$1" version=""
|
|
if command -v go >/dev/null 2>&1; then
|
|
version="$(go version "$bin" 2>/dev/null | awk '{ print $NF }')"
|
|
fi
|
|
if [[ -n "$version" ]]; then
|
|
printf '%s (%s)' "$version" "$bin"
|
|
else
|
|
printf '%s' "$bin"
|
|
fi
|
|
}
|
|
|
|
printf 'Checking Go formatting...\n'
|
|
printf 'Using gofmt %s\n' "$(describe_gofmt "$GOFMT_BIN")"
|
|
if UNFORMATTED="$("$GOFMT_BIN" -l .)"; then
|
|
:
|
|
else
|
|
status=$?
|
|
printf 'gofmt failed while checking formatting\n' >&2
|
|
exit "$status"
|
|
fi
|
|
|
|
if [[ -n "$UNFORMATTED" ]]; then
|
|
printf 'The following files are not properly formatted:\n'
|
|
printf '%s\n' "$UNFORMATTED"
|
|
printf '\n'
|
|
printf "Run 'make fmt' to fix formatting\n"
|
|
exit 1
|
|
fi
|
|
|
|
printf 'All Go files are properly formatted\n'
|