beads/engdocs/LINTING.md
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

4.4 KiB

Linting Policy

Last reviewed: 2026-09-13

Freshness source: .golangci.yml, scripts/ci/pr-lint.sh, scripts/pr-lint/, Makefile, .github/workflows/pr.yml, and .github/workflows/main.yml.

This document explains the required Go lint gate for this codebase.

Current Status

Lint is a required CI gate, and it runs in TWO LANES over the same pinned golangci-lint v2.10.1 release and the same .golangci.yml. Each must pass with zero issues in its own scope.

  • The PR lane reports only what the PR introduces. Both of its jobs are scoped to the diff against the merge base with main: the lint job passes only-new-issues, and pr-lint-wrapper runs the repository-owned ci-pr-lint wrapper with BD_LINT_NEW_FROM_MERGE_BASE. A finding in code the PR did not touch does not block it.
  • The main lane sweeps the whole tree. Both jobs run unscoped on every push to main, so a finding that lands there reds main's own run rather than every open PR.

Run the wrapper locally with:

make ci-pr-lint

That is the MAIN lane's contract, not the PR lane's: with BD_LINT_NEW_FROM_MERGE_BASE unset it sweeps the whole tree, so it is deliberately STRICTER than the gate your PR has to clear and may report findings you are not required to fix. Fix the ones that are yours; the PR gate is the authority on what blocks a merge, and main's own run is where the rest is answered.

The wrapper runs:

  • the shared scripts/ci/fmt-check.sh formatting check; and
  • the checkout-owned scripts/pr-lint Go driver, which runs golangci-lint with .golangci.yml, readonly module downloads, a five-minute timeout, the gms_pure_go build tag, and --new-from-merge-base when BD_LINT_NEW_FROM_MERGE_BASE names a ref, then runs the same scope for the non-CGO Windows target and the non-CGO darwin (arm64) target when the native host does not already cover them, so //go:build windows && !cgo and //go:build darwin files are linted from the Linux runner.

The shell wrapper records one aggregate timing for the Go lint driver. The driver prints a heading and result for each native or cross-target pass so failures remain attributable without duplicating target-selection policy in Bash.

The shared driver owns the lint passes used by scripts/ci/pr-lint.sh and Beads-source bd preflight. The standalone workflow lint action and pre-commit hook do not call it. The hook uses changed-lines scope, adds --fix, and omits the Windows cross-lint pass. Workflow consolidation remains tracked in #5629.

The driver matches .buildflags when preparing GOFLAGS: appending -tags=gms_pure_go can override an inherited bare-Go tags value; it does not merge tag lists. Each lint process has a six-minute context and a five-minute golangci-lint timeout. Cancelling preflight's outer go run command does not guarantee immediate descendant cleanup.

On Windows, the driver checks each pass's selected Go SDK and prefers its bin directory only in that linter child's PATH. This avoids an extra Go auto-selection process surviving cancellation. Discovery shares the existing 30-second probe budget; custom SDKs that cannot be verified retain the original PATH. Toolchain requests, formatting, and lint deadlines are unchanged. This does not guarantee cleanup of compiler descendants or fix slow package loading.

Policy

Treat new lint findings as defects to fix before merge. Do not add a tolerated failing baseline, and do not configure CI with --issues-exit-code=0.

When a linter reports an intentional or false-positive pattern:

  • Prefer a narrow .golangci.yml exclusion tied to a path, linter, and message.
  • Use //nolint:<linter> only when the reason is local to a specific line and the comment explains why the warning is not actionable.
  • Keep broad linter disables as a last resort.

The current configuration already encodes accepted exclusions for intentional patterns such as deferred cleanup errors, controlled subprocess execution, test-fixture file reads, and documented security false positives.

CI Cleanup Decision

pr-lint stays separate from pr-policy and pr-core so failures are easy to identify and rerun. Its repository-owned wrapper is scripts/ci/pr-lint.sh, exposed as make ci-pr-lint; its lint policy is owned by the shell-free scripts/pr-lint Go driver.

See CI_CLEANUP_PLAN.md for the full CI tier policy.

Future Work

  • Periodically audit .golangci.yml exclusions and remove entries that are no longer needed.