beads/scripts/pr-lint/main.go
Julian Knutsen a4509deb2d
ci(bazel): nogo replaces go vet and golangci-lint (ga-96smfk.18) (#7341)
* ci(bazel): nogo replaces go vet and golangci-lint (ga-96smfk.18)

Lint and vet now run as nogo inside the Bazel build, so they gate wherever
the Bazel lanes execute (rbe-west), and the golangci-lint and go vet jobs
leave pr.yml and main.yml.

- //tools/nogo (own go.mod, joined to beads' for go_deps only through
  gazelle.go.work): go test's vet checks (cmd/go's defaultVetFlags, the set
  scripts/ci/go-test-vet.sh ran) plus the eight linters .golangci.yml
  enables, each a wrapper over the library version golangci-lint v2.10.1
  uses (depguard, errcheck, forbidigo, gosec, misspell, sloglint, unconvert,
  unparam).
- .golangci.yml stays the single lint policy: Bazel embeds it and
  tools/nogo/internal/golangci applies it as golangci-lint does (enabled
  set, settings, run.tests: false as library-unit-only analysis, lax
  generated-file detection, path/path-except/text exclusion rules), with
  strict decoding so an unimplemented key fails the build.
- gascity's rules_go //nolint-range patch and x/tools ctrlflow patch,
  byte-identical.
- Windows/darwin: cross-configured, not a golangci leg. The pure lane's
  release cross-compile (//tools/bazel:release_cross, every library and
  binary for all eight release platforms, cgo off) runs nogo on every
  compile, a superset of golangci-lint's windows and darwin legs.
  scripts/pr-lint (make ci-pr-lint, bd preflight) runs `bazel build
  --config=nogo //...` plus release_cross for windows_amd64,darwin_arm64.
  release_cross skips tools/nogo, a separate Go module `go build ./...`
  never compiled.
- pr.yml: scripts-go-checks (vet) and pr-lint-wrapper (x3) are gone, with
  their ci-gate entries; main.yml: pr-lint-wrapper (x6) and go-vet-cache.
  scripts/ci/go-test-vet.sh and install-golangci-lint.sh are deleted.
