beads/scripts/pr-lint/main.go
ecuthiell 6899813df6
fix(preflight): share checkout lint authority (#5780)
* fix(preflight): share checkout lint authority

Move the Beads repository's native and Windows non-CGO lint policy into a checkout-owned Go driver. The supported Make/Bash wrapper and bd preflight now execute that same authority, while generic Go repositories retain their direct lint contract and formatting is rooted at the detected project.

Fixes #5300.

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell

* docs(lint): clarify shared driver scope and timeout limits

Address PR #5780 review 5191123668. Document existing inherited-tag semantics and distinguish the wrapper/preflight driver from standalone workflow and pre-commit callers.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* fix(lint): prefer the selected Windows Go toolchain (#6865)

Keep per-pass toolchain requests and deadlines while avoiding the extra auto-selection launcher under the linter. Nonstandard SDKs retain their original PATH.

Agent-Signature: codex-tui-gpt-6-astra-max on behalf of Ewen Cuthiell

* fix(lint): retain PATH for SDK directories containing separators (#6865)

Agent-Signature: codex-tui-gpt-6-astra-max on behalf of Ewen Cuthiell

* test(preflight): declare lint fixture executable for Bazel (#5300)

Agent-Signature: codex-tui-gpt-6-astra-max on behalf of Ewen Cuthiell

* fix(bazel): keep cmd/bd/testdata out of gazelle's package scan

PR #5780 adds the compilable fixture cmd/bd/testdata/preflight-go.go. Left to
itself gazelle generates a BUILD.bazel for cmd/bd/testdata, which makes it its
own package and then (a) empties cmd/bd's data = glob(["testdata/**"]) under the
default allow_empty = False and (b) puts testdata/preflight-go.go across a
package boundary from the preflight_go_fixture target that lists it.

bazel.yml's "BUILD files in sync" step runs `make bazel-sync` and fails on any
resulting drift, and fork PRs always run that job, so this was a live red whose
prescribed remedy ("commit the result") would have broken the build further.
Exclude the directory instead, and take gazelle's ordering for the data list.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(preflight): bound the lint deadline and report each check's scope

Review iteration 1 for PR #5780. Synthesis and scorecard both approve (975/1000,
no blocker or major), so this addresses non-gating findings only.

Finding 4 (Resource Lifecycle) - the 13-minute deadline could not bound the call
it guards. CombinedOutput collects through a pipe every descendant inherits, so
Wait kept blocking on a surviving golangci-lint after the context killed the
direct `go run` child. Measured on the same process shape (direct child killed at
1s, descendant holding the pipe for 8s): without WaitDelay the call returned in
8.00s, with WaitDelay in 1.50s. Sets cmd.WaitDelay so the deadline is a real
bound and the "lint check exceeded" line can actually print.

Also gates that line on err != nil, which retires the claude lane's nit (a): a
child that exits cleanly as the deadline fires is a pass, not a timeout. Measured
at the crossover, 68 of 300 passing runs would have carried a false "exceeded"
line; 0 after the gate.

Finding 1 (Change Impact) and nit 7 (Debuggability), one remedy - the PR re-roots
lint and fmt at preflightProjectRoot() but leaves runTestCheck in the caller's
cwd, so a --check run from a subdirectory mixes a repo-wide verdict with a
subtree-only one "with nothing in the output distinguishing them", and the
reported command is only reproducible from the root. CheckResult now carries the
directory each check resolved to, and the report prints it.

Took the synthesis's second arm (state the scope split) rather than re-rooting
runTestCheck, because re-rooting is a behaviour change neither lane priced: it
widens a subdirectory user's test run from 5 packages to 128, including every
Dolt-dependent package, on a repo whose full test gate the PR body already
discloses as red. The stated hazard is that the scopes are indistinguishable,
which reporting the directory discharges directly.

Finding 2 (Architectural Consistency), second clause - adds
TestBuildFlagsContractMatchesShellSource, which parses .buildflags and pins the
driver constants to it. The existing mirror test asserts today's values from the
Go side only, so the files could drift silently; buildEnvironment overrides
rather than merges -tags, so a tag added to .buildflags alone would be dropped
with every test still green. Each of the three legs was mutation-proved to red.

Declined, with the measurements that decided it:

- Finding 2's first clause (route the key comparisons through
  internal/execenv.KeyEqualForOS). The two are provably equivalent today -
  execenv's keyIdentityForWindows is strings.ToLower for the same documented
  reason - so this is de-duplication, not a defect, and the hazard it closes
  (os/exec key-identity semantics changing) is far less live than the
  .buildflags drift the second clause pins. The faithful swap means threading
  goos through five driver functions and ~27 test call sites in this PR's
  centerpiece file, and it would put a non-stdlib dependency into pr-lint_lib,
  which the binary currently does not have. Worth doing on its own footing, not
  inside a non-gating cleanup.

- Nit 6 (parse GOFLAGS into fields instead of a substring test). .buildflags:22
  guards its own append with `!= *gms_pure_go*`, a substring test, and the Go
  code is a deliberate mirror of it. Making only the Go side stricter would
  manufacture exactly the drift finding 2 warns about: on
  GOFLAGS=-tags=gms_pure_go_x the shell would skip its append while the driver
  performed one. The fix belongs to .buildflags and the driver together, and
  .buildflags is outside this PR. The new mirror test now pins that agreement.

- Nit 5 (probe golangci-lint as well as `go`). The claude lane examined the
  LookPath target change and recorded it as deliberate, with the driver's own
  missing-linter error as the mitigation. The suggested alternative - have the
  driver emit preflight's `--skip-lint` hint - puts a bd-specific flag into a
  driver that CI also invokes, where that advice is wrong.

- Finding 3 (two false PR-body claims). Correcting the body is outward-facing
  and belongs to finalize; stamped for it. Swept the in-tree copies: the PR's own
  LINTING.md sentence "Toolchain requests, formatting, and lint deadlines are
  unchanged" is scoped to the Windows SDK-preference paragraph it sits in and is
  true there, so it is graded rather than repaired. CHANGELOG.md contains no
  pr-lint entry, so the remedy's CHANGELOG half has no target in this PR.

Validation: go build, go vet and gofmt clean on the changed packages;
./scripts/pr-lint/ 12 pass / 2 Windows-only skip; ./scripts/prlintmake/ pass;
cmd/bd preflight selector pass; scripts/check-doc-freshness.sh PASSED;
make bazel-sync idempotent and bazel test //scripts/pr-lint:pr-lint_test PASSED.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(prlintmake): inject the pr-lint wrapper's gofmt shim through GOFMT

The merge with #6294 (be-gx8) left runPRLintWrapper relying on a
code path #6294 made deliberately loud. The helper put its gofmt shim
on PATH. Since #6294, fmt-check.sh resolves gofmt through
scripts/ci/gofmt-bin.sh, which ignores a PATH gofmt by design.

Both TestPRLintWrapper* tests still passed, but only via the
resolver's fallback. The go shim answers `go env GOVERSION` and
`go env GOROOT` with nothing, so gofmt-bin.sh warns "could not resolve
a gofmt from go.mod's go1.26.7 toolchain" and falls back to whatever
gofmt is first on PATH, which happened to be the shim. With GOFMT set
in the environment, which is #6294's documented override, the
resolver uses that value instead. Both tests then fail: exit status
127 from fmt-check.sh, and a missing aggregate failure diagnostic
because pr-lint.sh stops before `go run`. At the pre-merge head
(7f37ba5165) the same ambient GOFMT=/nonexistent run passed all 5
tests.

Pass the shim through GOFMT, as main's runFmtCheck does, and keep it
off PATH so only the go shim is found there. gofmt-bin.sh now returns
the override immediately, and the go shim sees only `version <shim>`
and `run ... ./scripts/pr-lint`. Both wrapper assertions, on the
driver arguments and on the failure diagnostic, are unchanged.

go test ./scripts/prlintmake/: 9/9 pass by default and 9/9 pass with
GOFMT=/nonexistent in the environment. Before this change the second
run was 7/9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-30 22:41:55 -07:00

374 lines
11 KiB
Go

// pr-lint runs the repository's canonical Go lint passes without requiring
// Make or Bash. The supported scripts/ci/pr-lint.sh entrypoint delegates here,
// and bd preflight runs this checkout-owned command for the Beads source tree.
// The standalone workflow lint action and pre-commit hook do not call this
// driver; their scope and behavior still differ.
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
const (
beadsBuildTags = "gms_pure_go"
goEnvTimeout = 30 * time.Second
lintProcessTimeout = 6 * time.Minute
lintReportedTimeout = "5m"
)
type commandSpec struct {
name string
args []string
dir string
env []string
}
type processRunner interface {
lookPath(string) (string, error)
output(context.Context, commandSpec) ([]byte, []byte, 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) output(ctx context.Context, spec commandSpec) ([]byte, []byte, error) {
cmd := exec.CommandContext(ctx, spec.name, spec.args...)
cmd.Dir = spec.dir
cmd.Env = spec.env
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.Bytes(), stderr.Bytes(), err
}
func (osProcessRunner) run(ctx context.Context, spec commandSpec, stdout, stderr io.Writer) error {
cmd := exec.CommandContext(ctx, spec.name, spec.args...) //nolint:gosec // G702: executable is PATH-resolved; argv positions are assembled internally, and the env merge-base remains one unshelled argv value.
cmd.Dir = spec.dir
cmd.Env = spec.env
cmd.Stdout = stdout
cmd.Stderr = stderr
return cmd.Run()
}
type nativeGoEnvironment struct {
GOOS string `json:"GOOS"`
CGOEnabled string `json:"CGO_ENABLED"`
GOROOT string `json:"GOROOT"`
GOVERSION string `json:"GOVERSION"`
}
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
}
goPath, err := runner.lookPath("go")
if err != nil {
fmt.Fprintf(stderr, "Go toolchain not found in PATH: %v\n", err)
return 1
}
lintPath, err := runner.lookPath("golangci-lint")
if err != nil {
fmt.Fprintf(stderr, "golangci-lint not found in PATH: %v\n", err)
return 1
}
effectiveEnv := buildEnvironment(environ, runtime.GOOS == "windows")
probeCtx, cancel := context.WithTimeout(context.Background(), goEnvTimeout)
defer cancel()
native, code := readNativeGoEnvironment(probeCtx, dir, effectiveEnv, goPath, stderr, runner)
if code != 0 {
return code
}
mergeBase, _ := environmentValue(effectiveEnv, "BD_LINT_NEW_FROM_MERGE_BASE", runtime.GOOS == "windows")
argsForLint := lintArgs(mergeBase)
skipWindows := native.GOOS == "windows" && native.CGOEnabled == "0"
windowsEnv := setEnvironment(effectiveEnv, map[string]string{
"CGO_ENABLED": "0",
"GOARCH": "amd64",
"GOOS": "windows",
"GOWORK": "off",
}, runtime.GOOS == "windows")
// Files guarded by //go:build darwin (and the !windows && !linux fallbacks)
// are invisible to the Linux runner too, so a darwin-only finding must fail
// the PR here instead of on the next maintainer's laptop.
skipDarwin := native.GOOS == "darwin" && native.CGOEnabled == "0"
darwinEnv := setEnvironment(effectiveEnv, map[string]string{
"CGO_ENABLED": "0",
"GOARCH": "arm64",
"GOOS": "darwin",
"GOWORK": "off",
}, runtime.GOOS == "windows")
nativeEnv := effectiveEnv
if runtime.GOOS == "windows" {
nativeEnv = preferSelectedGo(probeCtx, dir, effectiveEnv, native, stderr, runner)
if !skipWindows {
// GOWORK=off can select a different toolchain. Probe the original
// Go executable and PATH again, before either lint pass starts.
selected, code := readNativeGoEnvironment(probeCtx, dir, windowsEnv, goPath, stderr, runner)
if code == 0 {
windowsEnv = preferSelectedGo(probeCtx, dir, windowsEnv, selected, stderr, runner)
}
}
if !skipDarwin {
selected, code := readNativeGoEnvironment(probeCtx, dir, darwinEnv, goPath, stderr, runner)
if code == 0 {
darwinEnv = preferSelectedGo(probeCtx, dir, darwinEnv, selected, stderr, runner)
}
}
}
cancel() // All discovery shares the existing 30-second probe budget.
if code := runLintPass(
"golangci-lint (native)",
commandSpec{name: lintPath, args: argsForLint, dir: dir, env: nativeEnv},
stdout,
stderr,
runner,
); code != 0 {
return code
}
if skipWindows {
fmt.Fprintln(stdout, "==> golangci-lint (windows/amd64, non-CGO) already covered by native pass")
} else if code := runLintPass(
"golangci-lint (windows/amd64, non-CGO)",
commandSpec{name: lintPath, args: argsForLint, dir: dir, env: windowsEnv},
stdout,
stderr,
runner,
); code != 0 {
return code
}
if skipDarwin {
fmt.Fprintln(stdout, "==> golangci-lint (darwin/arm64, non-CGO) already covered by native pass")
return 0
}
return runLintPass(
"golangci-lint (darwin/arm64, non-CGO)",
commandSpec{name: lintPath, args: argsForLint, dir: dir, env: darwinEnv},
stdout,
stderr,
runner,
)
}
func readNativeGoEnvironment(
ctx context.Context,
dir string,
environ []string,
goPath string,
stderr io.Writer,
runner processRunner,
) (nativeGoEnvironment, int) {
spec := commandSpec{
name: goPath,
args: []string{"env", "-json", "GOOS", "CGO_ENABLED", "GOROOT", "GOVERSION"},
dir: dir,
env: environ,
}
output, commandStderr, err := runner.output(ctx, spec)
writeDiagnostic(stderr, commandStderr)
if err != nil {
writeDiagnostic(stderr, output)
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
fmt.Fprintf(stderr, "go env exceeded %s\n", goEnvTimeout)
return nativeGoEnvironment{}, 1
}
fmt.Fprintf(stderr, "inspect native Go target: %v\n", err)
return nativeGoEnvironment{}, processExitCode(err)
}
var native nativeGoEnvironment
if err := json.Unmarshal(output, &native); err != nil {
fmt.Fprintf(stderr, "parse native Go target: %v\n", err)
return nativeGoEnvironment{}, 1
}
if native.GOOS == "" || native.CGOEnabled == "" {
fmt.Fprintf(stderr, "go env returned an incomplete target: GOOS=%q CGO_ENABLED=%q\n", native.GOOS, native.CGOEnabled)
return nativeGoEnvironment{}, 1
}
return native, 0
}
// On Windows, Go auto-selection starts another process instead of replacing
// itself. Prefer the selected SDK for lint so cancellation reaches that Go
// process directly. This is not general descendant-process containment.
func preferSelectedGo(ctx context.Context, dir string, environ []string, selected nativeGoEnvironment, stderr io.Writer, runner processRunner) []string {
if !filepath.IsAbs(selected.GOROOT) || selected.GOVERSION == "" {
return environ
}
bin := filepath.Join(selected.GOROOT, "bin")
if strings.ContainsRune(bin, os.PathListSeparator) {
fmt.Fprintln(stderr, "Windows lint: retaining original Go PATH; selected SDK directory contains a PATH separator")
return environ
}
// GOROOT can describe a source-only or custom installation. Verify the
// candidate without allowing it to switch toolchains during this probe.
output, diagnostic, err := runner.output(ctx, commandSpec{
name: filepath.Join(bin, "go.exe"),
args: []string{"env", "-json", "GOROOT", "GOVERSION"},
dir: dir,
env: setEnvironment(environ, map[string]string{"GOTOOLCHAIN": "local"}, true),
})
writeDiagnostic(stderr, diagnostic)
var candidate nativeGoEnvironment
if err != nil || json.Unmarshal(output, &candidate) != nil ||
candidate.GOVERSION != selected.GOVERSION ||
!strings.EqualFold(filepath.Clean(candidate.GOROOT), filepath.Clean(selected.GOROOT)) {
fmt.Fprintln(stderr, "Windows lint: retaining original Go PATH; selected SDK could not be verified")
return environ
}
path, _ := environmentValue(environ, "PATH", true)
return setEnvironment(environ, map[string]string{"PATH": bin + string(os.PathListSeparator) + path}, true)
}
func writeDiagnostic(destination io.Writer, output []byte) {
if len(output) == 0 {
return
}
_, _ = destination.Write(output)
if output[len(output)-1] != '\n' {
fmt.Fprintln(destination)
}
}
func runLintPass(label string, spec commandSpec, stdout, stderr io.Writer, runner processRunner) int {
fmt.Fprintf(stdout, "==> %s\n", label)
ctx, cancel := context.WithTimeout(context.Background(), lintProcessTimeout)
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, lintProcessTimeout)
return 1
}
fmt.Fprintf(stderr, "<== %s failed: %v\n", label, err)
return processExitCode(err)
}
func lintArgs(mergeBase string) []string {
args := []string{
"run",
"--config=.golangci.yml",
"--modules-download-mode=readonly",
"--timeout=" + lintReportedTimeout,
"--build-tags=" + beadsBuildTags,
}
if mergeBase != "" {
args = append(args, "--new-from-merge-base="+mergeBase)
}
return append(args, "./...")
}
func buildEnvironment(environ []string, caseInsensitive bool) []string {
cgoEnabled, found := environmentValue(environ, "CGO_ENABLED", caseInsensitive)
if !found || cgoEnabled == "" {
cgoEnabled = "1"
}
goFlags, _ := environmentValue(environ, "GOFLAGS", caseInsensitive)
// Match .buildflags: an appended -tags value overrides an inherited bare-Go
// -tags value rather than merging tag lists.
if !strings.Contains(goFlags, beadsBuildTags) {
if goFlags != "" {
goFlags += " "
}
goFlags += "-tags=" + beadsBuildTags
}
return setEnvironment(environ, map[string]string{
"BEADS_BUILD_TAGS": beadsBuildTags,
"CGO_ENABLED": cgoEnabled,
"GOFLAGS": goFlags,
}, caseInsensitive)
}
func setEnvironment(environ []string, overrides map[string]string, caseInsensitive bool) []string {
result := make([]string, 0, len(environ)+len(overrides))
for _, entry := range environ {
key, _, ok := strings.Cut(entry, "=")
if !ok {
result = append(result, entry)
continue
}
if containsEnvironmentKey(overrides, key, caseInsensitive) {
continue
}
result = append(result, entry)
}
for key, value := range overrides {
result = append(result, key+"="+value)
}
return result
}
func environmentValue(environ []string, wanted string, caseInsensitive bool) (string, bool) {
for index := len(environ) - 1; index >= 0; index-- {
key, value, ok := strings.Cut(environ[index], "=")
if ok && environmentKeysEqual(key, wanted, caseInsensitive) {
return value, true
}
}
return "", false
}
func containsEnvironmentKey(values map[string]string, wanted string, caseInsensitive bool) bool {
for key := range values {
if environmentKeysEqual(key, wanted, caseInsensitive) {
return true
}
}
return false
}
func environmentKeysEqual(left, right string, caseInsensitive bool) bool {
if caseInsensitive {
return strings.ToLower(left) == strings.ToLower(right)
}
return left == right
}
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
}