From d855ac6c88dc984b15ccab8ea17148dba09eb8fc Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Mon, 19 Oct 2026 08:52:30 +0000 Subject: [PATCH] test(bazel): pin vet policy to module toolchain (#7668) * test(bazel): pin vet policy to module toolchain (wy-kk1b9a) Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of Steve Yegge * test(bazel): report an unavailable pinned toolchain as a missing host tool Review fixes for #7668 (adopt-pr iterations 1-2). The GOTOOLCHAIN pin added a failure path that base did not have. A host that runs another Go release and cannot fetch go.mod's toolchain (offline, GOPROXY=off, empty module cache) failed TestPRRunsGoTestsBazelSkips with a bare "exit status 1": cmd.Output() keeps the go command's stderr in the ExitError, and t.Fatal printed only the status. - Report the failure through skipOrFailWithoutHostTool, naming the pinned version and the go command's stderr when it printed any. Plain go test skips; Bazel still fails, and there the registered SDK is go.mod's toolchain, so nothing is fetched. - Compare VET_PASSES in a subtest, so the skip covers only the check that needs the toolchain. Given the parent's t, it skipped the whole test, and with it the six checks after it that read only the checkout. A failed subtest still fails the parent, so Bazel stays fail-closed. - Pad a two-component version ("go 1.26" with no toolchain line) to go1.26.0 in goToolchainName, as scripts/ci/gofmt-bin.sh does. GOTOOLCHAIN=go1.26 is rejected as a language version, not a toolchain version. A table test pins 1.26, 1.26.7 and 1.26rc1. - Say in the helper comment that the pin overrides an ambient GOTOOLCHAIN, local included, and may download into GOMODCACHE. Validated with go1.26.7-built test binaries under env -i, GOPROXY=off, with an empty HOME and module cache unless noted: - go1.26.9 host: the VET_PASSES subtest skips, naming go1.26.7 and "toolchain not available", and the parent passes. With a planted scripts/ci/go-test-vet.sh the parent fails, as on base; iteration 1 skipped the whole test there (rc 0). Same on go1.26.5 with GOTOOLCHAIN=local. - go1.27.1 host with go1.26.7 cached: passes; base fails on stdversion. go1.26.7 host: passes. - Bazel branch (TEST_SRCDIR, BEADS_TEST_GO): SDK go1.26.7 passes. SDK go1.26.9 fails the subtest and the parent with "executor lacks a host tool". A non-executable SDK path ends the message with the exec error, with no dangling ": ". - go.mod "go 1.26" without a toolchain line: passes on go1.26.0 and under Bazel with SDK go1.26.0; a go1.26.9 host skips, naming go1.26.0. Iteration 1's unpadded mutant only skipped under plain go test (it failed under Bazel); the table test now fails it, and fails mutants that count dots or drop the regex anchors. - go test ./scripts on go1.26.7: 306 pass, 0 fail, 1 skip; the only outcomes not on iteration 1 (305/0/1) are the two new tests passing. go vet ./scripts and the pinned gofmt -l are clean. Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- scripts/pr_lanes_bazel_coverage_test.go | 90 ++++++++++++++++++------- 1 file changed, 67 insertions(+), 23 deletions(-) diff --git a/scripts/pr_lanes_bazel_coverage_test.go b/scripts/pr_lanes_bazel_coverage_test.go index 2084b8928..dbde3c13c 100644 --- a/scripts/pr_lanes_bazel_coverage_test.go +++ b/scripts/pr_lanes_bazel_coverage_test.go @@ -1,6 +1,7 @@ package scripts_test import ( + "errors" "os" "os/exec" "path/filepath" @@ -431,28 +432,31 @@ func TestPRRunsGoTestsBazelSkips(t *testing.T) { root := sourceRepoRoot(t) // tools/nogo/analyzers.bzl's VET_PASSES are go test's vet checks (cmd/vet // keeps -bool and -buildtags as aliases of the bools and buildtag - // analyzers). - m := regexp.MustCompile(`(?ms)^VET_PASSES = \[(.*?)^\]`).FindStringSubmatch(readPolicyFile(t, root, "tools/nogo/analyzers.bzl")) - if m == nil { - t.Fatal("tools/nogo/analyzers.bzl has no VET_PASSES list") - } - var passes []string - for _, q := range regexp.MustCompile(`"([a-z]+)"`).FindAllStringSubmatch(m[1], -1) { - passes = append(passes, q[1]) - } - var want []string - for _, flag := range goTestDefaultVetFlags(t) { - flag = strings.TrimPrefix(flag, "-") - if alias, ok := map[string]string{"bool": "bools", "buildtags": "buildtag"}[flag]; ok { - flag = alias + // analyzers). A subtest, so a host that cannot run go.mod's toolchain skips + // only this comparison: the checks after it read nothing but the checkout. + t.Run("VET_PASSES", func(t *testing.T) { + m := regexp.MustCompile(`(?ms)^VET_PASSES = \[(.*?)^\]`).FindStringSubmatch(readPolicyFile(t, root, "tools/nogo/analyzers.bzl")) + if m == nil { + t.Fatal("tools/nogo/analyzers.bzl has no VET_PASSES list") } - want = append(want, flag) - } - sort.Strings(passes) - sort.Strings(want) - if !reflect.DeepEqual(passes, want) { - t.Errorf("tools/nogo/analyzers.bzl VET_PASSES = %v, want go test's vet checks %v", passes, want) - } + var passes []string + for _, q := range regexp.MustCompile(`"([a-z]+)"`).FindAllStringSubmatch(m[1], -1) { + passes = append(passes, q[1]) + } + var want []string + for _, flag := range goTestDefaultVetFlags(t) { + flag = strings.TrimPrefix(flag, "-") + if alias, ok := map[string]string{"bool": "bools", "buildtags": "buildtag"}[flag]; ok { + flag = alias + } + want = append(want, flag) + } + sort.Strings(passes) + sort.Strings(want) + if !reflect.DeepEqual(passes, want) { + t.Errorf("tools/nogo/analyzers.bzl VET_PASSES = %v, want go test's vet checks %v", passes, want) + } + }) if module := readPolicyFile(t, root, "MODULE.bazel"); !strings.Contains(module, "go_sdk.nogo(\n includes = [\"//:__subpackages__\"],\n nogo = \"//tools/nogo\",\n)") { t.Error("MODULE.bazel does not register //tools/nogo for every first-party package (go_sdk.nogo)") } @@ -503,13 +507,34 @@ func TestPRRunsGoTestsBazelSkips(t *testing.T) { // goTestDefaultVetFlags: the vet checks go.mod's Go toolchain gives `go test` // (cmd/go's defaultVetFlags), read from its source, so a Go upgrade that // changes them fails here until tools/nogo/analyzers.bzl follows. Under Bazel the -// toolchain is the registered SDK (testGo). +// toolchain is the registered SDK (testGo), so naming it in GOTOOLCHAIN fetches +// nothing; under plain go test, GOTOOLCHAIN keeps a host Go of another release +// from changing the policy this test validates. Like the Makefile's export, it +// overrides an ambient GOTOOLCHAIN (local included) and may download go.mod's +// toolchain into GOMODCACHE; a host that can neither run nor fetch it (offline, +// GOPROXY=off) lacks a host tool, so t skips (and fails under Bazel): call it +// from a subtest that checks nothing else. func goTestDefaultVetFlags(t *testing.T) []string { t.Helper() - goroot, err := exec.Command(testGo(t), "env", "GOROOT").Output() + root := sourceRepoRoot(t) + version, err := goModToolchainVersion(readPolicyFile(t, root, "go.mod")) if err != nil { t.Fatal(err) } + toolchain := goToolchainName(version) + cmd := exec.Command(testGo(t), "env", "GOROOT") + cmd.Env = append(os.Environ(), "GOTOOLCHAIN="+toolchain) + goroot, err := cmd.Output() + if err != nil { + msg := err.Error() + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + if stderr := strings.TrimSpace(string(exitErr.Stderr)); stderr != "" { + msg += ": " + stderr + } + } + skipOrFailWithoutHostTool(t, "go.mod's %s toolchain is unavailable: %s", toolchain, msg) + } src, err := os.ReadFile(filepath.Join(strings.TrimSpace(string(goroot)), "src", "cmd", "go", "internal", "test", "test.go")) if err != nil { t.Fatalf("reading cmd/go's test.go for defaultVetFlags: %v", err) @@ -532,6 +557,25 @@ func goTestDefaultVetFlags(t *testing.T) []string { return flags } +// goToolchainName: the GOTOOLCHAIN name of a go.mod version. A "go 1.26" +// directive is legal, but toolchain names need the patch component +// (scripts/ci/gofmt-bin.sh pads it the same way); a prerelease such as 1.26rc1 +// has none to add. +func goToolchainName(version string) string { + if regexp.MustCompile(`^[0-9]+\.[0-9]+$`).MatchString(version) { + version += ".0" + } + return "go" + version +} + +func TestGoToolchainNamePadsAMissingPatchComponent(t *testing.T) { + for version, want := range map[string]string{"1.26": "go1.26.0", "1.26.7": "go1.26.7", "1.26rc1": "go1.26rc1"} { + if got := goToolchainName(version); got != want { + t.Errorf("goToolchainName(%q) = %q, want %q", version, got, want) + } + } +} + // bazel.yml's step-3 lanes: in mode remote (always, on covered PRs) every // `bazel test` adds --config=sole-run, whose lines are exactly no cached // test results and no whole-invocation retry after a remote cache eviction;