- pre-commit hook and .pre-commit-config.yaml run `make lint-changed
  LINT_CHANGED_SCOPE=staged` (nogo over the staged packages); Makefile adds
  lint, vet and lint-changed. pr-lint.sh no longer runs gofmt
  (//scripts/repochecks:fmt_test owns it; ga-96smfk.41).
- internal/testutil/integration: five gosec findings in integration-tagged
  library files golangci-lint never built, now linted by the integration
  lanes, carry reasoned //nolint directives.

Parity: with the exclusion rules removed, golangci-lint and nogo report the
same 3,916 findings natively and 3,859 each for windows and darwin (file,
line, linter, text); with .golangci.yml as committed both report none.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* ci(bazel): equivalence.py leaves nested Go modules' tests out of go test's set

tools/nogo is its own Go module, so `go test ./...` in the root never runs
its tests; Bazel's unit lane does. Its packages are neither expected nor
extra.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-07 05:17:22 +00:00

209 lines
6.5 KiB
Go

// pr-lint runs the repository's lint gate without requiring Make or Bash:
// nogo (//tools/nogo: go test's vet checks plus the golangci-lint linters
// .golangci.yml enables) under Bazel. The supported scripts/ci/pr-lint.sh
// entrypoint delegates here, and bd preflight runs this checkout-owned
// command for the Beads source tree.
//
// BD_LINT_TARGETS (a comma list of native, windows and darwin; default all)
// selects what is analyzed:
//
// - native: `bazel build --config=nogo //...`, every package and test, in
// the configuration of bazel.yml's unit lane.
// - windows, darwin: //tools/bazel:release_cross, every go_library and
// go_binary split-transitioned to windows/amd64 and darwin/arm64 without
// cgo, so the files only those platforms compile (//go:build windows,
// darwin, !linux, ...) are analyzed on any host, as golangci-lint's
// former GOOS=windows/darwin legs did. bazel.yml's pure lane builds the
// same target for every release platform.
//
// Bazel executes remotely when the developer's or CI's rc configures it.
package main
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"strings"
"time"
)
const passTimeout = 30 * time.Minute
// defaultLintTargets is BD_LINT_TARGETS' value when unset or empty: the
// no-argument usage contract (`make ci-pr-lint`, `bd preflight`) runs all
// three.
var defaultLintTargets = []string{"native", "windows", "darwin"}
// crossPlatforms maps the cross targets to rules_go's non-cgo platforms.
var crossPlatforms = []struct{ target, platform string }{
{"windows", "windows_amd64"},
{"darwin", "darwin_arm64"},
}
type commandSpec struct {
name string
args []string
dir string
}
type processRunner interface {
lookPath(string) (string, error)
run(context.Context, commandSpec, io.Writer, io.Writer) error
}
type osProcessRunner struct{}
func (osProcessRunner) lookPath(name string) (string, error) {
return exec.LookPath(name)
}
func (osProcessRunner) run(ctx context.Context, spec commandSpec, stdout, stderr io.Writer) error {
cmd := exec.CommandContext(ctx, spec.name, spec.args...) //nolint:gosec // G204: the Bazel launcher with internally assembled arguments
cmd.Dir = spec.dir
cmd.Stdout = stdout
cmd.Stderr = stderr
return cmd.Run()
}
func main() {
dir, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "determine repository root: %v\n", err)
os.Exit(1)
}
os.Exit(run(os.Args[1:], dir, os.Environ(), os.Stdout, os.Stderr, osProcessRunner{}))
}
func run(args []string, dir string, environ []string, stdout, stderr io.Writer, runner processRunner) int {
if len(args) != 0 {
fmt.Fprintln(stderr, "usage: go run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint")
return 2
}
caseInsensitive := runtime.GOOS == "windows"
targets, err := parseLintTargets(environmentValue(environ, "BD_LINT_TARGETS", caseInsensitive))
if err != nil {
fmt.Fprintln(stderr, err)
return 2
}
bazel, err := findBazel(environmentValue(environ, "BAZEL", caseInsensitive), runner)
if err != nil {
fmt.Fprintln(stderr, err)
return 1
}
if containsTarget(targets, "native") {
spec := commandSpec{name: bazel, args: []string{"build", "--config=nogo", "//..."}, dir: dir}
if code := runPass("nogo (native)", spec, stdout, stderr, runner); code != 0 {
return code
}
}
var platforms []string
for _, p := range crossPlatforms {
if containsTarget(targets, p.target) {
platforms = append(platforms, p.platform)
}
}
if len(platforms) > 0 {
label := "nogo (" + strings.Join(platforms, ", ") + "; non-cgo)"
spec := commandSpec{
name: bazel,
args: []string{"build", "--config=nogo-cross", "--//tools/bazel:release_platforms=" + strings.Join(platforms, ","), "//tools/bazel:release_cross"},
dir: dir,
}
if code := runPass(label, spec, stdout, stderr, runner); code != 0 {
return code
}
}
return 0
}
// findBazel resolves the Bazel launcher: $BAZEL (the Makefile's variable),
// else bazel, else bazelisk.
func findBazel(override string, runner processRunner) (string, error) {
candidates := []string{"bazel", "bazelisk"}
if override != "" {
candidates = []string{override}
}
for _, name := range candidates {
if path, err := runner.lookPath(name); err == nil {
return path, nil
}
}
return "", fmt.Errorf("bazel not found in PATH (tried %s); install bazelisk (https://github.com/bazelbuild/bazelisk) or set BAZEL", strings.Join(candidates, ", "))
}
func runPass(label string, spec commandSpec, stdout, stderr io.Writer, runner processRunner) int {
fmt.Fprintf(stdout, "==> %s\n", label)
ctx, cancel := context.WithTimeout(context.Background(), passTimeout)
defer cancel()
err := runner.run(ctx, spec, stdout, stderr)
if err == nil {
fmt.Fprintf(stdout, "<== %s succeeded\n", label)
return 0
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
fmt.Fprintf(stderr, "<== %s exceeded %s\n", label, passTimeout)
return 1
}
fmt.Fprintf(stderr, "<== %s failed: %v\n", label, err)
return processExitCode(err)
}
// parseLintTargets parses BD_LINT_TARGETS: a comma-separated list of
// "native", "windows" and "darwin", in any order and with any repetition.
// An empty value (unset, or only whitespace and commas) selects all three.
// Any other token is an error: callers exit 2, the usage-error exit code.
func parseLintTargets(raw string) ([]string, error) {
var targets []string
for _, field := range strings.Split(raw, ",") {
target := strings.TrimSpace(field)
if target == "" {
continue
}
if !containsTarget(defaultLintTargets, target) {
return nil, fmt.Errorf("BD_LINT_TARGETS: unknown target %q (want a comma list of native, windows, darwin)", target)
}
targets = append(targets, target)
}
if len(targets) == 0 {
return defaultLintTargets, nil
}
return targets, nil
}
func containsTarget(targets []string, target string) bool {
for _, candidate := range targets {
if candidate == target {
return true
}
}
return false
}
// environmentValue returns the last value of wanted in environ; Windows
// environment names are case-insensitive.
func environmentValue(environ []string, wanted string, caseInsensitive bool) string {
for index := len(environ) - 1; index >= 0; index-- {
key, value, ok := strings.Cut(environ[index], "=")
if ok && (key == wanted || caseInsensitive && strings.EqualFold(key, wanted)) {
return value
}
}
return ""
}
func processExitCode(err error) int {
if err == nil {
return 0
}
var exitErr interface{ ExitCode() int }
if errors.As(err, &exitErr) && exitErr.ExitCode() >= 0 {
return exitErr.ExitCode()
}
return 1
}