diff --git a/.bazelrc b/.bazelrc index dcd27c345..4de47b6c1 100644 --- a/.bazelrc +++ b/.bazelrc @@ -287,6 +287,25 @@ build:js-wasm --platforms=@rules_go//go/toolchain:js_wasm build:js-wasm --use_target_platform_for_tests build:js-wasm --remote_download_outputs=toplevel +# nogo (//tools/nogo: go test's vet checks plus the golangci-lint linters +# .golangci.yml enables) runs beside every Go compile of every lane, so any +# `bazel build`/`bazel test` fails on a finding. --config=nogo asks for the +# analysis alone (the nogo_fix output group; nothing is linked), in the race +# configuration bazel.yml's unit lane (test:ci) builds, so a local run reuses +# CI's remote cache. make lint, scripts/pr-lint and the pre-commit hook use it. +build:nogo --@rules_go//go/config:race +build:nogo --keep_going +build:nogo --output_groups=nogo_fix +# The same analyzers over the files only other platforms compile +# (//go:build windows, darwin, !linux ...): scripts/pr-lint builds +# //tools/bazel:release_cross, every go_library and go_binary split-transitioned +# to rules_go's non-cgo platforms, for its windows/amd64 and darwin/arm64 +# passes (golangci-lint's former GOOS=windows/darwin legs). bazel.yml's pure +# lane builds the same target for every release platform, which validates +# nogo there too. +build:nogo-cross --keep_going +build:nogo-cross --output_groups=nogo_fix + # D2 step 3: bazel.yml's bazel-test, bazel-pure and bazel-doltserver lanes # add this in the remote modes, where (on same-repo PRs and merge groups) # they are the only pre-merge run of pr.yml's legacy PR Core, diff --git a/.githooks/pre-commit b/.githooks/pre-commit index b082d26f7..8e4a17732 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Pre-commit hook: auto-format with gofmt and lint with golangci-lint. +# Pre-commit hook: auto-format with gofmt, then lint and vet with nogo. # Installed via: git config core.hooksPath .githooks # See also: make install (sets hooksPath automatically) @@ -9,24 +9,6 @@ set -euo pipefail # the beads section at the bottom must run for non-Go commits too. staged_go_files=$(git diff --cached --name-only --diff-filter=ACM -- '*.go') if [ -n "$staged_go_files" ]; then - # Build golangci-lint from source so it is never a stale global binary, and - # pin the version to the one CI uses (.github/workflows/{pr,main}.yml). - GOLANGCI_LINT="go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1" - - # Build it with the repo's own Go, the way CI does. In CI, actions/setup-go - # installs the go.mod version and exports GOTOOLCHAIN=local. Locally - # GOTOOLCHAIN defaults to "auto", and for a pkg@version build that honours - # golangci-lint's own toolchain directive instead: it downgrades to - # go1.25.12, which then refuses this repo's newer go directive at config - # load ("the Go language version (go1.25) used to build golangci-lint is - # lower than the targeted Go version") and fails every Go commit. Pinning to - # the go directive reproduces CI's resolution on any host toolchain. - go_toolchain="go$(awk '$1 == "go" { print $2; exit }' go.mod)" - if [ "$go_toolchain" = "go" ]; then - echo >&2 "pre-commit: no go directive in go.mod; cannot pin the lint toolchain" - exit 1 - fi - # Auto-format and re-stage, through the go.mod-pinned gofmt. A bare PATH # gofmt belongs to the host toolchain, and on a host newer than the go # directive it rewrites and re-stages files into a form CI's gofmt rejects @@ -35,13 +17,13 @@ if [ -n "$staged_go_files" ]; then echo "$staged_go_files" | xargs "$gofmt_bin" -w echo "$staged_go_files" | xargs git add - # Lint changed code with auto-fix; block on remaining issues. - # CGO_ENABLED=0 avoids typecheck failures from CGO-only dependencies - # (e.g., dolthub/go-icu-regex) that require system headers not always present. - CGO_ENABLED=0 GOTOOLCHAIN="$go_toolchain" $GOLANGCI_LINT run --new-from-rev=HEAD --fix || exit 1 - - # Re-stage any auto-fixes from the linter - echo "$staged_go_files" | xargs git add + # Lint and vet: nogo (//tools/nogo) over the staged files' Bazel packages, + # the analyzers bazel.yml gates on (go test's vet checks plus the + # golangci-lint linters .golangci.yml enables). Findings fail the commit; + # nogo prints a `patch -p1` command for the ones with suggested fixes. + # Needs bazel (or bazelisk) on PATH; the windows/darwin passes run in CI + # and in `make lint`. + make lint-changed LINT_CHANGED_SCOPE=staged || exit 1 fi # --- BEGIN BEADS INTEGRATION v1.3.0 --- diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index df5f61c5e..4b74bd4a0 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -1047,7 +1047,10 @@ jobs: # a platform-gating mistake is caught here instead of at tag time # (#5662): the whole tree, cgo off, one remote `bazel build # --platforms=...` per target (the script's header has the coverage - # boundary: the cgo release builds are not compiled here). + # boundary: the cgo release builds are not compiled here). nogo + # (//tools/nogo) validates every one of those compiles, so this is also + # the lint and vet gate for files only other platforms build + # (//go:build windows, darwin, !linux ...; engdocs/LINTING.md). - name: Cross-compile every release target (--config=release-cross) run: | set -euo pipefail diff --git a/.github/workflows/ci-measurements.yml b/.github/workflows/ci-measurements.yml index 0bfdca1dd..e2278bd22 100644 --- a/.github/workflows/ci-measurements.yml +++ b/.github/workflows/ci-measurements.yml @@ -83,19 +83,12 @@ jobs: source ./.buildflags ci_time "install gotestsum" -- go install gotest.tools/gotestsum@v1.13.0 - ci_time "install golangci-lint" -- ./scripts/ci/install-golangci-lint.sh - # install-golangci-lint.sh appends to $GITHUB_PATH, which GitHub - # Actions only applies to *later steps*, not later commands in this - # same run block. Export it here too so the "pr-lint wrapper" call - # below finds golangci-lint on PATH. - export PATH="$RUNNER_TEMP/golangci-lint:$PATH" status=0 ci_time "pr-policy wrapper" -- make ci-pr-policy || status=$? # -timeout=30m keeps this in step with the lane it measures # (scripts/ci/pr-core.sh). Without it ./cmd/bd can die on `go test`'s # 10m default and the measurement reports a panic instead of a duration. ci_time "pr-core gotestsum" -- gotestsum --junitfile artifacts/pr-core-junit.xml --format testdox -- -race -short -timeout=30m -skip '^TestEmbedded' ./... || status=$? - ci_time "pr-lint wrapper" -- make ci-pr-lint || status=$? exit "$status" - name: Upload measurement artifacts diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cdb8c2751..8300b9502 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -455,90 +455,6 @@ jobs: - name: Run PR policy wrapper run: make ci-pr-policy - # F7b: pr.yml's pr-lint-wrapper moved to same-repo Blacksmith - # (blacksmith-4vcpu-ubuntu-2404) for same-repo PRs/merge_group, falling back - # to ubuntu-latest for forks/Dependabot. golangci-lint's own cache and this - # leg's lint GOCACHE are both scoped to the runner infrastructure (not just - # the key), so this push-lane saver needs to seed both runner selections — - # the `venue` matrix dimension crossed with the existing `target` dimension - # (6 legs on push) — the same reason windows-test-binaries-cache above seeds - # both `runner: [blacksmith, github]` legs. The ternary form (rather than a - # literal Blacksmith label in a matrix array) is required for actionlint, - # same as every other Blacksmith `runs-on` in this repo. - # F7b review fix (B2): job-level push-to-main-only guard, same rationale as - # blacksmith-go-build-cache above -- applied to the whole job (not just the - # Blacksmith leg) for simplicity, since the github venue leg is also only - # meant to run from main.yml's push trigger. Pinned by - # TestBlacksmithSaverJobsGuardedAgainstPullRequest. - pr-lint-wrapper: - name: PR Lint (${{ matrix.target }}) - runs-on: ${{ matrix.venue == 'blacksmith' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'gastownhall/beads' - timeout-minutes: 20 - strategy: - fail-fast: false - matrix: - target: [native, windows, darwin] - venue: [blacksmith, github] - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Restore Go module cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - - name: Install golangci-lint - run: ./scripts/ci/install-golangci-lint.sh - - # This push lane is the only saver (§0, F5.2): keyed on file content and - # the leg; a miss on the exact content key falls back to the leg's most - # recent entry, which golangci-lint revalidates against the current - # config and inputs. pr.yml only ever restores. The key omits the sha - # (SF-4) so a save only happens when .golangci.yml or go.sum change, - # not on every push to main. - - name: Restore golangci-lint cache - id: restore-golangci-lint-cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - ~/.cache/golangci-lint - ${{ matrix.target == 'native' && format('{0}/go-cache/lint-{1}', runner.temp, matrix.target) || '' }} - key: beads-golangci-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-v2.10.1-${{ matrix.target }}-${{ hashFiles('.golangci.yml', 'go.sum') }} - restore-keys: beads-golangci-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-v2.10.1-${{ matrix.target }}- - - # No BD_LINT_NEW_FROM_MERGE_BASE here: the push lane sweeps the whole - # tree, including the GOOS=windows and GOOS=darwin cross-lints. pr.yml - # scopes the same wrapper to the PR's own diff. - - name: Run PR lint wrapper - env: - BD_LINT_TARGETS: ${{ matrix.target }} - GOCACHE: ${{ runner.temp }}/go-cache/lint-${{ matrix.target }} - run: make ci-pr-lint - - # !cancelled() (not just a cache-hit check): golangci-lint's own cache is - # content-addressed and GOCACHE is unaffected by lint findings, so it is - # safe -- and worth keeping -- to save even when the lint step itself - # fails (SF-1). Without this, a red lint run on main never warms the - # cache and every PR leg stays cold. - - name: Save golangci-lint cache - if: ${{ !cancelled() && steps.restore-golangci-lint-cache.outputs.cache-hit != 'true' }} - uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - ~/.cache/golangci-lint - ${{ matrix.target == 'native' && format('{0}/go-cache/lint-{1}', runner.temp, matrix.target) || '' }} - key: beads-golangci-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-v2.10.1-${{ matrix.target }}-${{ hashFiles('.golangci.yml', 'go.sum') }} - # macOS full -race -short suite. Linux runs the same suite under Bazel # (bazel.yml's `test` lane, on every push to main, executed on rbe-west), # and Windows uses smoke tests below; rbe-west has no macOS workers, so this @@ -642,12 +558,11 @@ jobs: # runners cannot see caches `actions/cache` saved from a GitHub-hosted # runner (and vice versa) even for an identical key, so the `venue` matrix # dimension here seeds the Blacksmith selection too -- same reasoning as - # pr-lint-wrapper/go-vet-cache above and windows-test-binaries-cache's - # `runner` matrix. + # windows-test-binaries-cache's `runner` matrix. test-windows: name: Test (Windows - smoke, ${{ matrix.venue }}) # F7b review fix (B2): added the repository check to match - # blacksmith-go-build-cache/pr-lint-wrapper/go-vet-cache's convention + # blacksmith-go-build-cache's convention # above (this job's push-to-main guard predates F7b and already had the # event/ref check; the repository check closes the same fork gap F7c's # own blacksmith-setup-go-cache guard closes). Pinned by @@ -766,79 +681,6 @@ jobs: - name: Check gofmt run: make fmt-check - # Warms the vet cache pr.yml's scripts-go-checks vet leg restores (F5.3). - # `go vet` caches successful vet actions, so a warm cache re-vets only - # changed packages and their dependents. Restore-only in pr.yml (§0); this - # push job is the only saver, on an exact-key miss. - # F7b: pr.yml's scripts-go-checks vet leg moved to same-repo Blacksmith; this - # seed needs both runner selections for the same reason pr-lint-wrapper - # above does (Blacksmith and GitHub-hosted runners cannot see each other's - # actions/cache entries, even for an identical key). - # F7b review fix (B2): job-level push-to-main-only guard, same rationale and - # convention as pr-lint-wrapper above. Pinned by - # TestBlacksmithSaverJobsGuardedAgainstPullRequest. - go-vet-cache: - name: Warm go vet cache - runs-on: ${{ matrix.venue == 'blacksmith' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - if: github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository == 'gastownhall/beads' - timeout-minutes: 20 - strategy: - fail-fast: false - matrix: - venue: [blacksmith, github] - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - # F7b review fix (S7/Speed): see blacksmith-go-build-cache's identical - # step above for the full rationale -- bounds this saver's writes to at - # most once per UTC calendar day per go.sum, not once per push. - - name: Compute cache date - id: cache-date - run: echo "today=$(date -u +%Y%m%d)" >> "$GITHUB_OUTPUT" - - - name: Restore Go module cache - id: restore-go-module-cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - - name: Restore vet Go build cache - id: restore-vet-go-build-cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/vet - key: beads-go-vet-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-vet-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} - # The non-race build cache fallback this used to list can never hit: - # actions/cache versions an entry by its path list, and that cache - # saves to a different path (.../go-cache/non-race), so the prefix - # match is always filtered out (SF-5). - restore-keys: beads-go-vet-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-vet- - - - name: Go vet with go test's checks - env: - GOCACHE: ${{ runner.temp }}/go-cache/vet - run: bash scripts/ci/go-test-vet.sh - - # build-artifacts is the module cache's sole saver (same key); this job - # only restores it to warm the vet run, and only saves its own cache. - # !cancelled(): go vet's cache is content-addressed, so it is safe to - # save even when the vet step itself fails (SF-1). - - name: Save vet Go build cache - if: ${{ !cancelled() && steps.restore-vet-go-build-cache.outputs.cache-hit != 'true' }} - uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/vet - key: beads-go-vet-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-vet-${{ hashFiles('go.sum') }}-${{ steps.cache-date.outputs.today }} - # F7b: the saver for the race and non-race GOCACHEs that same-repo-PR # (Blacksmith, blacksmith-4vcpu-ubuntu-2404) jobs restore: the non-race one # for pr-preflight-platforms' and check-doc-freshness-platforms' ubuntu @@ -965,7 +807,7 @@ jobs: # for same-repo PRs/merge_group and on macos-latest for fork/Dependabot PRs, # and Blacksmith and GitHub-hosted runners cannot see each other's # actions/cache entries, so the `venue` matrix seeds both (same convention as - # pr-lint-wrapper/go-vet-cache/test-windows). This job is their only seeder + # test-windows). This job is their only seeder # (and the Blacksmith leg also seeds the `test` job's module and non-race # caches), and runs only the shared warm-up those legs compile # (warm-non-race-cache.sh, the same script the Linux/Windows seeders use) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 72eb4637b..c59e727a6 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -1075,8 +1075,8 @@ jobs: # D2 step 3: stands down where bazel-coverage says pr_lanes; Bazel / # test (`bazel test //... --config=ci`, the same selection, checked by its - # equivalence step) runs it, and scripts-go-checks runs what Bazel does not - # (go test's vet). + # equivalence step) runs it, and nogo (//tools/nogo) runs go test's vet + # checks beside every compile of that lane. pr-core-wrapper: name: PR Core (wrapper timing) runs-on: ubuntu-latest @@ -1136,122 +1136,6 @@ jobs: GOCACHE: ${{ runner.temp }}/go-cache/race run: make ci-pr-core - # PR Core's duty the Bazel lanes do not take over (D2 step 3), on every PR - # whatever bazel-coverage says: go test's own vet checks over ./... - # (rules_go's go_test runs none). The repository policy tests in - # ./scripts/... and the source-walking tests run under Bazel - # (//scripts:scripts_test, //test/docsync over //:repo_files), remotely. - # F7b: same-repo Blacksmith (blacksmith-4vcpu-ubuntu-2404) for same-repo - # PRs/merge_group, falling back to ubuntu-latest for forks/Dependabot, same - # pattern as advisory-reports/pr-core-wrapper above. - # main.yml's go-vet-cache venue matrix seeds the vet leg's GOCACHE. - scripts-go-checks: - name: Go checks (${{ matrix.check }}) - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - timeout-minutes: 30 - strategy: - fail-fast: false - matrix: - check: [vet] - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Restore Go module cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - - name: Restore vet Go build cache - if: matrix.check == 'vet' - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/go-cache/vet - key: beads-go-vet-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-vet-${{ github.sha }} - # The non-race build cache fallback this used to list can never hit: - # actions/cache versions an entry by its path list, and that cache - # saves to a different path (.../go-cache/non-race), so the prefix - # match is always filtered out (SF-5). - restore-keys: beads-go-vet-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-vet- - - - name: Go vet with go test's checks - if: ${{ !cancelled() && steps.setup-go.outcome == 'success' && matrix.check == 'vet' }} - env: - GOCACHE: ${{ runner.temp }}/go-cache/vet - run: bash scripts/ci/go-test-vet.sh - - # F7b: same-repo Blacksmith (blacksmith-4vcpu-ubuntu-2404) for same-repo - # PRs/merge_group, falling back to ubuntu-latest for forks/Dependabot. - # main.yml's pr-lint-wrapper `venue` matrix seeds this leg's golangci-lint - # and lint-native GOCACHEs. - pr-lint-wrapper: - name: PR Lint (${{ matrix.target }}) - runs-on: ${{ (github.event_name == 'merge_group' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]')) && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }} - timeout-minutes: 20 - permissions: - contents: read - strategy: - fail-fast: false - matrix: - target: [native, windows, darwin] - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - with: - # --new-from-merge-base below walks real history. - fetch-depth: 0 - - - name: Set up Go - id: setup-go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - - name: Restore Go module cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ~/go/pkg/mod - key: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('go.mod', 'go.sum') }} - restore-keys: beads-go-mod-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}- - - - name: Install golangci-lint - run: ./scripts/ci/install-golangci-lint.sh - - # Keyed on file content and the leg; a miss on the exact content key - # falls back to the leg's most recent entry, which golangci-lint - # revalidates against the current config and inputs. pr.yml never saves - # this cache (§0); main.yml's pr-lint-wrapper matrix is the only saver, - # on an exact-key miss. The key omits the sha (SF-4) so main.yml only - # saves when .golangci.yml or go.sum change. - - name: Restore golangci-lint cache - uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: | - ~/.cache/golangci-lint - ${{ matrix.target == 'native' && format('{0}/go-cache/lint-{1}', runner.temp, matrix.target) || '' }} - key: beads-golangci-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-v2.10.1-${{ matrix.target }}-${{ hashFiles('.golangci.yml', 'go.sum') }} - restore-keys: beads-golangci-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-v2.10.1-${{ matrix.target }}- - - # Scope the wrapper's lint to what this PR introduces, matching the - # (now-deleted) `lint` job's only-new-issues scope. main.yml runs the - # same wrapper with this unset, so the whole tree is still swept on - # every push to main. Both PR triggers (pull_request, merge_group) - # target main. - - name: Run PR lint wrapper - env: - BD_LINT_NEW_FROM_MERGE_BASE: origin/main - BD_LINT_TARGETS: ${{ matrix.target }} - GOCACHE: ${{ runner.temp }}/go-cache/lint-${{ matrix.target }} - run: make ci-pr-lint - # F7a: fold of build-examples + complexity-report, the two pr.yml jobs that # are advisory-only (not in ci-gate's needs/CI_GATE_REQUIRED) and restore no # GitHub-saved build cache, onto one Blacksmith runner for same-repo @@ -2089,8 +1973,6 @@ jobs: - check-doc-freshness-platforms - pr-preflight-platforms - pr-core-wrapper - - scripts-go-checks - - pr-lint-wrapper - windows-make-shell - bazel - bazel-coverage @@ -2113,8 +1995,6 @@ jobs: PACKAGE_MCP PACKAGE_NPM PR_CORE_WRAPPER - SCRIPTS_GO_CHECKS - PR_LINT_WRAPPER WINDOWS_MAKE_SHELL BAZEL BAZEL_TEST @@ -2162,8 +2042,6 @@ jobs: PACKAGE_MCP: ${{ needs.bazel.outputs.package-mcp || 'skipped' }} PACKAGE_NPM: ${{ needs.bazel.outputs.package-npm || 'skipped' }} PR_CORE_WRAPPER: ${{ needs.pr-core-wrapper.result }} - SCRIPTS_GO_CHECKS: ${{ needs.scripts-go-checks.result }} - PR_LINT_WRAPPER: ${{ needs.pr-lint-wrapper.result }} WINDOWS_MAKE_SHELL: ${{ needs.windows-make-shell.result }} # bazel.yml's lanes report their job.status as outputs; a lane that # did not report (skipped, or lost before its last step) reads as diff --git a/.golangci.yml b/.golangci.yml index 2298aebf6..5af1120fe 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,3 +1,9 @@ +# Lint policy for nogo (//tools/nogo), which runs these linters beside every +# Bazel Go compile: tools/nogo/internal/golangci embeds this file and applies +# it as golangci-lint v2.10.1 would (enabled linters, settings, run.tests, +# generated files, exclusion rules). Decoding is strict, so a key the nogo +# wrappers do not implement fails the build. golangci-lint itself still reads +# it (editors, ad-hoc runs), but CI gates on nogo; see engdocs/LINTING.md. version: "2" run: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fc8edcbea..fa008964c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,9 +1,15 @@ repos: - - repo: https://github.com/golangci/golangci-lint - rev: v2.10.1 + # Lint and vet: nogo (//tools/nogo) over the changed files' Bazel packages, + # the analyzers CI gates on (go test's vet checks plus the golangci-lint + # linters .golangci.yml enables). Needs bazel or bazelisk on PATH. + - repo: local hooks: - - id: golangci-lint - args: [--timeout=5m] + - id: nogo + name: nogo (bazel) + entry: make lint-changed LINT_CHANGED_SCOPE=staged + language: system + types: [go] + pass_filenames: false - repo: https://github.com/pre-commit/pre-commit-hooks rev: v6.0.0 diff --git a/AGENTS.md b/AGENTS.md index 5a0eabfff..b16a6db78 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -105,7 +105,7 @@ spans layers. See ```bash make install # build and install bd to ~/.local/bin (canonical) make test # unit tests with the right build tags -make ci-pr-lint # required zero-finding formatting + lint contract +make ci-pr-lint # required zero-finding lint + vet contract (nogo under Bazel) make check-docs # doc flag, freshness, and docsync checks make bazel-sync # after adding/removing/renaming Go files or changing imports/go.mod ``` diff --git a/BUILD.bazel b/BUILD.bazel index 1cb1bcf75..339780621 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -22,7 +22,6 @@ gazelle(name = "gazelle") exports_files( [".buildflags"], visibility = [ - "//scripts/pr-lint:__pkg__", "//scripts/prlintmake:__pkg__", "//tests/upgrade_smoke:__pkg__", ], @@ -33,6 +32,15 @@ exports_files([ "go.sum", ]) +# The golangci-lint configuration nogo applies (//tools/nogo), and its test. +exports_files( + [".golangci.yml"], + visibility = [ + "//tools/nogo:__pkg__", + "//tools/nogo/internal/golangci:__pkg__", + ], +) + # Repository files read by source-scanning tests, declared as their `data` so # the tests run under `bazel test` and remote execution. exports_files( @@ -382,6 +390,18 @@ filegroup( "//third_party/patches:repo_files", "//tools/bazel:repo_files", "//tools/docsmint:repo_files", + "//tools/nogo:repo_files", + "//tools/nogo/analyzers/depguard:repo_files", + "//tools/nogo/analyzers/errcheck:repo_files", + "//tools/nogo/analyzers/forbidigo:repo_files", + "//tools/nogo/analyzers/gosec:repo_files", + "//tools/nogo/analyzers/misspell:repo_files", + "//tools/nogo/analyzers/sloglint:repo_files", + "//tools/nogo/analyzers/unconvert:repo_files", + "//tools/nogo/analyzers/unparam:repo_files", + "//tools/nogo/internal/analyzertest:repo_files", + "//tools/nogo/internal/golangci:repo_files", + "//tools/nogo/internal/position:repo_files", ], visibility = [ "//scripts:__pkg__", diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3fa5b2b03..214bfc13c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -56,7 +56,8 @@ area is reviewed first. - Go (see `go.mod` for the required version; currently 1.26+) - Git - A C compiler (CGO is required for the embedded Dolt database) -- golangci-lint v2.10.1 for the required local lint gate +- [Bazelisk](https://github.com/bazelbuild/bazelisk) (installed as `bazel`) for + the required lint and vet gate (`make ci-pr-lint`) and the pre-commit hook - ICU headers are **not required** for building -- see [engdocs/ICU-POLICY.md](engdocs/ICU-POLICY.md) ### Getting Started @@ -91,7 +92,7 @@ beads/ │ ├── types/ # Core data types (Issue, Dependency, etc.) │ └── storage/ # Storage interface and implementations │ └── dolt/ # Dolt database backend -├── .golangci.yml # Linter configuration +├── .golangci.yml # Linter configuration (applied by nogo, tools/nogo) └── .github/workflows/ # CI/CD pipelines ``` @@ -114,23 +115,24 @@ We follow standard Go conventions: ### Linting -Use the same pinned golangci-lint version and repository-owned wrapper as CI: +Lint and vet run as nogo under Bazel: go test's vet checks plus the +golangci-lint linters `.golangci.yml` enables, the same analyzers CI gates on. ```bash -# Install the version pinned by CI -go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1 - -# Run the required formatting and lint contract +# Run the required lint and vet gate (native, windows and darwin) make ci-pr-lint + +# Faster: only the Bazel packages of your changed Go files +make lint-changed ``` -`make ci-pr-lint` must pass with zero issues. It checks formatting, lints the -repository's normal `gms_pure_go` build, and cross-lints Windows-only non-CGO -code. Accepted intentional patterns are encoded narrowly in `.golangci.yml`; -do not ignore a failing baseline. See [engdocs/LINTING.md](engdocs/LINTING.md) -for the full policy. +`make ci-pr-lint` must pass with zero issues. It analyzes the repository's +normal `gms_pure_go` build and cross-checks the Windows- and macOS-only +non-cgo code. Accepted intentional patterns are encoded narrowly in +`.golangci.yml`; do not ignore a failing baseline. See +[engdocs/LINTING.md](engdocs/LINTING.md) for the full policy. -CI runs the same required wrapper on all pull requests. +CI runs the same analyzers on all pull requests, in the Bazel test lane. ## Making Changes diff --git a/MODULE.bazel b/MODULE.bazel index f1e05a64c..9cd65e6fc 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -5,6 +5,18 @@ module( bazel_dep(name = "platforms", version = "1.1.0") bazel_dep(name = "rules_go", version = "0.63.0") + +# nogo honors //nolint directives through ast.CommentMap ranges, which differ +# from golangci-lint's (a comment trailing `func f(...) {` attaches to the +# body's first statement). This tree's directives were written against +# golangci-lint, so the patch adds golangci-lint's ranges as well. +single_version_override( + module_name = "rules_go", + patch_strip = 1, + patches = ["//third_party/patches:rules_go_nogo_golangci_nolint.patch"], + version = "0.63.0", +) + bazel_dep(name = "gazelle", version = "0.53.0") bazel_dep(name = "rules_shell", version = "0.6.1") bazel_dep(name = "toolchains_llvm", version = "1.11.0") @@ -158,8 +170,20 @@ go_sdk.download( ) use_repo(go_sdk, "go_sdk") +# nogo: //tools/nogo's analyzers (go test's vet checks plus the golangci-lint +# linters .golangci.yml enables) run beside every first-party Go compile, so +# a finding fails `bazel build`/`bazel test` wherever it executes (RBE +# included). External repos only feed analyzer facts; they are not validated. +go_sdk.nogo( + includes = ["//:__subpackages__"], + nogo = "//tools/nogo", +) + +# gazelle.go.work joins the beads module and the nogo analyzers' module +# (tools/nogo/go.mod) into one dependency graph for Bazel only; `go build` in +# the repository root never reads it (it is not named go.work). go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") -go_deps.from_file(go_mod = "//:go.mod") +go_deps.from_file(go_work = "//:gazelle.go.work") # beads builds go-mysql-server with its stdlib-regex backend (ICU policy): # gazelle must see gms_pure_go as satisfied so it selects regex_pure.go and @@ -193,7 +217,17 @@ go_deps.module_override( patches = ["//third_party/patches:gozstd_nocgo.patch"], path = "github.com/dolthub/gozstd", ) -use_repo(go_deps, "com_github_anthropics_anthropic_sdk_go", "com_github_burntsushi_toml", "com_github_cenkalti_backoff_v4", "com_github_charmbracelet_x_ansi", "com_github_creack_pty", "com_github_data_dog_go_sqlmock", "com_github_dolthub_dolt_go", "com_github_dolthub_driver_v2", "com_github_dolthub_eventkit", "com_github_dolthub_go_mysql_server", "com_github_dolthub_vitess", "com_github_go_sql_driver_mysql", "com_github_google_uuid", "com_github_gowebpki_jcs", "com_github_invopop_jsonschema", "com_github_johanneskaufmann_html_to_markdown_v2", "com_github_mattn_go_sqlite3", "com_github_microcosm_cc_bluemonday", "com_github_oapi_codegen_oapi_codegen_v2", "com_github_olebedev_when", "com_github_spf13_cobra", "com_github_spf13_pflag", "com_github_spf13_viper", "com_github_stretchr_testify", "com_github_subosito_gotenv", "com_github_testcontainers_testcontainers_go", "com_github_testcontainers_testcontainers_go_modules_dolt", "com_github_yuin_goldmark", "in_gopkg_yaml_v3", "io_opentelemetry_go_otel", "io_opentelemetry_go_otel_exporters_otlp_otlpmetric_otlpmetrichttp", "io_opentelemetry_go_otel_exporters_stdout_stdoutmetric", "io_opentelemetry_go_otel_exporters_stdout_stdouttrace", "io_opentelemetry_go_otel_metric", "io_opentelemetry_go_otel_sdk", "io_opentelemetry_go_otel_sdk_metric", "io_opentelemetry_go_otel_trace", "io_rsc_script", "land_charm_glamour_v2", "land_charm_huh_v2", "land_charm_lipgloss_v2", "org_golang_x_mod", "org_golang_x_net", "org_golang_x_sync", "org_golang_x_sys", "org_golang_x_term") + +# nogo does not analyze the standard library, so ctrlflow has no noReturn +# facts for t.Fatal, log.Fatal, os.Exit, ...; buildssa (unparam) and the vet +# passes then treat them as returning, unlike golangci-lint and go vet. The +# patch makes ctrlflow know those standard-library functions intrinsically. +go_deps.module_override( + patch_strip = 1, + patches = ["//third_party/patches:x_tools_ctrlflow_stdlib_noreturn.patch"], + path = "golang.org/x/tools", +) +use_repo(go_deps, "cc_mvdan_unparam", "com_github_anthropics_anthropic_sdk_go", "com_github_ashanbrown_forbidigo_v2", "com_github_burntsushi_toml", "com_github_cenkalti_backoff_v4", "com_github_charmbracelet_x_ansi", "com_github_creack_pty", "com_github_data_dog_go_sqlmock", "com_github_dolthub_dolt_go", "com_github_dolthub_driver_v2", "com_github_dolthub_eventkit", "com_github_dolthub_go_mysql_server", "com_github_dolthub_vitess", "com_github_go_sql_driver_mysql", "com_github_golangci_misspell", "com_github_golangci_unconvert", "com_github_google_uuid", "com_github_gowebpki_jcs", "com_github_invopop_jsonschema", "com_github_johanneskaufmann_html_to_markdown_v2", "com_github_kisielk_errcheck", "com_github_mattn_go_sqlite3", "com_github_microcosm_cc_bluemonday", "com_github_oapi_codegen_oapi_codegen_v2", "com_github_olebedev_when", "com_github_openpeedeep_depguard_v2", "com_github_securego_gosec_v2", "com_github_spf13_cobra", "com_github_spf13_pflag", "com_github_spf13_viper", "com_github_stretchr_testify", "com_github_subosito_gotenv", "com_github_testcontainers_testcontainers_go", "com_github_testcontainers_testcontainers_go_modules_dolt", "com_github_yuin_goldmark", "in_gopkg_yaml_v3", "io_opentelemetry_go_otel", "io_opentelemetry_go_otel_exporters_otlp_otlpmetric_otlpmetrichttp", "io_opentelemetry_go_otel_exporters_stdout_stdoutmetric", "io_opentelemetry_go_otel_exporters_stdout_stdouttrace", "io_opentelemetry_go_otel_metric", "io_opentelemetry_go_otel_sdk", "io_opentelemetry_go_otel_sdk_metric", "io_opentelemetry_go_otel_trace", "io_rsc_script", "land_charm_glamour_v2", "land_charm_huh_v2", "land_charm_lipgloss_v2", "org_go_simpler_sloglint", "org_golang_x_mod", "org_golang_x_net", "org_golang_x_sync", "org_golang_x_sys", "org_golang_x_term", "org_golang_x_tools") # Hermetic Dolt CLI for tests (tools/bazel/dolt.bzl pins the release to # scripts/ci/install-dolt.sh). //tools/bazel:dolt selects the repository for diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 4e781c838..275b6bfd9 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -24,7 +24,6 @@ "https://bcr.bazel.build/modules/aspect_bazel_lib/2.19.3/MODULE.bazel": "253d739ba126f62a5767d832765b12b59e9f8d2bc88cc1572f4a73e46eb298ca", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.19.3/source.json": "ffab9254c65ba945f8369297ad97ca0dec213d3adc6e07877e23a48624a8b456", "https://bcr.bazel.build/modules/aspect_bazel_lib/2.8.1/MODULE.bazel": "812d2dd42f65dca362152101fbec418029cc8fd34cbad1a2fde905383d705838", - "https://bcr.bazel.build/modules/bazel_features/1.1.0/MODULE.bazel": "cfd42ff3b815a5f39554d97182657f8c4b9719568eb7fded2b9135f084bf760b", "https://bcr.bazel.build/modules/bazel_features/1.1.1/MODULE.bazel": "27b8c79ef57efe08efccbd9dd6ef70d61b4798320b8d3c134fd571f78963dbcd", "https://bcr.bazel.build/modules/bazel_features/1.10.0/MODULE.bazel": "f75e8807570484a99be90abcd52b5e1f390362c258bcb73106f4544957a48101", "https://bcr.bazel.build/modules/bazel_features/1.11.0/MODULE.bazel": "f9382337dd5a474c3b7d334c2f83e50b6eaedc284253334cf823044a26de03e8", @@ -65,10 +64,6 @@ "https://bcr.bazel.build/modules/buildozer/8.5.1/source.json": "e3386e6ff4529f2442800dee47ad28d3e6487f36a1f75ae39ae56c70f0cd2fbd", "https://bcr.bazel.build/modules/gawk/5.3.2.bcr.1/MODULE.bazel": "cdf8cbe5ee750db04b78878c9633cc76e80dcf4416cbe982ac3a9222f80713c8", "https://bcr.bazel.build/modules/gawk/5.3.2.bcr.1/source.json": "fa7b512dfcb5eafd90ce3959cf42a2a6fe96144ebbb4b3b3928054895f2afac2", - "https://bcr.bazel.build/modules/gazelle/0.32.0/MODULE.bazel": "b499f58a5d0d3537f3cf5b76d8ada18242f64ec474d8391247438bf04f58c7b8", - "https://bcr.bazel.build/modules/gazelle/0.33.0/MODULE.bazel": "a13a0f279b462b784fb8dd52a4074526c4a2afe70e114c7d09066097a46b3350", - "https://bcr.bazel.build/modules/gazelle/0.34.0/MODULE.bazel": "abdd8ce4d70978933209db92e436deb3a8b737859e9354fb5fd11fb5c2004c8a", - "https://bcr.bazel.build/modules/gazelle/0.36.0/MODULE.bazel": "e375d5d6e9a6ca59b0cb38b0540bc9a05b6aa926d322f2de268ad267a2ee74c0", "https://bcr.bazel.build/modules/gazelle/0.51.3/MODULE.bazel": "618a729142f66de1e2cb776d026413763be5b80d5e3d29ffb9d3d90c5defde90", "https://bcr.bazel.build/modules/gazelle/0.53.0/MODULE.bazel": "76158efbaf12e35ae17af02f4a715bc6e7b3b410c693d34f422922ff4a56a925", "https://bcr.bazel.build/modules/gazelle/0.53.0/source.json": "1f5a4dd0399e5a20e0daeaa3ba1f42391a1b909bc3d337f7a62d3216ddbdfad0", @@ -107,13 +102,11 @@ "https://bcr.bazel.build/modules/protobuf/21.7/MODULE.bazel": "a5a29bb89544f9b97edce05642fac225a808b5b7be74038ea3640fae2f8e66a7", "https://bcr.bazel.build/modules/protobuf/27.0/MODULE.bazel": "7873b60be88844a0a1d8f80b9d5d20cfbd8495a689b8763e76c6372998d3f64c", "https://bcr.bazel.build/modules/protobuf/27.1/MODULE.bazel": "703a7b614728bb06647f965264967a8ef1c39e09e8f167b3ca0bb1fd80449c0d", - "https://bcr.bazel.build/modules/protobuf/29.0-rc2.bcr.1/MODULE.bazel": "52f4126f63a2f0bbf36b99c2a87648f08467a4eaf92ba726bc7d6a500bbf770c", "https://bcr.bazel.build/modules/protobuf/29.0-rc2/MODULE.bazel": "6241d35983510143049943fc0d57937937122baf1b287862f9dc8590fc4c37df", "https://bcr.bazel.build/modules/protobuf/29.0-rc3/MODULE.bazel": "33c2dfa286578573afc55a7acaea3cada4122b9631007c594bf0729f41c8de92", "https://bcr.bazel.build/modules/protobuf/29.0/MODULE.bazel": "319dc8bf4c679ff87e71b1ccfb5a6e90a6dbc4693501d471f48662ac46d04e4e", "https://bcr.bazel.build/modules/protobuf/29.1/MODULE.bazel": "557c3457560ff49e122ed76c0bc3397a64af9574691cb8201b4e46d4ab2ecb95", "https://bcr.bazel.build/modules/protobuf/3.19.0/MODULE.bazel": "6b5fbb433f760a99a22b18b6850ed5784ef0e9928a72668b66e4d7ccd47db9b0", - "https://bcr.bazel.build/modules/protobuf/3.19.2/MODULE.bazel": "532ffe5f2186b69fdde039efe6df13ba726ff338c6bc82275ad433013fa10573", "https://bcr.bazel.build/modules/protobuf/3.19.6/MODULE.bazel": "9233edc5e1f2ee276a60de3eaa47ac4132302ef9643238f23128fea53ea12858", "https://bcr.bazel.build/modules/protobuf/32.1/MODULE.bazel": "89cd2866a9cb07fee9ff74c41ceace11554f32e0d849de4e23ac55515cfada4d", "https://bcr.bazel.build/modules/protobuf/33.4/MODULE.bazel": "114775b816b38b6d0ca620450d6b02550c60ceedfdc8d9a229833b34a223dc42", @@ -150,10 +143,6 @@ "https://bcr.bazel.build/modules/rules_cc/0.2.8/MODULE.bazel": "f1df20f0bf22c28192a794f29b501ee2018fa37a3862a1a2132ae2940a23a642", "https://bcr.bazel.build/modules/rules_foreign_cc/0.9.0/MODULE.bazel": "c9e8c682bf75b0e7c704166d79b599f93b72cfca5ad7477df596947891feeef6", "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/MODULE.bazel": "40c97d1144356f52905566c55811f13b299453a14ac7769dfba2ac38192337a8", - "https://bcr.bazel.build/modules/rules_go/0.41.0/MODULE.bazel": "55861d8e8bb0e62cbd2896f60ff303f62ffcb0eddb74ecb0e5c0cbe36fc292c8", - "https://bcr.bazel.build/modules/rules_go/0.42.0/MODULE.bazel": "8cfa875b9aa8c6fce2b2e5925e73c1388173ea3c32a0db4d2b4804b453c14270", - "https://bcr.bazel.build/modules/rules_go/0.46.0/MODULE.bazel": "3477df8bdcc49e698b9d25f734c4f3a9f5931ff34ee48a2c662be168f5f2d3fd", - "https://bcr.bazel.build/modules/rules_go/0.59.0/MODULE.bazel": "b7e43e7414a3139a7547d1b4909b29085fbe5182b6c58cbe1ed4c6272815aeae", "https://bcr.bazel.build/modules/rules_go/0.63.0/MODULE.bazel": "a2079e783a5d2715a90e6e2970ec8ffd717a487ff2aecb5f553e3975c5294a21", "https://bcr.bazel.build/modules/rules_go/0.63.0/source.json": "5bf37b45c440ac03abad0cbfa8b8b8dd89cc4e82bc8305f468b8436fa58bae78", "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", diff --git a/Makefile b/Makefile index 24b375ea2..9797f1555 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,7 @@ endif endif .PHONY: all build doctor-build test test-icu-path test-full-cgo test-regression test-upgrade test-cross-version test-migration corpus-regen githooks-regen bench bench-quick clean clean-test-tmp install install-force help check-up-to-date fmt fmt-check check-testing-short +.PHONY: lint lint-changed vet .PHONY: ci-pr-core ci-pr-policy ci-pr-lint ci-complexity ci-complexity-diff ci-complexity-check ci-package-mcp ci-package-npm .PHONY: api-gen api-check .PHONY: bazel-sync bazel-sync-check @@ -197,9 +198,49 @@ ci-pr-core: ci-pr-policy: @./scripts/ci/pr-policy.sh +# Lint and vet are nogo (//tools/nogo): go test's vet checks plus the +# golangci-lint linters .golangci.yml enables, validated beside every Go +# compile, so any `bazel build`/`bazel test` (local or on rbe-west) fails on a +# finding. These targets ask Bazel for the analysis alone (.bazelrc's +# --config=nogo; nothing is linked). ci-pr-lint: @./scripts/ci/pr-lint.sh +# The CI lint gate: native, plus //tools/bazel:release_cross for windows/amd64 +# and darwin/arm64 (BD_LINT_TARGETS selects; scripts/pr-lint). +lint: ci-pr-lint + +# go test's vet checks run inside nogo; same as lint. +vet: lint + +# nogo over the Bazel packages of changed Go files, natively (the pre-commit +# hook's lint). LINT_CHANGED_SCOPE: staged, or worktree (changed against +# LINT_CHANGED_REF, staged, and untracked). +LINT_CHANGED_SCOPE ?= worktree +LINT_CHANGED_REF ?= HEAD +lint-changed: + @case "$(LINT_CHANGED_SCOPE)" in \ + staged) files="$$(git diff --cached --name-only --diff-filter=ACMRT -- '*.go')" ;; \ + worktree) files="$$( \ + git diff --name-only --diff-filter=ACMRT "$(LINT_CHANGED_REF)" -- '*.go'; \ + git diff --cached --name-only --diff-filter=ACMRT -- '*.go'; \ + git ls-files --others --exclude-standard -- '*.go')" ;; \ + *) echo "unknown LINT_CHANGED_SCOPE=$(LINT_CHANGED_SCOPE); expected staged or worktree" >&2; exit 2 ;; \ + esac; \ + packages=""; missing=""; \ + for file in $$files; do \ + dir="$$(dirname "$$file")"; \ + case "/$$dir/" in */testdata/*) continue ;; esac; \ + if [ -f "$$dir/BUILD.bazel" ]; then packages="$$packages //$${dir#.}:all"; else missing="$$missing $$dir"; fi; \ + done; \ + if [ -n "$$missing" ]; then \ + echo "lint-changed: no BUILD.bazel in$$missing; run 'make bazel-sync'" >&2; exit 1; \ + fi; \ + packages="$$(printf '%s\n' $$packages | sort -u | sed 's#^///#//#; s#^//\.:#//:#')"; \ + if [ -z "$$packages" ]; then echo "lint-changed: no changed Go packages"; exit 0; fi; \ + echo "lint-changed:" $$packages; \ + $(BAZEL) build --config=nogo -- $$packages + # Opt-in architecture experiment. Install gocyclo v0.6.0 first; # report is advisory while check exercises the local baseline guard. ci-complexity: @@ -462,7 +503,9 @@ help: @echo " make test-full-cgo - Deprecated alias for make test-icu-path" @echo " make ci-pr-core - Run required PR core Go test wrapper" @echo " make ci-pr-policy - Run required PR policy wrapper" - @echo " make ci-pr-lint - Run required PR formatting and lint wrapper" + @echo " make ci-pr-lint - Run the required lint gate: nogo (vet + golangci-lint's linters) under Bazel, native + windows/darwin" + @echo " make lint - Same as ci-pr-lint (make vet too)" + @echo " make lint-changed - nogo over the Bazel packages of changed Go files (LINT_CHANGED_SCOPE=staged|worktree)" @echo " make ci-complexity - Report production cyclomatic complexity (advisory)" @echo " make ci-complexity-diff - Compare complexity with COMPLEXITY_BASE_REF" @echo " make ci-complexity-check - Check complexity against the local baseline" diff --git a/cmd/bd/preflight.go b/cmd/bd/preflight.go index 848bf855c..03cd168be 100644 --- a/cmd/bd/preflight.go +++ b/cmd/bd/preflight.go @@ -378,9 +378,10 @@ func runLintCheckAt(root string, skipLint bool) CheckResult { cmd.Dir = invocation.dir // CombinedOutput collects through a pipe that every descendant inherits, so // killing the direct child on deadline does not close the write end while a - // golangci-lint grandchild still holds it. Without a WaitDelay the deadline - // therefore cannot bound this call at all, and the message appended below - // never prints in the one case it exists to report. + // linter grandchild (golangci-lint, or bazel under the Beads driver) still + // holds it. Without a WaitDelay the deadline therefore cannot bound this + // call at all, and the message appended below never prints in the one case + // it exists to report. cmd.WaitDelay = lintCancellationGrace output, err := cmd.CombinedOutput() // Report the deadline only when it actually decided the result: a child that diff --git a/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md b/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md index d78878efc..44771b99c 100644 --- a/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md +++ b/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md @@ -657,7 +657,6 @@ Do not require these existing check names directly: - `Test (Server Dolt Conformance)` - `Test (Server Dolt Full Suite 1/16)` through `Test (Server Dolt Full Suite 16/16)` - `Test (Windows - smoke)` -- `PR Lint (native)`, `PR Lint (windows)` and `PR Lint (darwin)` - `Test Nix Flake` - `Bazel / test` and the other jobs of `bazel.yml` - `Bazel Farm / *` (`bazel-farm.yml`'s advisory, PR-controlled results) @@ -929,12 +928,14 @@ manifests. On those PRs they are the tiers' only pre-merge run, and `//tools/bazel:pure_bd_has_no_cgo_only_deps` (a pure bd must not link gozstd, which `go build` rejects with cgo off). Same cgo-off boundary as the job it replaces. - - Also kept on every PR, in the required job `scripts-go-checks` - (`SCRIPTS_GO_CHECKS`, one leg, `Go checks (vet)`): `go test`'s own vet - checks (cmd/go's `defaultVetFlags`, policy-tested equal to the - toolchain's) over `./...` (`scripts/ci/go-test-vet.sh`): rules_go's - `go_test` runs no vet, so a `go test` vet finding would otherwise first - fail on `main` and then on every fork PR. + - `go test`'s own vet checks (cmd/go's `defaultVetFlags`, policy-tested + equal to the toolchain's) and the golangci-lint linters `.golangci.yml` + enables run as nogo (`//tools/nogo`) beside every compile of every Bazel + lane: natively in `bazel test //... --config=ci`, and for every release + platform in the `bazel-pure` lane's release cross-compile + (engdocs/LINTING.md). The former + `scripts-go-checks` (`Go checks (vet)`) and `pr-lint-wrapper` + (`PR Lint (native|windows|darwin)`) jobs are retired. - The repository policy tests (`./scripts/...`, including the D2 guards) and the tests that walk the checkout run only under Bazel, remotely: `//scripts:scripts_test` and `//test/docsync:docsync_test` take @@ -1528,8 +1529,7 @@ check read. Non-Bazel required jobs re-run in full on every merge group (approximate PR timings, 2026-10): `fast-checks` (~40 s), `pr-policy-wrapper` -(~2.5 min), `scripts-go-checks` (vet only), `pr-lint-wrapper` -(native/darwin/windows, up to ~4 min), `check-doc-flags` (~1.7 min), +(~2.5 min), `check-doc-flags` (~1.7 min), `check-doc-freshness-platforms` and `pr-preflight-platforms` (Linux, Windows and macOS legs, up to ~5 min on Windows), `windows-make-shell` (~2.7 min), diff --git a/engdocs/DOC_INVENTORY.md b/engdocs/DOC_INVENTORY.md index 02ec666e1..9735f1a55 100644 --- a/engdocs/DOC_INVENTORY.md +++ b/engdocs/DOC_INVENTORY.md @@ -35,7 +35,7 @@ freshness source. | `reference/json-schema.md` | `Last reviewed:` marker tied to `cmd/bd/output.go`, `cmd/bd/errors.go`, and protocol tests. (Mintlify port: was `JSON_SCHEMA.md`.) | | `recovery/init-safety.md` | `Last reviewed:` marker tied to `cmd/bd/init*.go` safety code and tests. (Mintlify port: was `RECOVERY.md`.) | | `ERROR_HANDLING.md` | `Last reviewed:` marker tied to current command error exits and JSON error helpers. | -| `LINTING.md` | `Last reviewed:` marker tied to `.golangci.yml`, `scripts/ci/pr-lint.sh`, `scripts/pr-lint/`, the `Makefile` wrapper, and CI workflow wiring. | +| `LINTING.md` | `Last reviewed:` marker tied to `.golangci.yml`, `tools/nogo/`, `scripts/ci/pr-lint.sh`, `scripts/pr-lint/`, the `Makefile` wrapper, and CI workflow wiring. | | `SERVE_RUNBOOK.md` | `Last reviewed:` marker tied to the operating-envelope constants in `internal/httpapi/server.go` and the log fields in its `event`/`request` emitters. | | `CI_CLEANUP_PLAN.md` | `Last reviewed:` marker tied to the CI audit, workflow files, wrapper scripts, `Makefile`, package manifests, and maintainer decision review. | | `design/otel/otel-data-model.md` | `Last reviewed:` marker tied to telemetry, Dolt storage, hooks, and AI call sites. | diff --git a/engdocs/LINTING.md b/engdocs/LINTING.md index 6b446e73b..15a8c4bdf 100644 --- a/engdocs/LINTING.md +++ b/engdocs/LINTING.md @@ -1,99 +1,88 @@ # Linting Policy -Last reviewed: 2026-10-03 +Last reviewed: 2026-10-07 -Freshness source: `.golangci.yml`, `scripts/ci/pr-lint.sh`, `scripts/pr-lint/`, -`Makefile`, `.github/workflows/pr.yml`, and `.github/workflows/main.yml`. +Freshness source: `.golangci.yml`, `tools/nogo/`, `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. +This document explains the required Go lint and vet gate for this codebase. ## Current Status -Lint is a required CI gate: ONE job per lane (`pr-lint-wrapper`), over the same -pinned golangci-lint v2.10.1 release and the same `.golangci.yml`. Each lane -must pass with zero issues in its own scope. +Lint and vet run as **nogo** (`//tools/nogo`) inside the Bazel build. rules_go +runs the analyzers beside every first-party Go compile, so any `bazel build` +or `bazel test`, locally or on rbe-west, fails on a finding. The analyzers are: -- **The PR lane reports only what the PR introduces.** `pr-lint-wrapper` runs - the repository-owned `ci-pr-lint` wrapper with `BD_LINT_NEW_FROM_MERGE_BASE` - set to the diff against the merge base with `main`. A finding in code the PR - did not touch does not block it. "New" is measured off the diff, so MOVED - CODE READS AS NEW: a pre-existing violation carried into a PR by a move or a - rename is reported against that PR and blocks it. Fix it or `//nolint` it - there — that is the accepted cost of the diff-scoped trade. -- **The main lane sweeps the whole tree.** The same job runs on every push to - `main` with `BD_LINT_NEW_FROM_MERGE_BASE` unset, so a finding that lands - there reds main's own run rather than every open PR. +- **go test's vet checks**: the passes cmd/go's `defaultVetFlags` give + `go test` (`atomic`, `bools`, `buildtag`, `directive`, `errorsas`, + `ifaceassert`, `nilfunc`, `printf`, `slog`, `stringintconv`, `tests`). They + see every file, tests included, as the former `go vet ./...` did. +- **The golangci-lint linters `.golangci.yml` enables** (depguard, errcheck, + forbidigo, gosec, misspell, sloglint, unconvert, unparam), each a thin + wrapper in `tools/nogo/analyzers/` over the same library version + golangci-lint v2.10.1 uses. -`pr-lint-wrapper` is a 3-leg matrix, `matrix.target: [native, windows, darwin]` -(`fail-fast: false`), one leg per `scripts/pr-lint` pass (see below); the gate -requires the matrix's aggregate result under the same `PR_LINT_WRAPPER` id as -before. A former separate `Lint` job (the `golangci-lint-action` with -`only-new-issues`) was functionally identical to this matrix's native leg -(same binary, config, flags and diff scope) and was folded into it; see #5629. +`.golangci.yml` is still the one place lint policy lives. Bazel embeds it into +the analyzers, and `tools/nogo/internal/golangci` applies it as golangci-lint +did: which linters are enabled, their settings, `run.tests: false` (only the +library compile unit of a package is linted, never its test unit), +generated-file handling, and the path/text exclusion rules. Decoding is +strict: a key the wrappers do not implement fails the build instead of being +ignored. `//nolint:` directives keep working; a rules_go patch +(`third_party/patches/rules_go_nogo_golangci_nolint.patch`, shared with +gascity) gives them golangci-lint's line ranges. -Each leg installs golangci-lint from the pinned release binary -(`scripts/ci/install-golangci-lint.sh`, sha256-verified) instead of -`go install`, and restores a per-leg cache -(`~/.cache/golangci-lint` plus a leg-specific `GOCACHE`) keyed on -`.golangci.yml`, `go.sum` and the leg. A miss on the exact content key falls -back to the leg's most recent entry; golangci-lint revalidates it against the -current config and inputs, so a stale restore cannot mask an issue. pr.yml -only ever restores; main.yml's matrix is the only saver, on an exact-key miss. +Parity with golangci-lint was proved on the whole tree: with the exclusion +rules removed, golangci-lint and nogo report the same 3,916 findings (file, +line, linter and text) natively and the same 3,859 for windows/amd64 and +darwin/arm64; with `.golangci.yml` as committed, both report none. -Run the wrapper locally with: +Where it gates: + +- **bazel.yml's `test` lane** (required through `CI Gate / Required`): + `bazel test //... --config=ci` validates every package natively. +- **bazel.yml's `pure` lane** (required): its release cross-compile + (`scripts/ci/bazel-release-cross-compile.sh`) builds + `//tools/bazel:release_cross`, every `go_library` and `go_binary` + split-transitioned to each release platform without cgo, and nogo + validates each of those compiles. So `//go:build windows`, `darwin`, + `!linux` (and freebsd, android, arm64) files are analyzed from Linux: a + superset of golangci-lint's former `GOOS=windows`/`GOOS=darwin` legs. +- **Every other Bazel lane** (integration, embedded, dolt-server) validates + what it compiles, including files only its build tags select. + +Every PR is checked against the whole tree, not only its diff: the tree has +no findings, so there is no baseline to scope against. + +Run the gate locally with: ```bash -make ci-pr-lint +make ci-pr-lint # native + windows + darwin (also: make lint, make vet) +make lint-changed # native, only the Bazel packages of changed Go files ``` -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. +Both need `bazel` (or `bazelisk`) on PATH. With remote execution configured +(`.bazelrc.local` or `user.bazelrc`), a warm run takes seconds: the native +pass uses the race configuration of the CI test lane, so it reuses CI's +remote cache. `--config=nogo` asks for the analysis output group alone, so +nothing is linked. -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 pre-commit hook does not call it: the hook -uses changed-lines scope, adds `--fix`, and omits the Windows cross-lint pass. -The driver honors `BD_LINT_TARGETS` (a comma list of `native`, `windows` and -`darwin`; default all three) so each CI matrix leg can run just its own pass; -`make ci-pr-lint` and `bd preflight` both leave it unset and keep running all -three. - -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. +`make ci-pr-lint` runs `scripts/ci/pr-lint.sh`, which times the checkout-owned +`scripts/pr-lint` Go driver: `bazel build --config=nogo //...`, then +`//tools/bazel:release_cross` for windows/amd64 and darwin/arm64 +(`--config=nogo-cross`). The driver honors `BD_LINT_TARGETS` (a comma list of +`native`, `windows` and `darwin`; default all three), prints a heading and +result per pass, and is what Beads-source `bd preflight` runs. The pre-commit +hook (and `.pre-commit-config.yaml`) runs `make lint-changed +LINT_CHANGED_SCOPE=staged`. Formatting is not part of lint: +`//scripts/repochecks:fmt_test` gates gofmt, and the hook formats staged files +before linting them. ## 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`. +Treat lint findings as defects to fix before merge. Do not add a tolerated +failing baseline. When a linter reports an intentional or false-positive pattern: @@ -102,16 +91,18 @@ When a linter reports an intentional or false-positive pattern: 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. +To enable another golangci-lint linter, add it to `.golangci.yml`, add a +wrapper under `tools/nogo/analyzers/` (with its settings in +`tools/nogo/internal/golangci`), and list it in `tools/nogo/analyzers.bzl`; +`//tools/nogo:nogo_test` fails until the enabled list and the analyzer list +agree. ## 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. +The former `PR Lint (native|windows|darwin)` and `Go checks (vet)` jobs, and +main.yml's lint and vet cache seeders, are retired: nogo runs the same checks +in the Bazel lanes on rbe-west. When the farm is switched off (bazel.yml mode +`skip`), no lane runs, so neither does lint. See [`CI_CLEANUP_PLAN.md`](CI_CLEANUP_PLAN.md) for the full CI tier policy. diff --git a/gazelle.go.work b/gazelle.go.work new file mode 100644 index 000000000..44b471a97 --- /dev/null +++ b/gazelle.go.work @@ -0,0 +1,11 @@ +// Bazel-only Go workspace: go_deps (MODULE.bazel) resolves the beads module +// and the nogo analyzers' module (tools/nogo) as one dependency graph, so the +// analyzers link against the same golang.org/x/tools as rules_go's nogo +// runner. Not named go.work on purpose: the go command never picks it up, and +// `go build`/`go list -m all` in the repository root keep seeing only go.mod. +go 1.26.5 + +use ( + . + ./tools/nogo +) diff --git a/internal/testutil/integration/diagnostics.go b/internal/testutil/integration/diagnostics.go index 136d724ec..0e0b7c73e 100644 --- a/internal/testutil/integration/diagnostics.go +++ b/internal/testutil/integration/diagnostics.go @@ -39,7 +39,7 @@ func (d *Diagnostics) CaptureOnFailure() { func (d *Diagnostics) dumpStateFiles() { for _, name := range []string{"dolt-server.pid", "dolt-server.port", "dolt-server.log"} { path := filepath.Join(d.beadsDir, name) - data, err := os.ReadFile(path) + data, err := os.ReadFile(path) //nolint:gosec // G304: a fixed state-file name under the test's own .beads dir if err != nil { d.t.Logf(" %s: not found", name) continue @@ -69,7 +69,7 @@ func (d *Diagnostics) dumpProcessList() { if pid[0] < '0' || pid[0] > '9' { continue } - cmdline, err := os.ReadFile(filepath.Join("/proc", pid, "cmdline")) + cmdline, err := os.ReadFile(filepath.Join("/proc", pid, "cmdline")) //nolint:gosec // G304: /proc//cmdline if err != nil { continue } diff --git a/internal/testutil/integration/harness.go b/internal/testutil/integration/harness.go index 03fd1607f..f6af935de 100644 --- a/internal/testutil/integration/harness.go +++ b/internal/testutil/integration/harness.go @@ -183,7 +183,7 @@ func (r *SubprocessRunner) Build(t *testing.T) string { r.buildErr = fmt.Errorf("failed to build test binary: %w\nstderr: %s\nstdout: %s", err, stderr.String(), stdout.String()) return } - if err := os.Chmod(r.testBin, 0700); err != nil { + if err := os.Chmod(r.testBin, 0700); err != nil { //nolint:gosec // G302: the test binary must be executable r.buildErr = fmt.Errorf("failed to chmod test binary: %w", err) } }) @@ -349,7 +349,7 @@ func WaitForPort(t *testing.T, host string, port int, timeout time.Duration) { // ReadPIDFile reads and parses a PID file, returning 0 if missing or corrupt. func ReadPIDFile(path string) int { - data, err := os.ReadFile(path) + data, err := os.ReadFile(path) //nolint:gosec // G304: a state file path the harness itself derived if err != nil { return 0 } @@ -362,7 +362,7 @@ func ReadPIDFile(path string) int { // ReadPortFile reads and parses a port file, returning 0 if missing or corrupt. func ReadPortFile(path string) int { - data, err := os.ReadFile(path) + data, err := os.ReadFile(path) //nolint:gosec // G304: a state file path the harness itself derived if err != nil { return 0 } diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index 443631a45..f19b1c2a6 100644 --- a/scripts/BUILD.bazel +++ b/scripts/BUILD.bazel @@ -42,6 +42,7 @@ go_test( "install_dolt_test.go", "managed_local_lane_test.go", "migration_corpus_bazel_test.go", + "nogo_lint_policy_test.go", "pr_lanes_bazel_coverage_test.go", "pr_preflight_test.go", "pr_risk_bazel_coverage_test.go", diff --git a/scripts/README.md b/scripts/README.md index d1969de1b..d9d99d6de 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -19,10 +19,11 @@ Each wrapper auto-detects the repository root, sources `.buildflags` when it invokes Go in the default build mode, and records per-command timing through `scripts/ci/lib/timing.sh`. -`ci-pr-lint` keeps its supported Bash/Make entrypoint but delegates native and -Windows/non-CGO target selection to the checkout-owned `scripts/pr-lint` Go -driver. This is the same shell-free lint authority used by `bd preflight` in a -Beads source checkout. +`ci-pr-lint` keeps its supported Bash/Make entrypoint but delegates to the +checkout-owned `scripts/pr-lint` Go driver, which runs nogo (`//tools/nogo`) +under Bazel natively and cross-configured for windows/amd64 and darwin/arm64 +(see [engdocs/LINTING.md](../engdocs/LINTING.md)). This is the same shell-free +lint authority used by `bd preflight` in a Beads source checkout. Broad Go test wrappers also source `scripts/ci/lib/test-env.sh`, which creates a temporary HOME/XDG/Dolt root, isolates Git global/system config, clears runtime @@ -136,7 +137,6 @@ This master script automates the **entire release process**: - Clean git working directory - All changes committed -- golangci-lint installed - Homebrew installed (for local upgrade) - Push access to gastownhall/beads diff --git a/scripts/check_doc_freshness_test.go b/scripts/check_doc_freshness_test.go index 0d33d1a5e..59e3a4bb2 100644 --- a/scripts/check_doc_freshness_test.go +++ b/scripts/check_doc_freshness_test.go @@ -25,7 +25,7 @@ var freshnessDocuments = []struct { {"docs/recovery/init-safety.md", []string{"cmd/bd/init.go", "cmd/bd/init_safety.go", "cmd/bd/init_safety_test.go", "cmd/bd/init_safety_help.go", "cmd/bd/dolt.go"}}, {"engdocs/ERROR_HANDLING.md", []string{"cmd/bd/*.go", "cmd/bd/errors.go"}}, {"engdocs/SERVE_RUNBOOK.md", []string{"internal/httpapi/server.go", "internal/httpapi/events_watch.go", "cmd/bd/serve.go", "internal/httpapi/auth.go"}}, - {"engdocs/LINTING.md", []string{".golangci.yml", "scripts/ci/pr-lint.sh", "scripts/pr-lint/", "Makefile", ".github/workflows/pr.yml", ".github/workflows/main.yml"}}, + {"engdocs/LINTING.md", []string{".golangci.yml", "tools/nogo/", "scripts/ci/pr-lint.sh", "scripts/pr-lint/", "Makefile", ".github/workflows/pr.yml", ".github/workflows/main.yml"}}, {"engdocs/CI_CLEANUP_PLAN.md", []string{"engdocs/CI_TEST_SURFACE_AUDIT.md", ".github/workflows/*.yml", ".buildflags", ".golangci.yml", "scripts/ci/pr-lint.sh", "scripts/pr-lint/", "Makefile"}}, {"engdocs/design/otel/otel-data-model.md", []string{"internal/telemetry/", "internal/storage/dolt/store.go", "internal/compact/haiku.go", "cmd/bd/find_duplicates.go", "internal/hooks/"}}, } diff --git a/scripts/ci/bazel-release-cross-compile.sh b/scripts/ci/bazel-release-cross-compile.sh index e369ff044..a1457b1cb 100755 --- a/scripts/ci/bazel-release-cross-compile.sh +++ b/scripts/ci/bazel-release-cross-compile.sh @@ -50,11 +50,13 @@ for target in "${targets[@]}"; do platforms+=("${target%/*}_${target#*/}") done -# Every Bazel package holding a Go target (less testonly targets and the -# packages of cgo-only ones) must be reached by //tools/bazel:release_cross; +# Every Bazel package holding a Go target (less testonly targets, the +# packages of cgo-only ones, and //tools/nogo, the nogo analyzers' own Go +# module, which `go build ./...` skips) must be reached by +# //tools/bazel:release_cross; # a listed label that no longer exists fails the build itself. universe='kind("go_library|go_binary", //...)' -wanted="$universe except siblings(attr(tags, \"\\bcgo-only\\b\", $universe)) except attr(testonly, 1, $universe)" +wanted="$universe except siblings(attr(tags, \"\\bcgo-only\\b\", $universe)) except attr(testonly, 1, $universe) except //tools/nogo/..." # One query at a time: they share the Bazel server. want_pkgs="$(bazel query "$wanted" --output=package | LC_ALL=C sort -u)" built_pkgs="$(bazel query 'deps(//tools/bazel:release_cross)' --output=package | LC_ALL=C sort -u)" diff --git a/scripts/ci/go-test-vet.sh b/scripts/ci/go-test-vet.sh deleted file mode 100755 index 91fbbea2a..000000000 --- a/scripts/ci/go-test-vet.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash -# The vet checks `go test` runs on every package it tests (cmd/go's -# defaultVetFlags), over the whole module. rules_go's go_test runs none, so -# where PR Core's `go test ./...` stands down for the Bazel lane (D2 step 3) -# this is what keeps them pre-merge. pr.yml runs it on every PR -# (scripts-go-checks). scripts/pr_lanes_bazel_coverage_test.go keeps the list -# equal to the Go toolchain's. - -set -euo pipefail - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" - -# shellcheck source=../../.buildflags -source "$REPO_ROOT/.buildflags" -# shellcheck source=lib/timing.sh -source "$REPO_ROOT/scripts/ci/lib/timing.sh" - -cd "$REPO_ROOT" - -GO_TEST_VET_FLAGS=(-atomic -bool -buildtags -directive -errorsas -ifaceassert -nilfunc -printf -slog -stringintconv -tests) - -ci_time "go vet (go test's checks)" -- \ - go vet -tags gms_pure_go "${GO_TEST_VET_FLAGS[@]}" ./... diff --git a/scripts/ci/install-golangci-lint.sh b/scripts/ci/install-golangci-lint.sh deleted file mode 100755 index f9c0182ce..000000000 --- a/scripts/ci/install-golangci-lint.sh +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail - -# Install the pinned golangci-lint release binary instead of `go install` -# (F5.2): the release tarball drops the install step from about 58s to about -# 3s, because it is a straight download-and-verify rather than a compile. -# CI-only: this script requires $RUNNER_TEMP and $GITHUB_PATH, so the local -# `.githooks/pre-commit` hook keeps using -# `go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.10.1`, -# which needs no change here. `make ci-pr-lint` (scripts/pr-lint) looks up -# whatever `golangci-lint` binary is on PATH; it does not use `go run`. -# -# Keep this version and the sha256 values in sync with CONTRIBUTING.md, -# engdocs/LINTING.md, .github/workflows/pr.yml, .github/workflows/main.yml, -# .github/workflows/ci-measurements.yml and .githooks/pre-commit. Verified -# locally against the release's own golangci-lint-2.10.1-checksums.txt. -readonly version="2.10.1" -readonly max_attempts=3 -readonly retry_delay_seconds=5 - -# A case function rather than `declare -A`: associative arrays need bash 4, -# and macOS's /bin/bash 3.2 silently parses `declare -A` as an indexed array, -# so `[amd64]=` becomes an arithmetic lookup that dies under `set -u` before -# the Linux-only guard below can print its message. -sha256_for_arch() { - case "$1" in - amd64) printf '%s' "dfa775874cf0561b404a02a8f4481fc69b28091da95aa697259820d429b09c99" ;; - arm64) printf '%s' "6652b42ae02915eb2f9cb2a2e0cac99514c8eded8388d88ae3e06e1a52c00de8" ;; - esac -} - -: "${RUNNER_TEMP:?RUNNER_TEMP is required; this script is CI-only}" -: "${GITHUB_PATH:?GITHUB_PATH is required; this script is CI-only}" - -os="$(uname -s | tr '[:upper:]' '[:lower:]')" -if [[ "$os" != "linux" ]]; then - printf 'install-golangci-lint.sh only supports Linux runners, got: %s\n' "$os" >&2 - exit 1 -fi - -arch="$(uname -m)" -case "$arch" in - x86_64 | amd64) arch="amd64" ;; - aarch64 | arm64) arch="arm64" ;; - *) - printf 'Unsupported architecture for pinned golangci-lint install: %s\n' "$arch" >&2 - exit 1 - ;; -esac - -expected_sha256="$(sha256_for_arch "$arch")" -: "${expected_sha256:?no pinned sha256 for arch: $arch}" -readonly asset="golangci-lint-${version}-${os}-${arch}.tar.gz" -readonly url="https://github.com/golangci/golangci-lint/releases/download/v${version}/${asset}" - -workdir="$(mktemp -d)" -trap 'rm -rf "$workdir"' EXIT - -for ((attempt = 1; attempt <= max_attempts; attempt++)); do - # See install-dolt.sh for why the status capture can't live in the `if`. - status=0 - curl -fsSL -o "$workdir/$asset" "$url" || status=$? - if ((status == 0)); then - break - fi - if ((attempt == max_attempts)); then - printf 'Failed to download %s after %d attempts (curl exit %d).\n' "$url" "$max_attempts" "$status" >&2 - exit "$status" - fi - printf 'Failed to download %s (attempt %d/%d); retrying in %d seconds.\n' \ - "$url" "$attempt" "$max_attempts" "$retry_delay_seconds" >&2 - sleep "$retry_delay_seconds" -done - -actual_sha256="$(sha256sum "$workdir/$asset" | awk '{print $1}')" -if [[ "$actual_sha256" != "$expected_sha256" ]]; then - printf '%s sha256 mismatch: got %s, want %s\n' "$asset" "$actual_sha256" "$expected_sha256" >&2 - exit 1 -fi - -tar -xzf "$workdir/$asset" -C "$workdir" - -install_dir="$RUNNER_TEMP/golangci-lint" -mkdir -p "$install_dir" -install -m 0755 "$workdir/golangci-lint-${version}-${os}-${arch}/golangci-lint" "$install_dir/golangci-lint" - -echo "$install_dir" >> "$GITHUB_PATH" - -# Fail loudly if the extracted binary does not report the pinned version: a -# bad tarball layout would otherwise silently put a stale or wrong binary on -# PATH. Compare the version token exactly, as install-dolt.sh does. -installed="$("$install_dir/golangci-lint" version)" -if [[ "$installed" != *"version $version"* ]]; then - printf 'golangci-lint reports "%s", want version %s\n' "$installed" "$version" >&2 - exit 1 -fi -printf 'Installed pinned golangci-lint %s to %s (sha256 %s)\n' "$version" "$install_dir" "$actual_sha256" diff --git a/scripts/ci/pr-lint.sh b/scripts/ci/pr-lint.sh index 7b50ab423..166dace31 100755 --- a/scripts/ci/pr-lint.sh +++ b/scripts/ci/pr-lint.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash -# Required PR formatting and Go lint contract. +# Required PR lint contract: nogo (//tools/nogo: go test's vet checks plus +# the golangci-lint linters .golangci.yml enables) under Bazel, natively and +# cross-configured for windows/amd64 and darwin/arm64. BD_LINT_TARGETS selects +# passes (default: native,windows,darwin). gofmt is a Bazel test +# (//scripts/repochecks:fmt_test). set -euo pipefail @@ -13,31 +17,8 @@ source "$REPO_ROOT/scripts/ci/lib/timing.sh" cd "$REPO_ROOT" -# gofmt only has one pass (there is no windows- or darwin-only gofmt target), -# so it belongs to the native leg of the pr.yml PR Lint matrix. Skip it when -# BD_LINT_TARGETS names only windows and/or darwin; run it when the variable -# is unset, empty, or names "native" (matching scripts/pr-lint's own default -# of all three targets). -should_run_fmt_check() { - local IFS=',' - local target saw_target=0 - for target in ${BD_LINT_TARGETS:-}; do - target="${target//[[:space:]]/}" - [[ -z "$target" ]] && continue - saw_target=1 - [[ "$target" == "native" ]] && return 0 - done - ((saw_target == 0)) -} - -if should_run_fmt_check; then - ci_time "gofmt check" -- ./scripts/ci/fmt-check.sh -fi - -# The checkout-owned Go driver is the single authority for the native and -# cross-target lint arguments. Files guarded by //go:build windows && !cgo or -# //go:build darwin are invisible to the Linux runner, so the driver cross-lints -# those non-CGO targets from the same runner. Keep this wrapper as the supported -# direct Bash/Make entrypoint and aggregate timing boundary. -ci_time "golangci-lint (native + windows/darwin non-CGO)" -- \ +# The checkout-owned Go driver is the single authority for the passes and +# their Bazel arguments. Keep this wrapper as the supported direct Bash/Make +# entrypoint and aggregate timing boundary. +ci_time "nogo (native + windows/darwin non-cgo)" -- \ go run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint diff --git a/scripts/ci_f7b_review_test.go b/scripts/ci_f7b_review_test.go index 3346151e2..2387d8863 100644 --- a/scripts/ci_f7b_review_test.go +++ b/scripts/ci_f7b_review_test.go @@ -69,10 +69,10 @@ func TestBlacksmithReachablePRJobsDisableDefaultCachingActions(t *testing.T) { // Blacksmith-selection GOCACHE/vet-cache a same-repo PR or merge_group job // above restores from. F7c's blacksmith-setup-go-cache is pinned by its own // TestBlacksmithSeederGuardedAgainstPullRequest in ci_f7c_advisory_test.go; -// these are F7b's four (B2), the macOS saver, and the Blacksmith macOS +// these are F7b's remaining savers (B2), the macOS saver, and the Blacksmith macOS // `test` job (which saves its own race cache). var blacksmithSaverJobs = []string{ - "blacksmith-go-build-cache", "pr-lint-wrapper", "go-vet-cache", "test-windows", + "blacksmith-go-build-cache", "test-windows", "blacksmith-macos-go-build-cache", "test", } @@ -135,7 +135,7 @@ func TestBlacksmithSaverJobsGuardedAgainstPullRequest(t *testing.T) { } // TestBlacksmithSaverCacheKeysAreNotPerCommit is the S7 fix's pin: each of -// blacksmith-go-build-cache's, go-vet-cache's and test-windows' Go-cache +// blacksmith-go-build-cache's and test-windows' Go-cache // restore/save key pairs is bounded by go.sum content plus a UTC calendar day // (via that job's own "Compute cache date" step, id: cache-date), not by // github.sha, so an ordinary day of push traffic to main cannot mint a new @@ -158,11 +158,6 @@ func TestBlacksmithSaverCacheKeysAreNotPerCommit(t *testing.T) { restoreStepNames: []string{"Restore race Go build cache", "Restore non-race Go build cache"}, saveStepNames: []string{"Save race Go build cache", "Save non-race Go build cache"}, }, - { - job: "go-vet-cache", - restoreStepNames: []string{"Restore vet Go build cache"}, - saveStepNames: []string{"Save vet Go build cache"}, - }, { job: "test-windows", restoreStepNames: []string{"Restore non-race Go build cache"}, @@ -208,7 +203,7 @@ func TestBlacksmithSaverCacheKeysAreNotPerCommit(t *testing.T) { // TestBlacksmithSaverVenueAndFlavorMatricesAreComplete re-pins mutations the // reviewer's mutate.py found surviving against pre-fix code (M8, M9): main.yml's -// "venue matrix" savers (pr-lint-wrapper, go-vet-cache, test-windows, and the +// "venue matrix" savers (test-windows and the // Linux and macOS Go build cache savers) // must each keep BOTH the `blacksmith` leg (the actual same-repo-PR seed) and // the `github` leg (that job's pre-existing fork-PR/GitHub-hosted coverage), @@ -219,7 +214,7 @@ func TestBlacksmithSaverCacheKeysAreNotPerCommit(t *testing.T) { // axis's value list shrinks). func TestBlacksmithSaverVenueAndFlavorMatricesAreComplete(t *testing.T) { workflow := readCIWorkflow(t, "main.yml") - for _, jobName := range []string{"pr-lint-wrapper", "go-vet-cache", "test-windows", "blacksmith-go-build-cache", "blacksmith-macos-go-build-cache"} { + for _, jobName := range []string{"test-windows", "blacksmith-go-build-cache", "blacksmith-macos-go-build-cache"} { job := workflow.job(t, jobName) if got := job.Strategy.Matrix.Venue; !equalStrings(got, []string{"blacksmith", "github"}) { t.Errorf("main.yml's %s matrix.venue = %v, want [blacksmith github]", jobName, got) diff --git a/scripts/ci_workflow_test.go b/scripts/ci_workflow_test.go index c0aeaf076..afacfa1ba 100644 --- a/scripts/ci_workflow_test.go +++ b/scripts/ci_workflow_test.go @@ -9,7 +9,6 @@ import ( "path/filepath" "reflect" "regexp" - "runtime" "slices" "sort" "strconv" @@ -53,59 +52,16 @@ func TestCIWorkflowArtifactOwnership(t *testing.T) { } else { assertJobRunsExactly(t, workflow.job(t, "pr-policy-wrapper"), "make ci-pr-policy") } - assertJobRunsExactly(t, workflow.job(t, "pr-lint-wrapper"), "make ci-pr-lint") - - // F5.1: the duplicate whole-tree/diff-scoped `Lint` job (and its - // LINT gate id, pr.yml only) is folded into pr-lint-wrapper. - if _, found := workflow.Jobs["lint"]; found { - t.Errorf("%s has a %q job; its only-new-issues pass duplicates pr-lint-wrapper's native leg (H7) and was folded into it", workflowName, "lint") - } - - // F5.2: pr-lint-wrapper is a 3-leg matrix keyed on matrix.target, - // with BD_LINT_TARGETS telling scripts/pr-lint which legs to run. - lintWrapper := workflow.job(t, "pr-lint-wrapper") - if lintWrapper.Strategy.FailFast { - t.Errorf("%s pr-lint-wrapper strategy.fail-fast = true, want false", workflowName) - } - if want := []string{"native", "windows", "darwin"}; !equalStrings(lintWrapper.Strategy.Matrix.Target, want) { - t.Errorf("%s pr-lint-wrapper matrix.target = %v, want %v", workflowName, lintWrapper.Strategy.Matrix.Target, want) - } - if got := lintWrapper.step(t, "Run PR lint wrapper").Env["BD_LINT_TARGETS"]; got != "${{ matrix.target }}" { - t.Errorf("%s pr-lint-wrapper BD_LINT_TARGETS = %q, want %q", workflowName, got, "${{ matrix.target }}") - } - // N-5: a dropped GOCACHE env here silently stops the lint cache - // from doing anything (the step still passes; it just never - // reuses or populates the restored/saved directory). - if got, want := lintWrapper.step(t, "Run PR lint wrapper").Env["GOCACHE"], "${{ runner.temp }}/go-cache/lint-${{ matrix.target }}"; got != want { - t.Errorf("%s pr-lint-wrapper GOCACHE = %q, want %q", workflowName, got, want) - } - if got := lintWrapper.step(t, "Install golangci-lint").Run; got != "./scripts/ci/install-golangci-lint.sh" { - t.Errorf("%s pr-lint-wrapper installs golangci-lint with %q, want the pinned install script", workflowName, got) - } - // N-7. - if lintWrapper.TimeoutMinutes != 20 { - t.Errorf("%s pr-lint-wrapper timeout-minutes = %d, want 20 (matches scripts-go-checks)", workflowName, lintWrapper.TimeoutMinutes) + // Lint and vet are nogo inside Bazel (TestLintAndVetRunAsNogo): + // no workflow job runs golangci-lint or the retired lint and vet + // jobs. + for _, retired := range []string{"pr-lint-wrapper", "go-vet-cache", "scripts-go-checks", "lint"} { + if _, found := workflow.Jobs[retired]; found { + t.Errorf("%s has a %q job; lint and vet run as nogo in bazel.yml's test lane", workflowName, retired) + } } }) } - - if got := readCIWorkflow(t, "pr.yml").job(t, "pr-lint-wrapper").step(t, "Run PR lint wrapper").Env["BD_LINT_NEW_FROM_MERGE_BASE"]; got != "origin/main" { - t.Errorf("pr.yml pr-lint-wrapper BD_LINT_NEW_FROM_MERGE_BASE = %q, want origin/main (PR lane stays diff-scoped)", got) - } - if got := readCIWorkflow(t, "main.yml").job(t, "pr-lint-wrapper").step(t, "Run PR lint wrapper").Env["BD_LINT_NEW_FROM_MERGE_BASE"]; got != "" { - t.Errorf("main.yml pr-lint-wrapper BD_LINT_NEW_FROM_MERGE_BASE = %q, want unset", got) - } - // N-5: --new-from-merge-base in pr.yml's pr-lint-wrapper needs real - // history; a dropped fetch-depth: 0 would fail loudly (M3, a NIT-grade - // mutation per the review), but pin it anyway since it is part of the - // same wrapper contract as the GOCACHE pin above. - if got := readCIWorkflow(t, "pr.yml").job(t, "pr-lint-wrapper").Steps[0].With["fetch-depth"]; got != "0" { - t.Errorf("pr.yml pr-lint-wrapper checkout fetch-depth = %q, want %q", got, "0") - } - // go-vet-cache (N-7). - if job := readCIWorkflow(t, "main.yml").job(t, "go-vet-cache"); job.TimeoutMinutes != 20 { - t.Errorf("main.yml go-vet-cache timeout-minutes = %d, want 20 (matches scripts-go-checks)", job.TimeoutMinutes) - } } // TestPullRequestWorkflowsTriggerOnHotfixBranches is the #7148 regression. @@ -144,18 +100,22 @@ func TestPullRequestWorkflowsTriggerOnHotfixBranches(t *testing.T) { } } -func TestPRCIGateRequiresPolicyAndLintWrappers(t *testing.T) { +func TestPRCIGateDropsRetiredPolicyAndLintJobs(t *testing.T) { gate := readCIWorkflow(t, "pr.yml").job(t, "ci-gate") gateEnv := gate.step(t, "Evaluate CI gate").Env - if !contains(gate.Needs, "pr-lint-wrapper") { - t.Errorf("ci-gate needs %q: %v", "pr-lint-wrapper", gate.Needs) - } - if got, want := gateEnv["PR_LINT_WRAPPER"], "${{ needs.pr-lint-wrapper.result }}"; got != want { - t.Errorf("ci-gate env PR_LINT_WRAPPER = %q, want %q", got, want) - } - if !strings.Contains(gateEnv["CI_GATE_REQUIRED"], "PR_LINT_WRAPPER") { - t.Errorf("ci-gate CI_GATE_REQUIRED does not include %q", "PR_LINT_WRAPPER") + // The retired lint and vet jobs (now nogo in the Bazel test lane, which + // gates through BAZEL_TEST) must not linger in the gate. + for job, token := range map[string]string{"pr-lint-wrapper": "PR_LINT_WRAPPER", "scripts-go-checks": "SCRIPTS_GO_CHECKS"} { + if contains(gate.Needs, job) { + t.Errorf("ci-gate needs the retired %s: %v", job, gate.Needs) + } + if _, found := gateEnv[token]; found { + t.Errorf("ci-gate env still sets %s", token) + } + if contains(strings.Fields(gateEnv["CI_GATE_REQUIRED"]), token) { + t.Errorf("ci-gate CI_GATE_REQUIRED still lists %s", token) + } } // The retired PR Policy job's checks gate through BAZEL_TEST (the Bazel @@ -185,214 +145,6 @@ func TestPRCIGateRequiresPolicyAndLintWrappers(t *testing.T) { } } -// F5.2/F5.7: the pinned golangci-lint install script's version and sha256 -// values must stay in sync with LINTING.md's documented version and with -// .githooks/pre-commit's `go run ...@`, and the three workflows that -// use the script (pr.yml, main.yml, ci-measurements.yml) must all call it -// instead of `go install`. -func TestGolangciLintInstallScriptPinned(t *testing.T) { - root := sourceRepoRoot(t) - script := readPolicyFile(t, root, "scripts/ci/install-golangci-lint.sh") - - const version = "2.10.1" - wantSHA256 := map[string]string{ - "amd64": "dfa775874cf0561b404a02a8f4481fc69b28091da95aa697259820d429b09c99", - "arm64": "6652b42ae02915eb2f9cb2a2e0cac99514c8eded8388d88ae3e06e1a52c00de8", - } - - if !strings.Contains(script, `readonly version="`+version+`"`) { - t.Errorf("install-golangci-lint.sh does not pin version %q", version) - } - for arch, sha := range wantSHA256 { - // The script maps arch to sha256 with a case arm, not `declare -A`, - // so it still parses under macOS's bash 3.2. - if !strings.Contains(script, arch+") printf '%s' \""+sha+"\"") { - t.Errorf("install-golangci-lint.sh does not pin %s sha256 %q", arch, sha) - } - } - - linting := readPolicyFile(t, root, "engdocs/LINTING.md") - if !strings.Contains(linting, "v"+version) { - t.Errorf("engdocs/LINTING.md does not mention v%s", version) - } - - preCommit := readPolicyFile(t, root, ".githooks/pre-commit") - if !strings.Contains(preCommit, "golangci-lint/v2/cmd/golangci-lint@v"+version) { - t.Errorf(".githooks/pre-commit does not pin golangci-lint @v%s", version) - } - // The hook stays on `go run` deliberately (CI-only RUNNER_TEMP/GITHUB_PATH - // requirement); it must not switch to the install script. - if strings.Contains(preCommit, "install-golangci-lint.sh") { - t.Error(".githooks/pre-commit must keep using `go run`, not the CI-only install script") - } - - for _, workflow := range []string{"pr.yml", "main.yml"} { - job := readCIWorkflow(t, workflow).job(t, "pr-lint-wrapper") - if got := job.step(t, "Install golangci-lint").Run; got != "./scripts/ci/install-golangci-lint.sh" { - t.Errorf("%s pr-lint-wrapper installs golangci-lint with %q, want the pinned install script", workflow, got) - } - } - measurements := readPolicyFile(t, root, ".github/workflows/ci-measurements.yml") - if strings.Contains(measurements, "go install github.com/golangci/golangci-lint") { - t.Error("ci-measurements.yml still uses go install for golangci-lint; use scripts/ci/install-golangci-lint.sh") - } - if !strings.Contains(measurements, "install-golangci-lint.sh") { - t.Error("ci-measurements.yml does not call scripts/ci/install-golangci-lint.sh") - } - - // SF-2: install-golangci-lint.sh and the "pr-lint wrapper" measurement run - // in the same shell block (not separate steps), so appending to - // $GITHUB_PATH (which only takes effect in later *steps*) is not enough; - // the script's install_dir must also be exported into this step's own - // PATH, after the install call and before the wrapper that needs it. - const ( - installMarker = "install-golangci-lint.sh" - pathExport = `export PATH="$RUNNER_TEMP/golangci-lint:$PATH"` - wrapperMarker = `ci_time "pr-lint wrapper"` - ) - installIdx := strings.Index(measurements, installMarker) - pathIdx := strings.Index(measurements, pathExport) - wrapperIdx := strings.Index(measurements, wrapperMarker) - if pathIdx < 0 { - t.Errorf("ci-measurements.yml does not export golangci-lint's install dir onto PATH in the same step (want %q)", pathExport) - } - if installIdx < 0 || wrapperIdx < 0 { - t.Fatalf("ci-measurements.yml missing expected markers: install=%d wrapper=%d", installIdx, wrapperIdx) - } - if !(installIdx < pathIdx && pathIdx < wrapperIdx) { - t.Errorf("ci-measurements.yml PATH export must sit between the golangci-lint install (%d) and the pr-lint wrapper call (%d); got PATH export at %d", installIdx, wrapperIdx, pathIdx) - } -} - -// TestInstallGolangciLintFailsClosedOnChecksumMismatch is the regression net -// for install-golangci-lint.sh's supply-chain gate (SF-3). Without it, nothing -// exercises the sha256 comparison's behavior -- TestGolangciLintInstallScriptPinned -// above only pins the hash strings as text, so e.g. replacing the comparison -// with an always-false condition passes every other test in this package. A -// fake curl hands the script a tarball whose content never matches either -// pinned hash; a fake tar makes extraction succeed anyway (so a bypassed check -// is exposed cleanly as "installed the wrong thing", not masked by tar -// choking on bad bytes), which is exactly the gap a real supply-chain attack -// (a compromised release asset, a MITM'd download) would exploit if the -// comparison were missing or broken. -func TestInstallGolangciLintFailsClosedOnChecksumMismatch(t *testing.T) { - bash := requireHostTool(t, "bash") - - bin := t.TempDir() - stateDir := t.TempDir() - runnerTemp := t.TempDir() - curlLog := filepath.Join(stateDir, "curl-calls") - tarLog := filepath.Join(stateDir, "tar-calls") - githubPath := filepath.Join(stateDir, "github-path") - for _, path := range []string{curlLog, tarLog, githubPath} { - if err := os.WriteFile(path, nil, 0o600); err != nil { - t.Fatal(err) - } - } - - // Writes fixed, wrong-checksum bytes to curl's -o target. Any download - // (first attempt or a retry) "succeeds" the same way, so the script - // reaches the sha256 comparison deterministically. - writeExecutable(t, filepath.Join(bin, "curl"), `#!/bin/sh -set -eu -printf '%s\n' "$*" >>"$GOLANGCI_INSTALL_CURL_LOG" -out="" -prev="" -for arg in "$@"; do - if [ "$prev" = "-o" ]; then - out="$arg" - fi - prev="$arg" -done -if [ -n "$out" ]; then - printf 'not the real golangci-lint release asset\n' >"$out" -fi -`) - // Materializes a plausible-looking extracted binary unconditionally, the - // way a real tarball's contents would after a successful `tar -xzf`. The - // script's sha256 check -- not tar's own format validation -- must be - // what stands between this fake binary and $RUNNER_TEMP/golangci-lint. - writeExecutable(t, filepath.Join(bin, "tar"), `#!/bin/sh -set -eu -printf '%s\n' "$*" >>"$GOLANGCI_INSTALL_TAR_LOG" -outdir="" -prev="" -for arg in "$@"; do - if [ "$prev" = "-C" ]; then - outdir="$arg" - fi - prev="$arg" -done -mkdir -p "$outdir/golangci-lint-2.10.1-linux-amd64" -cat >"$outdir/golangci-lint-2.10.1-linux-amd64/golangci-lint" <<'SCRIPT' -#!/bin/sh -printf 'golangci-lint has version 2.10.1 built from unknown\n' -SCRIPT -chmod +x "$outdir/golangci-lint-2.10.1-linux-amd64/golangci-lint" -`) - // Pins the platform so the asset name, arch lookup and extracted layout - // are fixed regardless of the machine running the test. - writeExecutable(t, filepath.Join(bin, "uname"), `#!/bin/sh -set -eu -case "${1:-}" in - -m) printf 'x86_64\n' ;; - *) printf 'Linux\n' ;; -esac -`) - - pathEnv := shellPathEnv() - binPath := shellPathUnderEnv(t, bash, bin, pathEnv) - statePath := shellPathUnderEnv(t, bash, stateDir, pathEnv) - commandPath := binPath + ":" + os.Getenv("PATH") + ":/usr/bin:/bin" - if runtime.GOOS == "windows" { - commandPath = binPath + ":/usr/bin:/bin" - } - root := sourceRepoRoot(t) - env := []string{ - "PATH=" + os.Getenv("PATH"), - "BEADS_TEST_COMMAND_PATH=" + commandPath, - "LC_ALL=C", - "LANG=C", - "BASH_ENV=", - "ENV=", - "RUNNER_TEMP=" + shellPathUnderEnv(t, bash, runnerTemp, pathEnv), - "GITHUB_PATH=" + statePath + "/github-path", - "GOLANGCI_INSTALL_CURL_LOG=" + statePath + "/curl-calls", - "GOLANGCI_INSTALL_TAR_LOG=" + statePath + "/tar-calls", - } - for _, name := range []string{"curl", "tar", "uname"} { - requireShellCommandPath(t, bash, root, env, name, binPath+"/"+name) - } - - cmd := bashScriptCommand(bash, "scripts/ci/install-golangci-lint.sh") - cmd.Dir = root - cmd.Env = env - output, runErr := cmd.CombinedOutput() - - if code := pullDoltExitCode(runErr); code == 0 { - t.Fatalf("exit = 0, want non-zero; output:\n%s", output) - } - if !strings.Contains(string(output), "sha256 mismatch") { - t.Errorf("output does not contain %q:\n%s", "sha256 mismatch", output) - } - if calls := readCallLines(t, tarLog); len(calls) != 0 { - t.Errorf("tar was invoked %d time(s), want 0 (the sha256 check must reject the download before extraction): %q", len(calls), calls) - } - if calls := readCallLines(t, curlLog); len(calls) == 0 { - t.Fatalf("curl was never invoked; test did not exercise the script's download path") - } - if entries, err := os.ReadDir(runnerTemp); err != nil { - t.Fatal(err) - } else if len(entries) != 0 { - t.Errorf("RUNNER_TEMP = %v, want empty (nothing installed on a checksum mismatch)", entries) - } - if data, err := os.ReadFile(githubPath); err != nil { - t.Fatal(err) - } else if len(data) != 0 { - t.Errorf("GITHUB_PATH = %q, want empty (nothing appended on a checksum mismatch)", data) - } -} - // TestReleaseCrossCompileRunsInBazelPureLane pins the release-target // cross-compilation gate into bazel.yml's pure-Go lane, whose job.status // ci-gate requires (BAZEL_PURE), and keeps the retired pr.yml job from @@ -1236,19 +988,13 @@ func TestGoCacheOwnershipTopology(t *testing.T) { assertGoCacheInventory(t, workflows["main.yml"].job(t, "test-windows"), []goCacheStep{ mainRestoreModuleCache(), mainRestoreBuildCacheDayBucketedIf("non-race", ""), saveModuleCache(), saveBuildCacheDayBucketed("non-race"), }) - assertGoCacheInventory(t, workflows["main.yml"].job(t, "pr-lint-wrapper"), []goCacheStep{ - restoreModuleCache(), mainRestoreGolangciCache(), saveGolangciCache(), - }) - assertGoCacheInventory(t, workflows["main.yml"].job(t, "go-vet-cache"), []goCacheStep{ - mainRestoreModuleCache(), mainRestoreVetCache(), saveVetCache(), - }) // F4.7: seeds the module and xcompile caches pr.yml's windows-test-binaries // job only restores (PR workflows never save). assertGoCacheInventory(t, workflows["main.yml"].job(t, "windows-test-binaries-cache"), []goCacheStep{ mainRestoreModuleCache(), mainRestoreXCompileCache(), saveModuleCacheAfterFailure(), saveXCompileCache(), }) // F7b: race/non-race GOCACHE saves for same-repo PR consumers - // (scripts-go-checks, pr-preflight-platforms, check-doc-freshness-platforms) + // (pr-preflight-platforms, check-doc-freshness-platforms) // that run on Blacksmith, whose cache namespace is invisible to // GitHub-hosted build-artifacts; its github venue leg seeds the fork // path's race cache (build-artifacts seeds its non-race one). @@ -1275,11 +1021,6 @@ func TestGoCacheOwnershipTopology(t *testing.T) { assertGoCacheInventory(t, workflows["pr.yml"].job(t, "pr-core-wrapper"), []goCacheStep{ restoreModuleCache(), restoreBuildCache("race"), }) - // D2 step 3: PR Core's go vet; read-only like PR Core. - assertGoCacheInventory(t, workflows["pr.yml"].job(t, "scripts-go-checks"), []goCacheStep{ - restoreModuleCache(), restoreVetCache(), - }) - assertGoCacheInventory(t, workflows["pr.yml"].job(t, "pr-lint-wrapper"), []goCacheStep{restoreModuleCache(), restoreGolangciCache()}) assertGoCacheInventory(t, workflows["pr.yml"].job(t, "worktree-remove-windows"), []goCacheStep{ restoreModuleCache(), restoreBuildCache("non-race"), }) @@ -1322,7 +1063,7 @@ func TestGoCacheOwnershipTopology(t *testing.T) { assertNoUnmanagedGoCacheSteps(t, workflows, map[string]map[string]bool{ "main.yml": { "build-artifacts": true, "test": true, "test-windows": true, - "pr-lint-wrapper": true, "go-vet-cache": true, "windows-test-binaries-cache": true, "blacksmith-go-build-cache": true, + "windows-test-binaries-cache": true, "blacksmith-go-build-cache": true, "blacksmith-macos-go-build-cache": true, // F7c: blacksmith-setup-go-cache's own restore/save pair seeds the // self-defined blacksmith-sg-v1- cache namespace every advisory @@ -1334,9 +1075,9 @@ func TestGoCacheOwnershipTopology(t *testing.T) { "blacksmith-setup-go-cache": true, }, "pr.yml": { - "build-artifacts": true, "pr-core-wrapper": true, "scripts-go-checks": true, "worktree-remove-windows": true, + "build-artifacts": true, "pr-core-wrapper": true, "worktree-remove-windows": true, "check-doc-freshness-platforms": true, "pr-preflight-platforms": true, "advisory-reports": true, - "pr-lint-wrapper": true, "windows-test-binaries": true, + "windows-test-binaries": true, "test-windows-liveness": true, }, "pr-risk.yml": {"build-embedded": true}, @@ -1418,8 +1159,6 @@ func TestGoCacheOwnershipTopology(t *testing.T) { assertGoCacheWriter(t, workflows["main.yml"], "test", mainBlacksmithMacOSTestRunsOn, "Save race Go build cache", failureSurvivingCacheSaveCondition(cacheMissCondition(goBuildCacheRestoreID("race")))) assertGoCacheWriter(t, workflows["main.yml"], "test-windows", mainVenueBlacksmithWindows4vcpuRunsOn, "Save Go module cache", cacheMissCondition(goModuleCacheRestoreID)) assertGoCacheWriter(t, workflows["main.yml"], "test-windows", mainVenueBlacksmithWindows4vcpuRunsOn, "Save non-race Go build cache", cacheMissCondition(goBuildCacheRestoreID("non-race"))) - assertGoCacheWriter(t, workflows["main.yml"], "pr-lint-wrapper", mainVenueBlacksmithUbuntu4vcpuRunsOn, "Save golangci-lint cache", failureSurvivingCacheSaveCondition(cacheMissCondition(golangciCacheRestoreID()))) - assertGoCacheWriter(t, workflows["main.yml"], "go-vet-cache", mainVenueBlacksmithUbuntu4vcpuRunsOn, "Save vet Go build cache", failureSurvivingCacheSaveCondition(cacheMissCondition(goVetCacheRestoreID()))) assertGoCacheWriter(t, workflows["main.yml"], "blacksmith-go-build-cache", mainBlacksmithGoBuildCacheRunsOn, "Save race Go build cache", saveBuildCacheAfterFailureIf("race", "matrix.flavor == 'race'").ifCondition) assertGoCacheWriter(t, workflows["main.yml"], "blacksmith-go-build-cache", mainBlacksmithGoBuildCacheRunsOn, "Save non-race Go build cache", saveBuildCacheAfterFailureIf("non-race", "matrix.flavor == 'non-race'").ifCondition) assertGoCacheWriter(t, workflows["main.yml"], "blacksmith-macos-go-build-cache", mainBlacksmithMacOSGoBuildCacheRunsOn, "Save Go module cache", saveModuleCacheAfterFailure().ifCondition) @@ -1703,7 +1442,7 @@ func goBuildCacheKey(profile string) string { // goCacheDateSuffix is the key suffix for the day-bucketed caches saved by // main.yml's push-to-main-only Blacksmith seeders (blacksmith-go-build-cache, -// go-vet-cache, test-windows): bounded by go.sum content plus a UTC calendar +// test-windows): bounded by go.sum content plus a UTC calendar // day, not by commit sha, so an ordinary day of push traffic cannot exhaust // Blacksmith's 25 GB/week/repo cache quota the way a brand-new multi-GB entry // per push did (F7b review fix S7; measured ~1.74-2.4GB/push under the old @@ -1830,88 +1569,6 @@ func saveBuildCacheAfterFailureIf(profile, condition string) goCacheStep { return step } -// The go-vet-cache family (F5.3): a dedicated, go-vet-only GOCACHE restored -// by pr.yml's scripts-go-checks vet leg and warmed by main.yml's go-vet-cache -// job on push. There is deliberately no non-race-build-cache fallback restore -// key (SF-5): actions/cache versions each entry by its path list, and that -// cache saves to a different path, so a prefix fallback naming it could never -// hit. -const goVetCacheSchema = "v1" - -func goVetCachePrefix() string { - return "beads-go-vet-" + goVetCacheSchema + "-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-" + goBaseTag + "-vet-" -} - -func goVetCacheKey() string { return goVetCachePrefix() + "${{ github.sha }}" } - -// goVetCacheDayBucketedKey is goVetCacheKey with the S7 day-bucketed suffix -// (see goCacheDateSuffix), for main.yml's go-vet-cache seeder. -func goVetCacheDayBucketedKey() string { return goVetCachePrefix() + goCacheDateSuffix() } - -func goVetCacheRestoreKeys() string { return goVetCachePrefix() } - -func goVetCachePath() string { return "${{ runner.temp }}/go-cache/vet" } - -func goVetCacheRestoreID() string { return "restore-vet-go-build-cache" } - -func restoreVetCache() goCacheStep { - return goCacheStep{name: "Restore vet Go build cache", family: cacheRestoreActionFamily, key: goVetCacheKey(), restoreKeys: goVetCacheRestoreKeys(), path: goVetCachePath(), ifCondition: "matrix.check == 'vet'"} -} - -func mainRestoreVetCache() goCacheStep { - step := restoreVetCache() - step.id = goVetCacheRestoreID() - step.ifCondition = "" - step.key = goVetCacheDayBucketedKey() - return step -} - -func saveVetCache() goCacheStep { - return goCacheStep{name: "Save vet Go build cache", family: cacheSaveActionFamily, key: goVetCacheDayBucketedKey(), path: goVetCachePath(), ifCondition: failureSurvivingCacheSaveCondition(cacheMissCondition(goVetCacheRestoreID()))} -} - -// The golangci-lint cache family (F5.2): one cache per PR Lint matrix leg, -// keyed on file content and the leg. A miss on the exact content key falls -// back to the leg's prefix (its most recent entry); golangci-lint revalidates -// a restored cache against the current config and inputs, so a stale restore -// cannot mask an issue. pr.yml only ever restores; main.yml's matrix is the -// only saver. The key deliberately omits the commit sha (SF-4): with it, -// every green push to main saved a new multi-GB entry per leg regardless of -// whether lint's inputs changed; keying on content alone means a save only -// happens when .golangci.yml or go.sum change. Only the native leg's path -// includes the lint GOCACHE (SF-4): the windows/darwin legs cross-compile and -// their GOCACHE is large relative to the small golangci-lint result cache -// that is the part actually worth keeping warm for them. -func golangciCachePrefix() string { - return "beads-golangci-v1-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-v2.10.1-${{ matrix.target }}-" -} - -func golangciCacheKey() string { - return golangciCachePrefix() + "${{ hashFiles('.golangci.yml', 'go.sum') }}" -} - -func golangciCacheRestoreKeys() string { return golangciCachePrefix() } - -func golangciCachePath() string { - return "~/.cache/golangci-lint\n${{ matrix.target == 'native' && format('{0}/go-cache/lint-{1}', runner.temp, matrix.target) || '' }}\n" -} - -func golangciCacheRestoreID() string { return "restore-golangci-lint-cache" } - -func restoreGolangciCache() goCacheStep { - return goCacheStep{name: "Restore golangci-lint cache", family: cacheRestoreActionFamily, key: golangciCacheKey(), restoreKeys: golangciCacheRestoreKeys(), path: golangciCachePath()} -} - -func mainRestoreGolangciCache() goCacheStep { - step := restoreGolangciCache() - step.id = golangciCacheRestoreID() - return step -} - -func saveGolangciCache() goCacheStep { - return goCacheStep{name: "Save golangci-lint cache", family: cacheSaveActionFamily, key: golangciCacheKey(), path: golangciCachePath(), ifCondition: failureSurvivingCacheSaveCondition(cacheMissCondition(golangciCacheRestoreID()))} -} - // The Windows cross-compile GOCACHE (F4.4) is a separate cache dimension from // goBuildCache*'s base/race/non-race profiles: it is never shared with a // native build on the runner's own OS, so it gets its own key shape (no @@ -2140,8 +1797,8 @@ type ciWorkflowMatrix struct { Check []string `yaml:"check"` Group []string `yaml:"group"` // Venue and Flavor back main.yml's "venue matrix" (F7b spec §4) push-to- - // main-only Blacksmith cache savers: pr-lint-wrapper/go-vet-cache/ - // test-windows's `venue: [blacksmith, github]` and + // main-only Blacksmith cache savers: test-windows's + // `venue: [blacksmith, github]` and // blacksmith-go-build-cache's `flavor: [race, non-race]`. Pinned by // TestBlacksmithSaverVenueAndFlavorMatricesAreComplete (F7b review B2/S7 // follow-up) so dropping either leg from these lists -- silently losing @@ -2588,7 +2245,7 @@ const mainWindowsTestBinariesCacheRunsOn = "${{ matrix.runner == 'blacksmith' && // F7b: venue-matrix runs-on for main.yml jobs that seed a Blacksmith GOCACHE // alongside their GitHub-hosted seed ("venue: [blacksmith, github]"): -// pr-lint-wrapper, go-vet-cache, test-windows, blacksmith-go-build-cache and +// test-windows, blacksmith-go-build-cache and // blacksmith-macos-go-build-cache. const mainVenueBlacksmithUbuntu4vcpuRunsOn = "${{ matrix.venue == 'blacksmith' && 'blacksmith-4vcpu-ubuntu-2404' || 'ubuntu-latest' }}" const mainVenueBlacksmithWindows4vcpuRunsOn = "${{ matrix.venue == 'blacksmith' && 'blacksmith-4vcpu-windows-2025' || 'windows-latest' }}" @@ -2868,12 +2525,6 @@ func TestSameRepoBlacksmithRunners(t *testing.T) { "advisory-reports": sameRepoBlacksmith2vcpu, "check-doc-flags": sameRepoBlacksmith4vcpu, "windows-test-binaries": sameRepoBlacksmith8vcpu, - // F7b: scripts-go-checks/pr-lint-wrapper moved to same-repo - // Blacksmith now that main.yml's blacksmith-go-build-cache and - // pr-lint-wrapper `venue` matrix seed their Blacksmith-selection - // GOCACHEs. - "scripts-go-checks": sameRepoBlacksmith4vcpu, - "pr-lint-wrapper": sameRepoBlacksmith4vcpu, // F7b: same-repo Blacksmith Windows (blacksmith-*vcpu-windows-2025, // public beta). test-windows-liveness and worktree-remove-windows are // sized at 4vcpu (a build plus a focused test run each); @@ -2919,17 +2570,15 @@ func TestSameRepoBlacksmithRunners(t *testing.T) { "main.yml": { "blacksmith-setup-go-cache": "${{ 'blacksmith-4vcpu-ubuntu-2404' }}", "windows-test-binaries-cache": mainWindowsTestBinariesCacheRunsOn, - // F7b review (B2): these four push-to-main-only jobs seed the + // F7b review (B2): these push-to-main-only jobs seed the // Blacksmith-selection GOCACHEs same-repo PR/merge_group jobs - // above restore from (pr-lint-wrapper/go-vet-cache/test-windows's - // "venue" matrix, blacksmith-go-build-cache's own single leg). + // above restore from (test-windows's "venue" matrix, + // blacksmith-go-build-cache's own single leg). // Each is additionally guarded by a job-level // github.event_name == 'push' && github.ref == // 'refs/heads/main' && github.repository == 'gastownhall/beads' // if: (TestBlacksmithSaverJobsGuardedAgainstPullRequest below), // matching F7c's own saver convention. - "pr-lint-wrapper": mainVenueBlacksmithUbuntu4vcpuRunsOn, - "go-vet-cache": mainVenueBlacksmithUbuntu4vcpuRunsOn, "test-windows": mainVenueBlacksmithWindows4vcpuRunsOn, "blacksmith-go-build-cache": mainBlacksmithGoBuildCacheRunsOn, // Same guard, seeding pr.yml's same-repo macOS legs. diff --git a/scripts/embedded_shard_count_test.go b/scripts/embedded_shard_count_test.go index 3af9b1469..9820ed0ab 100644 --- a/scripts/embedded_shard_count_test.go +++ b/scripts/embedded_shard_count_test.go @@ -65,7 +65,7 @@ func bazelEmbeddedStorageShardCount(t *testing.T) int { // --check verifies only that the committed block names every discovered // test exactly once, failing with the exact command to fix it when a name // is missing, stale, or duplicated — run here so a drifted block fails `go -// test ./scripts/...` (and scripts-go-checks on fork PRs) instead of only +// test ./scripts/...` (//scripts:scripts_test under Bazel) instead of only // surfacing as a test silently never running in any shard. The legacy // blocks are deliberately excluded: both files document that their 20- and // 5-shard blocks are frozen (see .github/scripts/embedded-{cmd,storage}- diff --git a/scripts/nogo_lint_policy_test.go b/scripts/nogo_lint_policy_test.go new file mode 100644 index 000000000..7e06407ca --- /dev/null +++ b/scripts/nogo_lint_policy_test.go @@ -0,0 +1,109 @@ +package scripts_test + +import ( + "os" + "path/filepath" + "regexp" + "strings" + "testing" +) + +// Lint and vet are nogo (//tools/nogo): go test's vet checks plus the +// golangci-lint linters .golangci.yml enables, validated beside every Go +// compile. bazel.yml's required lanes gate them: natively inside +// `bazel test //... --config=ci` (test lane), and for the files only other +// platforms compile inside the pure lane's release cross-compile +// (//tools/bazel:release_cross, every release platform). These pin that +// wiring and that golangci-lint is gone from CI, hooks and make. + +// nogoBazelrcLines are .bazelrc's nogo configs, exactly. +var nogoBazelrcLines = []string{ + "build:nogo --@rules_go//go/config:race", + "build:nogo --keep_going", + "build:nogo --output_groups=nogo_fix", + "build:nogo-cross --keep_going", + "build:nogo-cross --output_groups=nogo_fix", +} + +func TestLintAndVetRunAsNogo(t *testing.T) { + root := sourceRepoRoot(t) + + rc := readPolicyFile(t, root, ".bazelrc") + var got []string + for _, line := range strings.Split(rc, "\n") { + if line = strings.TrimSpace(line); strings.HasPrefix(line, "build:nogo") { + got = append(got, line) + } + } + if !equalStrings(got, nogoBazelrcLines) { + t.Errorf(".bazelrc nogo configs = %q, want %q", got, nogoBazelrcLines) + } + // Every lane validates: a lane that skipped nogo would let findings in + // the files only it compiles (integration-tagged, pure) through. + if strings.Contains(rc, "run_validations") { + t.Error(".bazelrc turns nogo validation off for some lane") + } + + // The cross-platform half: the pure lane's release cross-compile builds + // every library and binary for every release platform, and nogo + // validates each compile (TestReleaseCrossCompileRunsInBazelPureLane + // pins the step). Nothing on that path may opt out of validation. + if script := readPolicyFile(t, root, "scripts/ci/bazel-release-cross-compile.sh"); strings.Contains(script, "run_validations") { + t.Error("scripts/ci/bazel-release-cross-compile.sh turns nogo validation off") + } + for _, line := range strings.Split(rc, "\n") { + if strings.HasPrefix(strings.TrimSpace(line), "build:release-cross") && strings.Contains(line, "validation") { + t.Errorf(".bazelrc %q changes validation for the release cross-compile", line) + } + } + + // No workflow installs or runs golangci-lint. + entries, err := os.ReadDir(filepath.Join(root, ".github", "workflows")) + if err != nil { + t.Fatal(err) + } + for _, e := range entries { + if !strings.HasSuffix(e.Name(), ".yml") { + continue + } + for name, j := range readCIWorkflow(t, e.Name()).Jobs { + for _, st := range j.Steps { + if strings.Contains(st.Run, "golangci") || strings.Contains(st.Uses, "golangci") { + t.Errorf("%s %s step %q runs golangci-lint; nogo replaces it", e.Name(), name, st.Name) + } + } + } + } + for _, gone := range []string{"scripts/ci/install-golangci-lint.sh", "scripts/ci/go-test-vet.sh"} { + if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(gone))); err == nil { + t.Errorf("%s is back; nogo replaces it", gone) + } + } + + // Local entrypoints: make, the pre-commit hook and pre-commit's config. + makefile := readPolicyFile(t, root, "Makefile") + for _, want := range []string{ + "ci-pr-lint:\n\t@./scripts/ci/pr-lint.sh\n", + "lint: ci-pr-lint\n", + "vet: lint\n", + "\t$(BAZEL) build --config=nogo -- $$packages\n", + } { + if !strings.Contains(makefile, want) { + t.Errorf("Makefile lacks %q", want) + } + } + for _, hook := range []string{".githooks/pre-commit", ".pre-commit-config.yaml"} { + body := readPolicyFile(t, root, hook) + if strings.Contains(body, "golangci-lint run") || strings.Contains(body, "golangci-lint@") || strings.Contains(body, "golangci/golangci-lint") { + t.Errorf("%s still runs golangci-lint", hook) + } + if !strings.Contains(body, "make lint-changed LINT_CHANGED_SCOPE=staged") { + t.Errorf("%s does not lint with nogo (make lint-changed LINT_CHANGED_SCOPE=staged)", hook) + } + } + // gofmt is //scripts/repochecks:fmt_test's, not the lint wrapper's + // (ga-96smfk.41). + if wrapper := readPolicyFile(t, root, "scripts/ci/pr-lint.sh"); regexp.MustCompile(`fmt-check|gofmt-bin`).MatchString(wrapper) { + t.Error("scripts/ci/pr-lint.sh runs gofmt; //scripts/repochecks:fmt_test gates formatting") + } +} diff --git a/scripts/pr-lint/BUILD.bazel b/scripts/pr-lint/BUILD.bazel index 906740444..a232d5557 100644 --- a/scripts/pr-lint/BUILD.bazel +++ b/scripts/pr-lint/BUILD.bazel @@ -16,9 +16,7 @@ go_binary( go_test( name = "pr-lint_test", srcs = ["main_test.go"], - data = ["//:.buildflags"], embed = [":pr-lint_lib"], - deps = ["//internal/testutil/bazeltest"], ) # --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- diff --git a/scripts/pr-lint/main.go b/scripts/pr-lint/main.go index 0f855347e..d2d18ad27 100644 --- a/scripts/pr-lint/main.go +++ b/scripts/pr-lint/main.go @@ -1,47 +1,57 @@ -// 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. +// 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 ( - "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" -) +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`) must keep -// running all three passes locally and from the pre-commit hook. +// 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 - 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 } @@ -51,34 +61,14 @@ 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 := exec.CommandContext(ctx, spec.name, spec.args...) //nolint:gosec // G204: the Bazel launcher with internally assembled arguments 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 { @@ -93,124 +83,81 @@ func run(args []string, dir string, environ []string, stdout, stderr io.Writer, fmt.Fprintln(stderr, "usage: go run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint") return 2 } - - rawTargets, _ := environmentValue(environ, "BD_LINT_TARGETS", runtime.GOOS == "windows") - targets, err := parseLintTargets(rawTargets) + caseInsensitive := runtime.GOOS == "windows" + targets, err := parseLintTargets(environmentValue(environ, "BD_LINT_TARGETS", caseInsensitive)) if err != nil { fmt.Fprintln(stderr, err) return 2 } - runNative := containsTarget(targets, "native") - runWindows := containsTarget(targets, "windows") - runDarwin := containsTarget(targets, "darwin") - - goPath, err := runner.lookPath("go") + bazel, err := findBazel(environmentValue(environ, "BAZEL", caseInsensitive), runner) 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) + fmt.Fprintln(stderr, err) return 1 } - effectiveEnv := buildEnvironment(environ, runtime.GOOS == "windows") - probeCtx, cancel := context.WithTimeout(context.Background(), goEnvTimeout) + 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() - 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 runWindows && !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 runDarwin && !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 runNative { - if code := runLintPass( - "golangci-lint (native)", - commandSpec{name: lintPath, args: argsForLint, dir: dir, env: nativeEnv}, - stdout, - stderr, - runner, - ); code != 0 { - return code - } - } - - if runWindows { - 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 !runDarwin { + err := runner.run(ctx, spec, stdout, stderr) + if err == nil { + fmt.Fprintf(stdout, "<== %s succeeded\n", label) return 0 } - if skipDarwin { - fmt.Fprintln(stdout, "==> golangci-lint (darwin/arm64, non-CGO) already covered by native pass") - return 0 + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + fmt.Fprintf(stderr, "<== %s exceeded %s\n", label, passTimeout) + return 1 } - - return runLintPass( - "golangci-lint (darwin/arm64, non-CGO)", - commandSpec{name: lintPath, args: argsForLint, dir: dir, env: darwinEnv}, - stdout, - stderr, - runner, - ) + 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 (the variable unset, or set to only whitespace and commas) -// selects all three, so the no-argument usage contract is unchanged. Any -// other token is an error: callers exit 2, matching the usage-error exit -// code above. +// 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, ",") { @@ -238,184 +185,16 @@ func containsTarget(targets []string, target string) bool { return false } -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) { +// 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 && environmentKeysEqual(key, wanted, caseInsensitive) { - return value, true + if ok && (key == wanted || caseInsensitive && strings.EqualFold(key, wanted)) { + return value } } - 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 + return "" } func processExitCode(err error) int { diff --git a/scripts/pr-lint/main_test.go b/scripts/pr-lint/main_test.go index 28e681cc4..120579baa 100644 --- a/scripts/pr-lint/main_test.go +++ b/scripts/pr-lint/main_test.go @@ -3,30 +3,18 @@ package main import ( "bytes" "context" - "encoding/json" "errors" "io" - "os" - "os/exec" - "path/filepath" "reflect" - "regexp" "runtime" "strings" "testing" - - "github.com/steveyegge/beads/internal/testutil/bazeltest" ) type recordingRunner struct { - paths map[string]string - goEnvOutput string - goEnvStderr string - goEnvErr error - runErrors []error - outputCommands []commandSpec - runCommands []commandSpec - outputFunc func(context.Context, commandSpec) ([]byte, []byte, error) + paths map[string]string + runErrors []error + runCommands []commandSpec } func (runner *recordingRunner) lookPath(name string) (string, error) { @@ -36,17 +24,9 @@ func (runner *recordingRunner) lookPath(name string) (string, error) { return "", errors.New("synthetic missing command") } -func (runner *recordingRunner) output(ctx context.Context, spec commandSpec) ([]byte, []byte, error) { - runner.outputCommands = append(runner.outputCommands, spec) - if runner.outputFunc != nil { - return runner.outputFunc(ctx, spec) - } - return []byte(runner.goEnvOutput), []byte(runner.goEnvStderr), runner.goEnvErr -} - func (runner *recordingRunner) run(_ context.Context, spec commandSpec, stdout, _ io.Writer) error { runner.runCommands = append(runner.runCommands, spec) - _, _ = io.WriteString(stdout, "synthetic lint output\n") + _, _ = io.WriteString(stdout, "synthetic bazel output\n") index := len(runner.runCommands) - 1 if index < len(runner.runErrors) { return runner.runErrors[index] @@ -66,643 +46,136 @@ func (err syntheticExitError) ExitCode() int { return err.code } -func TestRunUsesCanonicalNativeAndCrossTargetPasses(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "/tools/go", - "golangci-lint": "/tools/golangci-lint", - }, - goEnvOutput: `{"GOOS":"linux","CGO_ENABLED":"1"}`, - } - var stdout bytes.Buffer - var stderr bytes.Buffer - environ := []string{ - "PATH=/tools", - "BD_LINT_NEW_FROM_MERGE_BASE=origin/main", - } +func newRunner() *recordingRunner { + return &recordingRunner{paths: map[string]string{"bazel": "/bin/bazel"}} +} - code := run(nil, "/repo", environ, &stdout, &stderr, runner) - if code != 0 { - t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String()) +var nativeArgs = []string{"build", "--config=nogo", "//..."} + +func crossArgs(platforms string) []string { + return []string{"build", "--config=nogo-cross", "--//tools/bazel:release_platforms=" + platforms, "//tools/bazel:release_cross"} +} + +func commandArgs(runner *recordingRunner) [][]string { + var got [][]string + for _, spec := range runner.runCommands { + got = append(got, spec.args) } - wantProbes := 1 - if runtime.GOOS == "windows" { - // One native probe plus one per cross-target pass. - wantProbes = 3 + return got +} + +func TestRunsNativeThenTheWindowsAndDarwinCrossPass(t *testing.T) { + runner := newRunner() + var stdout, stderr bytes.Buffer + if code := run(nil, "/repo", []string{"PATH=/bin"}, &stdout, &stderr, runner); code != 0 { + t.Fatalf("run() = %d, stderr:\n%s", code, stderr.String()) } - if len(runner.outputCommands) != wantProbes { - t.Fatalf("go env calls = %d, want %d", len(runner.outputCommands), wantProbes) - } - wantGoArgs := []string{"env", "-json", "GOOS", "CGO_ENABLED", "GOROOT", "GOVERSION"} - if got := runner.outputCommands[0].args; !reflect.DeepEqual(got, wantGoArgs) { - t.Fatalf("go env args = %#v, want %#v", got, wantGoArgs) - } - if len(runner.runCommands) != 3 { - t.Fatalf("lint calls = %d, want 3", len(runner.runCommands)) - } - wantLintArgs := []string{ - "run", - "--config=.golangci.yml", - "--modules-download-mode=readonly", - "--timeout=5m", - "--build-tags=gms_pure_go", - "--new-from-merge-base=origin/main", - "./...", - } - for index, command := range runner.runCommands { - if command.name != "/tools/golangci-lint" { - t.Fatalf("lint call %d executable = %q", index, command.name) - } - if command.dir != "/repo" { - t.Fatalf("lint call %d dir = %q, want /repo", index, command.dir) - } - if !reflect.DeepEqual(command.args, wantLintArgs) { - t.Fatalf("lint call %d args = %#v, want %#v", index, command.args, wantLintArgs) + for _, spec := range runner.runCommands { + if spec.name != "/bin/bazel" || spec.dir != "/repo" { + t.Errorf("command %q in %q, want /bin/bazel in /repo", spec.name, spec.dir) } } - assertEnvironmentValue(t, runner.runCommands[0].env, "CGO_ENABLED", "1", false) - assertEnvironmentValue(t, runner.runCommands[0].env, "BEADS_BUILD_TAGS", "gms_pure_go", false) - assertEnvironmentValue(t, runner.runCommands[0].env, "GOFLAGS", "-tags=gms_pure_go", false) - assertEnvironmentValue(t, runner.runCommands[1].env, "GOOS", "windows", false) - assertEnvironmentValue(t, runner.runCommands[1].env, "GOARCH", "amd64", false) - assertEnvironmentValue(t, runner.runCommands[1].env, "CGO_ENABLED", "0", false) - assertEnvironmentValue(t, runner.runCommands[1].env, "GOWORK", "off", false) - assertEnvironmentValue(t, runner.runCommands[2].env, "GOOS", "darwin", false) - assertEnvironmentValue(t, runner.runCommands[2].env, "GOARCH", "arm64", false) - assertEnvironmentValue(t, runner.runCommands[2].env, "CGO_ENABLED", "0", false) - assertEnvironmentValue(t, runner.runCommands[2].env, "GOWORK", "off", false) - for _, heading := range []string{ - "==> golangci-lint (native)", - "==> golangci-lint (windows/amd64, non-CGO)", - "==> golangci-lint (darwin/arm64, non-CGO)", - } { - if !strings.Contains(stdout.String(), heading) { - t.Fatalf("missing lane heading %q in output:\n%s", heading, stdout.String()) + want := [][]string{nativeArgs, crossArgs("windows_amd64,darwin_arm64")} + if got := commandArgs(runner); !reflect.DeepEqual(got, want) { + t.Errorf("bazel commands = %q, want %q", got, want) + } + for _, label := range []string{"nogo (native)", "nogo (windows_amd64, darwin_arm64; non-cgo)"} { + if !strings.Contains(stdout.String(), "==> "+label+"\n") || !strings.Contains(stdout.String(), "<== "+label+" succeeded\n") { + t.Errorf("stdout lacks the %q pass markers:\n%s", label, stdout.String()) } } } -// TestRunHonorsBDLintTargetsSelection covers the pr.yml PR Lint matrix -// (F5.2): each leg sets BD_LINT_TARGETS to exactly one of native, windows or -// darwin, and only that pass must run. -func TestRunHonorsBDLintTargetsSelection(t *testing.T) { - for _, tc := range []struct { - targets string - wantRuns []string // GOOS set on the one command's env, in order +func TestBDLintTargetsSelectsPasses(t *testing.T) { + for _, c := range []struct { + targets string + want [][]string }{ - {"native", []string{"linux"}}, - {"windows", []string{"windows"}}, - {"darwin", []string{"darwin"}}, - {"native,windows", []string{"linux", "windows"}}, - {" windows , darwin ", []string{"windows", "darwin"}}, - {"windows,windows", []string{"windows"}}, + {"native", [][]string{nativeArgs}}, + {" darwin , darwin", [][]string{crossArgs("darwin_arm64")}}, + {"windows", [][]string{crossArgs("windows_amd64")}}, + // Order and repetition do not matter. + {"darwin,native,windows", [][]string{nativeArgs, crossArgs("windows_amd64,darwin_arm64")}}, + {" , ", [][]string{nativeArgs, crossArgs("windows_amd64,darwin_arm64")}}, } { - t.Run(tc.targets, func(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "/tools/go", - "golangci-lint": "/tools/golangci-lint", - }, - goEnvOutput: `{"GOOS":"linux","CGO_ENABLED":"1"}`, - } - var stdout, stderr bytes.Buffer - environ := []string{"PATH=/tools", "BD_LINT_TARGETS=" + tc.targets} - - code := run(nil, "/repo", environ, &stdout, &stderr, runner) - if code != 0 { - t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String()) - } - if len(runner.runCommands) != len(tc.wantRuns) { - t.Fatalf("lint calls = %d, want %d (%v); commands=%#v", len(runner.runCommands), len(tc.wantRuns), tc.wantRuns, runner.runCommands) - } - for index, wantGOOS := range tc.wantRuns { - if wantGOOS == "linux" { - // The native pass does not force a GOOS override. - if got, found := environmentValue(runner.runCommands[index].env, "GOOS", false); found && got != "" && got != "linux" { - t.Fatalf("native pass GOOS override = %q", got) - } - continue - } - assertEnvironmentValue(t, runner.runCommands[index].env, "GOOS", wantGOOS, false) - } - }) - } -} - -func TestRunDefaultsBDLintTargetsToAllThree(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "/tools/go", - "golangci-lint": "/tools/golangci-lint", - }, - goEnvOutput: `{"GOOS":"linux","CGO_ENABLED":"1"}`, - } - var stdout, stderr bytes.Buffer - // No BD_LINT_TARGETS at all, and a second case with it set but empty: - // both must run the full native+windows+darwin contract (the no-argument - // usage contract `make ci-pr-lint` and `bd preflight` depend on). - for _, environ := range [][]string{ - {"PATH=/tools"}, - {"PATH=/tools", "BD_LINT_TARGETS="}, - {"PATH=/tools", "BD_LINT_TARGETS= , ,"}, - } { - runner.runCommands = nil - code := run(nil, "/repo", environ, &stdout, &stderr, runner) - if code != 0 { - t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String()) + runner := newRunner() + var stdout, stderr bytes.Buffer + if code := run(nil, "/repo", []string{"BD_LINT_TARGETS=" + c.targets}, &stdout, &stderr, runner); code != 0 { + t.Fatalf("%q: run() = %d, stderr:\n%s", c.targets, code, stderr.String()) } - if len(runner.runCommands) != 3 { - t.Fatalf("env=%v: lint calls = %d, want 3", environ, len(runner.runCommands)) + if got := commandArgs(runner); !reflect.DeepEqual(got, c.want) { + t.Errorf("%q: bazel commands = %q, want %q", c.targets, got, c.want) } } } -func TestRunRejectsUnknownBDLintTarget(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "/tools/go", - "golangci-lint": "/tools/golangci-lint", - }, - goEnvOutput: `{"GOOS":"linux","CGO_ENABLED":"1"}`, - } +func TestRejectsUnknownTargetAndArguments(t *testing.T) { + runner := newRunner() var stdout, stderr bytes.Buffer - environ := []string{"PATH=/tools", "BD_LINT_TARGETS=native,solaris"} - - code := run(nil, "/repo", environ, &stdout, &stderr, runner) - if code != 2 { - t.Fatalf("run exit = %d, want 2; stderr=%s", code, stderr.String()) + if code := run(nil, "/repo", []string{"BD_LINT_TARGETS=native,linux"}, &stdout, &stderr, runner); code != 2 { + t.Errorf("unknown target: run() = %d, want 2", code) + } + if !strings.Contains(stderr.String(), `unknown target "linux"`) { + t.Errorf("stderr = %q, want the unknown target named", stderr.String()) + } + if code := run([]string{"./..."}, "/repo", nil, &stdout, &stderr, runner); code != 2 { + t.Errorf("arguments: run() = %d, want 2", code) } if len(runner.runCommands) != 0 { - t.Fatalf("an unknown target must run nothing, got %d lint calls", len(runner.runCommands)) - } - if !strings.Contains(stderr.String(), "solaris") { - t.Fatalf("missing the unknown target in the diagnostic: %q", stderr.String()) + t.Errorf("ran %+v after a usage error", runner.runCommands) } } -func TestParseLintTargets(t *testing.T) { - for _, tc := range []struct { - raw string - want []string - wantErr bool - }{ - {"", []string{"native", "windows", "darwin"}, false}, - {" , ", []string{"native", "windows", "darwin"}, false}, - {"native", []string{"native"}, false}, - {"native,darwin", []string{"native", "darwin"}, false}, - {" native , darwin ", []string{"native", "darwin"}, false}, - {"bogus", nil, true}, - {"native,bogus", nil, true}, - } { - t.Run(tc.raw, func(t *testing.T) { - got, err := parseLintTargets(tc.raw) - if tc.wantErr { - if err == nil { - t.Fatalf("parseLintTargets(%q) = %v, want an error", tc.raw, got) - } - return - } - if err != nil { - t.Fatalf("parseLintTargets(%q) unexpected error: %v", tc.raw, err) - } - if !reflect.DeepEqual(got, tc.want) { - t.Fatalf("parseLintTargets(%q) = %#v, want %#v", tc.raw, got, tc.want) - } - }) - } -} - -func TestLintArgsKeepsHostileMergeBaseInOneArgument(t *testing.T) { - mergeBase := `origin/main; printf injected >&2` - args := lintArgs(mergeBase) - want := "--new-from-merge-base=" + mergeBase - count := 0 - for _, arg := range args { - if arg == want { - count++ - } - } - if count != 1 { - t.Fatalf("hostile merge base occurrence count = %d, want 1; args=%#v", count, args) - } - if len(args) != 7 { - t.Fatalf("hostile merge base changed argv cardinality: %#v", args) - } -} - -func TestRunSkipsDuplicateNativeWindowsNonCGOPass(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "go", - "golangci-lint": "golangci-lint", - }, - goEnvOutput: `{"GOOS":"windows","CGO_ENABLED":"0"}`, - } - var stdout bytes.Buffer - var stderr bytes.Buffer - - code := run(nil, "/repo", []string{"CGO_ENABLED=0"}, &stdout, &stderr, runner) - if code != 0 { - t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String()) - } - // The native pass already covers windows/non-CGO, so only the native and - // darwin cross-target passes remain. - if len(runner.runCommands) != 2 { - t.Fatalf("lint calls = %d, want 2", len(runner.runCommands)) - } - assertEnvironmentValue(t, runner.runCommands[1].env, "GOOS", "darwin", false) - if !strings.Contains(stdout.String(), "==> golangci-lint (windows/amd64, non-CGO) already covered by native pass") { - t.Fatalf("missing duplicate-pass diagnostic:\n%s", stdout.String()) - } -} - -func TestRunSkipsDuplicateNativeDarwinNonCGOPass(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "go", - "golangci-lint": "golangci-lint", - }, - goEnvOutput: `{"GOOS":"darwin","CGO_ENABLED":"0"}`, - } - var stdout bytes.Buffer - var stderr bytes.Buffer - - code := run(nil, "/repo", []string{"CGO_ENABLED=0"}, &stdout, &stderr, runner) - if code != 0 { - t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String()) - } - // The native pass already covers darwin/non-CGO, so only the native and - // windows cross-target passes remain. - if len(runner.runCommands) != 2 { - t.Fatalf("lint calls = %d, want 2", len(runner.runCommands)) - } - assertEnvironmentValue(t, runner.runCommands[1].env, "GOOS", "windows", false) - if !strings.Contains(stdout.String(), "==> golangci-lint (darwin/arm64, non-CGO) already covered by native pass") { - t.Fatalf("missing duplicate-pass diagnostic:\n%s", stdout.String()) - } -} - -func TestOSProcessRunnerSeparatesStdoutAndStderr(t *testing.T) { - const helperEnvironment = "BEADS_PR_LINT_OUTPUT_HELPER" - if os.Getenv(helperEnvironment) == "1" { - _, _ = io.WriteString(os.Stdout, `{"GOOS":"windows","CGO_ENABLED":"0"}`) - _, _ = io.WriteString(os.Stderr, "synthetic benign go env warning\n") - os.Exit(0) - } - - stdout, stderr, err := (osProcessRunner{}).output(context.Background(), commandSpec{ - name: os.Args[0], - args: []string{"-test.run=^TestOSProcessRunnerSeparatesStdoutAndStderr$"}, - env: append(os.Environ(), helperEnvironment+"=1"), - }) - if err != nil { - t.Fatalf("output helper failed: %v; stderr=%s", err, stderr) - } - if got, want := string(stdout), `{"GOOS":"windows","CGO_ENABLED":"0"}`; got != want { - t.Fatalf("stdout = %q, want JSON-only %q", got, want) - } - if got, want := string(stderr), "synthetic benign go env warning\n"; got != want { - t.Fatalf("stderr = %q, want warning-only %q", got, want) - } -} - -func TestRunParsesGoEnvStdoutWhenSuccessfulCommandWarnsOnStderr(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "go", - "golangci-lint": "golangci-lint", - }, - goEnvOutput: `{"GOOS":"windows","CGO_ENABLED":"0"}`, - goEnvStderr: "synthetic benign go env warning\n", - } - var stdout bytes.Buffer - var stderr bytes.Buffer - - code := run(nil, "/repo", []string{"CGO_ENABLED=0"}, &stdout, &stderr, runner) - if code != 0 { - t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String()) - } - if !strings.Contains(stderr.String(), "synthetic benign go env warning") { - t.Fatalf("go env stderr warning was not surfaced: %q", stderr.String()) - } - if strings.Contains(stderr.String(), "parse native Go target") { - t.Fatalf("go env stderr corrupted stdout JSON parsing: %q", stderr.String()) - } - if len(runner.runCommands) != 2 { - t.Fatalf("lint calls = %d, want the native Windows/non-CGO pass plus the darwin cross-lint", len(runner.runCommands)) - } -} - -func TestRunPreservesNativeLintExitCodeAndStops(t *testing.T) { - runner := &recordingRunner{ - paths: map[string]string{ - "go": "go", - "golangci-lint": "golangci-lint", - }, - goEnvOutput: `{"GOOS":"linux","CGO_ENABLED":"1"}`, - runErrors: []error{syntheticExitError{code: 23}}, - } - var stdout bytes.Buffer - var stderr bytes.Buffer - - code := run(nil, "/repo", nil, &stdout, &stderr, runner) - if code != 23 { - t.Fatalf("run exit = %d, want 23", code) +func TestStopsAtTheFirstFailingPassWithItsExitCode(t *testing.T) { + runner := newRunner() + runner.runErrors = []error{syntheticExitError{code: 3}} + var stdout, stderr bytes.Buffer + if code := run(nil, "/repo", nil, &stdout, &stderr, runner); code != 3 { + t.Fatalf("run() = %d, want the native pass's 3", code) } if len(runner.runCommands) != 1 { - t.Fatalf("lint calls = %d, want only failed native pass", len(runner.runCommands)) + t.Errorf("ran %d passes after the native pass failed", len(runner.runCommands)) } - if !strings.Contains(stderr.String(), "golangci-lint (native) failed") { - t.Fatalf("missing native failure diagnostic:\n%s", stderr.String()) + if !strings.Contains(stderr.String(), "<== nogo (native) failed") { + t.Errorf("stderr = %q, want the failed pass named", stderr.String()) + } + runner = newRunner() + runner.runErrors = []error{nil, errors.New("no exit code")} + stderr.Reset() + if code := run(nil, "/repo", nil, &stdout, &stderr, runner); code != 1 { + t.Fatalf("run() = %d, want 1 for a failure without an exit code", code) } } -func TestRunFailsClearlyWhenCapabilityIsMissing(t *testing.T) { - for _, missing := range []string{"go", "golangci-lint"} { - t.Run(missing, func(t *testing.T) { - paths := map[string]string{ - "go": "go", - "golangci-lint": "golangci-lint", - } - delete(paths, missing) - runner := &recordingRunner{paths: paths} - var stderr bytes.Buffer - - if code := run(nil, "/repo", nil, io.Discard, &stderr, runner); code == 0 { - t.Fatal("missing capability unexpectedly passed") - } - if !strings.Contains(strings.ToLower(stderr.String()), missing) || !strings.Contains(stderr.String(), "PATH") { - t.Fatalf("missing %s diagnostic is unclear: %q", missing, stderr.String()) - } - }) +func TestBazelLookup(t *testing.T) { + runner := &recordingRunner{paths: map[string]string{"bazelisk": "/bin/bazelisk", "custom": "/opt/custom"}} + var stdout, stderr bytes.Buffer + // No bazel on PATH: bazelisk is the same launcher. + if code := run(nil, "/repo", []string{"BD_LINT_TARGETS=native"}, &stdout, &stderr, runner); code != 0 || runner.runCommands[0].name != "/bin/bazelisk" { + t.Fatalf("run() = %d with %+v, want bazelisk", code, runner.runCommands) + } + // $BAZEL names the launcher, as the Makefile's BAZEL does. + if code := run(nil, "/repo", []string{"BD_LINT_TARGETS=native", "BAZEL=custom"}, &stdout, &stderr, runner); code != 0 || runner.runCommands[1].name != "/opt/custom" { + t.Fatalf("run() = %d with %+v, want $BAZEL", code, runner.runCommands) + } + missing := &recordingRunner{} + stderr.Reset() + if code := run(nil, "/repo", nil, &stdout, &stderr, missing); code != 1 || !strings.Contains(stderr.String(), "bazel not found") { + t.Fatalf("run() = %d, stderr %q; want a missing-bazel error", code, stderr.String()) } } -func TestBuildEnvironmentMatchesBuildFlagsContract(t *testing.T) { - environ := []string{ - "PATH=/tools", - "CGO_ENABLED=0", - "GOFLAGS=-mod=readonly", - "BEADS_BUILD_TAGS=stale", +func TestEnvironmentLookupIsCaseInsensitiveOnlyOnWindows(t *testing.T) { + runner := newRunner() + var stdout, stderr bytes.Buffer + if code := run(nil, "/repo", []string{"bd_lint_targets=native"}, &stdout, &stderr, runner); code != 0 { + t.Fatalf("run() = %d", code) } - got := buildEnvironment(environ, false) - assertEnvironmentValue(t, got, "CGO_ENABLED", "0", false) - assertEnvironmentValue(t, got, "GOFLAGS", "-mod=readonly -tags=gms_pure_go", false) - assertEnvironmentValue(t, got, "BEADS_BUILD_TAGS", "gms_pure_go", false) - - defaulted := buildEnvironment([]string{"CGO_ENABLED="}, false) - assertEnvironmentValue(t, defaulted, "CGO_ENABLED", "1", false) - assertEnvironmentValue(t, defaulted, "GOFLAGS", "-tags=gms_pure_go", false) -} - -func TestBuildEnvironmentNormalizesWindowsKeyIdentity(t *testing.T) { - environ := []string{ - "cgo_enabled=0", - "CGO_ENABLED=1", - "GoFlags=-mod=readonly", - "GOFLAGS=-trimpath", - "beads_build_tags=stale", - "BEADſ_BUILD_TAGS=near-collision", - "MALFORMED", - "=C:=C:\\source\\beads", + wantPasses := 2 // the lower-case name is a different variable: every pass + if runtime.GOOS == "windows" { + wantPasses = 1 } - got := buildEnvironment(environ, true) - - assertSingleLogicalEnvironmentKey(t, got, "CGO_ENABLED") - assertSingleLogicalEnvironmentKey(t, got, "GOFLAGS") - assertSingleLogicalEnvironmentKey(t, got, "BEADS_BUILD_TAGS") - assertEnvironmentValue(t, got, "CGO_ENABLED", "1", true) - assertEnvironmentValue(t, got, "GOFLAGS", "-trimpath -tags=gms_pure_go", true) - assertEnvironmentValue(t, got, "BEADS_BUILD_TAGS", "gms_pure_go", true) - for _, preserved := range []string{"BEADſ_BUILD_TAGS=near-collision", "MALFORMED", "=C:=C:\\source\\beads"} { - if !containsExact(got, preserved) { - t.Fatalf("environment entry %q was not preserved in %#v", preserved, got) - } - } -} - -func TestRunRejectsArguments(t *testing.T) { - var stderr bytes.Buffer - code := run([]string{"--unexpected"}, "/repo", nil, io.Discard, &stderr, &recordingRunner{}) - if code != 2 { - t.Fatalf("run exit = %d, want 2", code) - } - if !strings.Contains(stderr.String(), "usage:") { - t.Fatalf("missing usage diagnostic: %q", stderr.String()) - } -} - -func assertEnvironmentValue(t *testing.T, environ []string, key, want string, caseInsensitive bool) { - t.Helper() - got, found := environmentValue(environ, key, caseInsensitive) - if !found || got != want { - t.Fatalf("%s = %q, found=%v, want %q; env=%#v", key, got, found, want, environ) - } -} - -func assertSingleLogicalEnvironmentKey(t *testing.T, environ []string, wanted string) { - t.Helper() - count := 0 - for _, entry := range environ { - key, _, ok := strings.Cut(entry, "=") - if ok && strings.ToLower(key) == strings.ToLower(wanted) { - count++ - } - } - if count != 1 { - t.Fatalf("logical environment key %s occurs %d times in %#v", wanted, count, environ) - } -} - -func containsExact(values []string, wanted string) bool { - for _, value := range values { - if value == wanted { - return true - } - } - return false -} - -func TestRunUsesEachWindowsPassSelectedToolchain(t *testing.T) { - if runtime.GOOS != "windows" { - t.Skip("Windows auto-selection creates the extra process") - } - nativeRoot, crossRoot := filepath.Join(t.TempDir(), "workspace SDK"), filepath.Join(t.TempDir(), "module SDK") - environ := []string{"Path=original", "GOTOOLCHAIN=auto", "GOROOT=custom", "GOWORK=workspace"} - runner := &recordingRunner{paths: map[string]string{"go": "launcher", "golangci-lint": "lint"}} - var probeContext context.Context - runner.outputFunc = func(ctx context.Context, spec commandSpec) ([]byte, []byte, error) { - if probeContext != nil && ctx != probeContext { - t.Fatal("toolchain discovery escaped the shared probe deadline") - } - probeContext = ctx - selected := nativeGoEnvironment{GOOS: "windows", CGOEnabled: "1", GOROOT: nativeRoot, GOVERSION: "go1.26.7"} - if work, _ := environmentValue(spec.env, "GOWORK", true); work == "off" { - selected.GOROOT, selected.GOVERSION, selected.CGOEnabled = crossRoot, "go1.26.5", "0" - } - assertEnvironmentValue(t, spec.env, "PATH", "original", true) - if spec.name != "launcher" { - if spec.name != filepath.Join(selected.GOROOT, "bin", "go.exe") { - t.Fatalf("candidate = %q, root = %q", spec.name, selected.GOROOT) - } - assertEnvironmentValue(t, spec.env, "GOTOOLCHAIN", "local", true) - } - data, err := json.Marshal(selected) - return data, nil, err - } - var stderr bytes.Buffer - if code := run(nil, ".", environ, io.Discard, &stderr, runner); code != 0 || len(runner.runCommands) != 2 { - t.Fatalf("run = %d, passes = %d, stderr=%s", code, len(runner.runCommands), &stderr) - } - for index, root := range []string{nativeRoot, crossRoot} { - env := runner.runCommands[index].env - assertSingleLogicalEnvironmentKey(t, env, "PATH") - assertEnvironmentValue(t, env, "PATH", filepath.Join(root, "bin")+";original", true) - assertEnvironmentValue(t, env, "GOTOOLCHAIN", "auto", true) - assertEnvironmentValue(t, env, "GOROOT", "custom", true) - } - if environ[0] != "Path=original" { - t.Fatal("lint changed the caller's PATH") - } -} - -func TestSelectedGoFallsBackForNonstandardSDK(t *testing.T) { - root := t.TempDir() - selected := nativeGoEnvironment{GOROOT: root, GOVERSION: "go1.26.7"} - environ := []string{"Path=original", "GOTOOLCHAIN=auto", "GOROOT=custom"} - t.Run("PATH separator", func(t *testing.T) { - unsupported := selected - unsupported.GOROOT += string(os.PathListSeparator) + "SDK" - data, err := json.Marshal(unsupported) - if err != nil { - t.Fatal(err) - } - var diagnostic bytes.Buffer - got := preferSelectedGo(context.Background(), ".", environ, unsupported, &diagnostic, &recordingRunner{goEnvOutput: string(data)}) - if !reflect.DeepEqual(got, environ) || !strings.Contains(diagnostic.String(), "retaining original Go PATH") { - t.Fatalf("fallback env=%v, diagnostic=%s", got, &diagnostic) - } - }) - for _, tc := range []struct { - name, root, version string - err error - }{ - {"missing executable", root, "go1.26.7", os.ErrNotExist}, - {"wrong version", root, "go1.26.5", nil}, - {"different root", filepath.Join(root, "other"), "go1.26.7", nil}, - {"deadline", root, "go1.26.7", context.DeadlineExceeded}, - } { - t.Run(tc.name, func(t *testing.T) { - data, err := json.Marshal(nativeGoEnvironment{GOROOT: tc.root, GOVERSION: tc.version}) - if err != nil { - t.Fatal(err) - } - runner := &recordingRunner{goEnvOutput: string(data), goEnvErr: tc.err} - var diagnostic bytes.Buffer - got := preferSelectedGo(context.Background(), ".", environ, selected, &diagnostic, runner) - if !reflect.DeepEqual(got, environ) || !strings.Contains(diagnostic.String(), "retaining original Go PATH") { - t.Fatalf("fallback env=%v, diagnostic=%s", got, &diagnostic) - } - }) - } -} - -func TestSelectedGoIsResolvedByLintChild(t *testing.T) { - const marker = "BEADS_SELECTED_GO_PROBE" - if expected := os.Getenv(marker); expected != "" { - resolved, err := exec.LookPath("go") - if err != nil { - t.Fatal(err) - } - got, gotErr := os.Stat(resolved) - want, wantErr := os.Stat(expected) - if gotErr != nil || wantErr != nil || !os.SameFile(got, want) { - t.Fatalf("child resolved %q, want identity of %q", resolved, expected) - } - return - } - if runtime.GOOS != "windows" { - t.Skip("Windows executable discovery") - } - root := t.TempDir() - selectedBin, oldBin := filepath.Join(root, "selected SDK", "bin"), filepath.Join(root, "old SDK", "bin") - for _, bin := range []string{selectedBin, oldBin} { - if err := os.MkdirAll(bin, 0o755); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(bin, "go.exe"), nil, 0o755); err != nil { - t.Fatal(err) - } - } - selected := nativeGoEnvironment{GOROOT: filepath.Dir(selectedBin), GOVERSION: "go1.26.7"} - data, err := json.Marshal(selected) - if err != nil { - t.Fatal(err) - } - environ := setEnvironment(os.Environ(), map[string]string{"PATH": oldBin, marker: filepath.Join(selectedBin, "go.exe")}, true) - cmd := exec.Command(os.Args[0], "-test.run=^TestSelectedGoIsResolvedByLintChild$") - cmd.Env = preferSelectedGo(context.Background(), root, environ, selected, io.Discard, &recordingRunner{goEnvOutput: string(data)}) - if output, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("lint child Go discovery: %v\n%s", err, output) - } -} - -// TestBuildFlagsContractMatchesShellSource reads .buildflags itself and pins -// the driver constants to it. TestBuildEnvironmentMatchesBuildFlagsContract -// pins today's values from the Go side only, so the two files could drift -// apart silently: buildEnvironment appends its own -tags value and overrides -// rather than merges, which means a tag added to .buildflags alone would be -// dropped by the driver with every existing test still green. -func TestBuildFlagsContractMatchesShellSource(t *testing.T) { - _, thisFile, _, ok := runtime.Caller(0) - if !ok { - t.Fatal("runtime.Caller(0) failed") - } - repoRoot := filepath.Dir(filepath.Dir(bazeltest.CallerDir(thisFile, "scripts/pr-lint"))) - path := filepath.Join(repoRoot, ".buildflags") - data, err := os.ReadFile(path) - if err != nil { - t.Fatalf("read %s: %v", path, err) - } - source := string(data) - - // export BEADS_BUILD_TAGS="gms_pure_go" - tags := regexp.MustCompile(`(?m)^export BEADS_BUILD_TAGS="([^"]*)"$`).FindStringSubmatch(source) - if tags == nil { - t.Fatalf(".buildflags no longer exports BEADS_BUILD_TAGS in the expected form:\n%s", source) - } - if tags[1] != beadsBuildTags { - t.Errorf("BEADS_BUILD_TAGS drift: .buildflags has %q, driver constant beadsBuildTags is %q; "+ - "buildEnvironment overrides rather than merges -tags, so the shell value would be silently dropped", - tags[1], beadsBuildTags) - } - - // : "${CGO_ENABLED:=1}" - cgo := regexp.MustCompile(`(?m)^: "\$\{CGO_ENABLED:=([^}]*)\}"$`).FindStringSubmatch(source) - if cgo == nil { - t.Fatalf(".buildflags no longer defaults CGO_ENABLED in the expected form:\n%s", source) - } - if got, _ := environmentValue(buildEnvironment([]string{"CGO_ENABLED="}, false), "CGO_ENABLED", false); got != cgo[1] { - t.Errorf("CGO_ENABLED default drift: .buildflags defaults to %q, buildEnvironment defaults to %q", cgo[1], got) - } - - // if [[ "${GOFLAGS:-}" != *gms_pure_go* ]]; then - guard := regexp.MustCompile(`(?m)^if \[\[ "\$\{GOFLAGS:-\}" != \*([^*]*)\* \]\]; then$`).FindStringSubmatch(source) - if guard == nil { - t.Fatalf(".buildflags no longer guards GOFLAGS in the expected form:\n%s", source) - } - if guard[1] != beadsBuildTags { - t.Errorf("GOFLAGS guard drift: .buildflags skips its append when GOFLAGS contains %q, "+ - "driver skips on %q; the two must test the same token or one appends when the other does not", - guard[1], beadsBuildTags) - } - // The shell guard is a substring match (*tag*), and buildEnvironment uses - // strings.Contains for the same reason. Pin that agreement: making only the - // Go side stricter would append a tag the wrapper had already decided was - // present. - preset := []string{"GOFLAGS=-tags=" + guard[1] + "_x"} - if got, _ := environmentValue(buildEnvironment(preset, false), "GOFLAGS", false); got != preset[0][len("GOFLAGS="):] { - t.Errorf("GOFLAGS guard is no longer the substring test .buildflags performs: got %q, want %q", - got, preset[0][len("GOFLAGS="):]) + if len(runner.runCommands) != wantPasses { + t.Errorf("%d passes, want %d", len(runner.runCommands), wantPasses) } } diff --git a/scripts/pr_lanes_bazel_coverage_test.go b/scripts/pr_lanes_bazel_coverage_test.go index dc6148611..d6b5ce0d8 100644 --- a/scripts/pr_lanes_bazel_coverage_test.go +++ b/scripts/pr_lanes_bazel_coverage_test.go @@ -22,13 +22,12 @@ import ( // TestPRRiskDecisionMatchesBazelMode). Everything the retired jobs did // besides those tests keeps running on every PR: the package gates take the // Bazel-built bd, the Dolt server fingerprint runs on the dolt-server lane, -// and scripts-go-checks runs go test's vet checks. +// and nogo (//tools/nogo) runs go test's vet checks beside every compile of +// the Bazel lanes. const ( // The legacy jobs' if: not covered. - prLaneLegacyIf = "needs." + prRiskCoverageJobName + ".outputs.pr_lanes != 'true'" - prScriptsChecksJob = "scripts-go-checks" - prScriptsChecksID = "SCRIPTS_GO_CHECKS" + prLaneLegacyIf = "needs." + prRiskCoverageJobName + ".outputs.pr_lanes != 'true'" // bazel.yml's lanes for the step add --config=sole-run whenever they // execute remotely (enabled: mode remote, and the rbe-fork modes // fork-ro/fork-rw, whose lanes are a fork's only run once @@ -334,7 +333,7 @@ func TestPRLegacyLanesDeferToBazelLanes(t *testing.T) { } for _, c := range []string{"true", "false"} { for _, res := range []string{"skipped", "failure", "cancelled"} { - for _, job := range []string{"package-mcp", "package-npm", "pr-preflight-platforms", prScriptsChecksJob} { + for _, job := range []string{"package-mcp", "package-npm", "pr-preflight-platforms"} { sc := prGateFor(t, lanes, "pull_request", "remote", cov(c)) // F3: package-mcp/package-npm are bazel.yml call outputs // (needs.bazel.outputs.package-mcp), not pr.yml job @@ -345,7 +344,7 @@ func TestPRLegacyLanesDeferToBazelLanes(t *testing.T) { } else { sc.results = map[string]string{job: res} } - id := map[string]string{"pr-preflight-platforms": "PR_PREFLIGHT_PLATFORMS", prScriptsChecksJob: prScriptsChecksID}[job] + id := map[string]string{"pr-preflight-platforms": "PR_PREFLIGHT_PLATFORMS"}[job] if id == "" { id = prPackageGateIDs[job] } @@ -572,53 +571,55 @@ func TestDoltServerFingerprintRunsOnTheDoltServerLane(t *testing.T) { } } -// PR Core's one duty the Bazel lanes do not take over runs on every PR, in -// pr.yml's required scripts-go-checks job: go test's vet checks over ./... -// (rules_go's go_test runs none). The repository policy tests and the Go -// tests that walk the checkout run under Bazel (//scripts:scripts_test and -// //test/docsync over //:repo_files), so no workflow runs `go test` over -// ./scripts/... or the equivalence allowlist any more. +// PR Core's one duty the Bazel lanes did not take over, go test's vet checks +// over ./... (rules_go's go_test runs none), is nogo's: //tools/nogo runs +// cmd/go's defaultVetFlags analyzers beside every compile of every lane, so +// bazel.yml's required test lane (`bazel test //... --config=ci`) fails on a +// finding. No workflow runs `go vet` any more. The repository policy tests +// and the Go tests that walk the checkout run under Bazel +// (//scripts:scripts_test and //test/docsync over //:repo_files), so no +// workflow runs `go test` over ./scripts/... or the equivalence allowlist +// either. func TestPRRunsGoTestsBazelSkips(t *testing.T) { pr := readCIWorkflow(t, "pr.yml") - job := pr.job(t, prScriptsChecksJob) - // F7b: same-repo PRs now run this job on Blacksmith (forks/Dependabot keep - // ubuntu-latest); see sameRepoBlacksmith4vcpu in ci_blacksmith_runner_test.go. - if job.If != "" || job.ContinueOnError || len(job.Needs) != 0 || job.RunsOn != sameRepoBlacksmith4vcpu || job.TimeoutMinutes == 0 { - t.Errorf("%s: if %q, continue-on-error %v, needs %v, runs-on %q, timeout %d; want an unconditional same-repo-Blacksmith job with a timeout", - prScriptsChecksJob, job.If, job.ContinueOnError, job.Needs, job.RunsOn, job.TimeoutMinutes) + 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") } - if want := []string{"vet"}; !equalStrings(job.Strategy.Matrix.Check, want) { - t.Errorf("%s matrix.check = %v, want %v", prScriptsChecksJob, job.Strategy.Matrix.Check, want) + var passes []string + for _, q := range regexp.MustCompile(`"([a-z]+)"`).FindAllStringSubmatch(m[1], -1) { + passes = append(passes, q[1]) } - var names []string - for _, st := range job.Steps { - names = append(names, st.Name) - if st.ContinueOnError != nil { - t.Errorf("%s step %q has continue-on-error", prScriptsChecksJob, st.Name) + 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) } - wantNames := []string{"", "Set up Go", "Restore Go module cache", "Restore vet Go build cache", "Go vet with go test's checks"} - if !reflect.DeepEqual(names, wantNames) { - t.Errorf("%s steps %q, want %q", prScriptsChecksJob, names, wantNames) + 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) } - st := job.step(t, "Go vet with go test's checks") - if want := "bash scripts/ci/go-test-vet.sh"; st.Run != want || st.If != "${{ !cancelled() && steps.setup-go.outcome == 'success' && matrix.check == 'vet' }}" || - !reflect.DeepEqual(st.Env, map[string]string{"GOCACHE": "${{ runner.temp }}/go-cache/vet"}) { - t.Errorf("%s vet step: run %q, if %q, env %v", prScriptsChecksJob, st.Run, st.If, st.Env) + 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)") } - gate := pr.job(t, "ci-gate") - env := gate.step(t, "Evaluate CI gate").Env - if !contains(gate.Needs, prScriptsChecksJob) || env[prScriptsChecksID] != "${{ needs."+prScriptsChecksJob+".result }}" || - !contains(strings.Fields(env["CI_GATE_REQUIRED"]), prScriptsChecksID) { - t.Errorf("ci-gate does not require %s", prScriptsChecksID) + if _, err := os.Stat(filepath.Join(root, "scripts", "ci", "go-test-vet.sh")); err == nil { + t.Error("scripts/ci/go-test-vet.sh is back; go test's vet checks run as nogo") } - // Nothing else in pr.yml runs go-test-vet.sh, and no workflow brings back - // a go test of the scripts packages or of the allowlist (they run under - // Bazel). - for name, j := range pr.Jobs { - for _, st := range j.Steps { - if strings.Contains(st.Run, "go-test-vet.sh") && name != prScriptsChecksJob { - t.Errorf("%s step %q runs go-test-vet.sh; only %s does", name, st.Name, prScriptsChecksJob) + goVet := regexp.MustCompile(`\bgo vet\b|go-test-vet\.sh`) + for _, wf := range []string{"pr.yml", "main.yml", "nightly.yml", "pr-risk.yml", bazelWorkflowName} { + for name, j := range readCIWorkflow(t, wf).Jobs { + for _, st := range j.Steps { + if goVet.MatchString(st.Run) { + t.Errorf("%s %s step %q runs go vet; nogo runs its checks in the Bazel lanes", wf, name, st.Name) + } } } } @@ -633,45 +634,11 @@ func TestPRRunsGoTestsBazelSkips(t *testing.T) { } } } - // main.yml's push-only go-vet-cache job (F5.3) is the one explicit - // exception: it warms the vet cache pr.yml's vet leg restores, and - // nothing else in main.yml may run go-test-vet.sh either. - for name, j := range readCIWorkflow(t, "main.yml").Jobs { - for _, st := range j.Steps { - if strings.Contains(st.Run, "go-test-vet.sh") && name != "go-vet-cache" { - t.Errorf("main.yml %s step %q runs go-test-vet.sh; only go-vet-cache may", name, st.Name) - } - } - } - if got := readCIWorkflow(t, "main.yml").job(t, "go-vet-cache").step(t, "Go vet with go test's checks").Run; got != "bash scripts/ci/go-test-vet.sh" { - t.Errorf("main.yml go-vet-cache does not run go-test-vet.sh: %q", got) - } - root := sourceRepoRoot(t) for _, gone := range []string{"scripts/ci/allowlisted-go-tests.sh", "scripts/ci/scripts-go-test.sh", "tools/bazel/run_allowlisted_go_tests.py"} { if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(gone))); err == nil { t.Errorf("%s is back; the tests it ran under go test run under Bazel", gone) } } - prelude := []string{ - "set -euo pipefail", - `SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"`, - `REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"`, - `source "$REPO_ROOT/.buildflags"`, - `source "$REPO_ROOT/scripts/ci/lib/timing.sh"`, - `cd "$REPO_ROOT"`, - "GO_TEST_VET_FLAGS=(" + strings.Join(goTestDefaultVetFlags(t), " ") + ")", - `ci_time "go vet (go test's checks)" -- \`, - `go vet -tags gms_pure_go "${GO_TEST_VET_FLAGS[@]}" ./...`, - } - var code []string - for _, line := range strings.Split(readPolicyFile(t, root, "scripts/ci/go-test-vet.sh"), "\n") { - if l := strings.TrimSpace(line); l != "" && !strings.HasPrefix(l, "#") { - code = append(code, l) - } - } - if !reflect.DeepEqual(code, prelude) { - t.Errorf("scripts/ci/go-test-vet.sh code changed:\n%s\nwant:\n%s", strings.Join(code, "\n"), strings.Join(prelude, "\n")) - } // The allowlist's remaining skip entries run in pr-preflight-platforms // (every OS), each named in its go test selector. fallback := pr.job(t, "pr-preflight-platforms").step(t, "Exercise go test's bd build fallback").Run @@ -689,7 +656,7 @@ 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 go-test-vet.sh follows. Under Bazel the +// changes them fails here until tools/nogo/analyzers.bzl follows. Under Bazel the // toolchain is the registered SDK (testGo). func goTestDefaultVetFlags(t *testing.T) []string { t.Helper() diff --git a/scripts/pr_risk_bazel_coverage_test.go b/scripts/pr_risk_bazel_coverage_test.go index 0157ad143..bde3778fe 100644 --- a/scripts/pr_risk_bazel_coverage_test.go +++ b/scripts/pr_risk_bazel_coverage_test.go @@ -1535,7 +1535,7 @@ func TestShardScriptsListOnlyRealTests(t *testing.T) { // otherwise force a full repack and conflict on unrelated shard lines (see // --repack below for the explicit opt-in to that). Run --check here so a // block with missing/stale/duplicate names fails go test ./scripts/... -// (and so scripts-go-checks, which runs on fork PRs) instead of only +// (//scripts:scripts_test under Bazel) instead of only // surfacing as a test silently never running in any shard. The legacy // 15-shard block is deliberately excluded: its header documents that it is // frozen and must not be regenerated (see diff --git a/scripts/prlintmake/prlintmake_test.go b/scripts/prlintmake/prlintmake_test.go index 20298f6a1..4f5101a08 100644 --- a/scripts/prlintmake/prlintmake_test.go +++ b/scripts/prlintmake/prlintmake_test.go @@ -86,18 +86,20 @@ func TestPRLintWrapperDelegatesPolicyToCheckoutGoDriver(t *testing.T) { if run.goArgs != "run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint" { t.Fatalf("go args = %q, want checkout driver invocation", run.goArgs) } + if strings.Contains(run.output, "gofmt ran") { + t.Fatalf("the lint wrapper ran gofmt; //scripts/repochecks:fmt_test owns it:\n%s", run.output) + } for _, want := range []string{ "CGO_ENABLED=1", "BEADS_BUILD_TAGS=gms_pure_go", "GOFLAGS=-mod=readonly -tags=gms_pure_go", - "BD_LINT_NEW_FROM_MERGE_BASE=origin/main", } { if !strings.Contains(run.goEnvironment, want+"\n") { t.Fatalf("driver environment missing %q:\n%s", want, run.goEnvironment) } } - if !strings.Contains(run.output, "==> golangci-lint (native + windows/darwin non-CGO)") || - !strings.Contains(run.output, "<== golangci-lint (native + windows/darwin non-CGO) succeeded") { + if !strings.Contains(run.output, "==> nogo (native + windows/darwin non-cgo)") || + !strings.Contains(run.output, "<== nogo (native + windows/darwin non-cgo) succeeded") { t.Fatalf("aggregate lint timing is not attributable:\n%s", run.output) } } @@ -127,17 +129,16 @@ func runPRLintWrapper(t *testing.T, goBody string) prLintWrapperRun { if err := os.MkdirAll(shimDir, 0o755); err != nil { t.Fatal(err) } - // fmt-check.sh ignores a PATH gofmt (be-gx8), so the gofmt shim goes in - // through GOFMT, as in runFmtCheck. Only the go shim needs to be on PATH. + // gofmt is //scripts/repochecks:fmt_test's, not the lint wrapper's + // (ga-96smfk.41): a gofmt that would fail every file must not be run. gofmt := filepath.Join(testRoot, "gofmt") - writeShellExecutable(t, bash, gofmt, "#!/usr/bin/env bash\nset -euo pipefail\nexit 0\n") + writeShellExecutable(t, bash, gofmt, "#!/usr/bin/env bash\nprintf 'gofmt ran\\n' >&2\nexit 1\n") writeShellExecutable(t, bash, filepath.Join(shimDir, "go"), `#!/usr/bin/env bash set -euo pipefail printf '%s\n' "$*" >"$GO_ARGS_MARKER" printf 'CGO_ENABLED=%s\n' "${CGO_ENABLED-}" >"$GO_ENV_MARKER" printf 'BEADS_BUILD_TAGS=%s\n' "${BEADS_BUILD_TAGS-}" >>"$GO_ENV_MARKER" printf 'GOFLAGS=%s\n' "${GOFLAGS-}" >>"$GO_ENV_MARKER" -printf 'BD_LINT_NEW_FROM_MERGE_BASE=%s\n' "${BD_LINT_NEW_FROM_MERGE_BASE-}" >>"$GO_ENV_MARKER" `+goBody) argsMarker := filepath.Join(testRoot, "go-args") @@ -155,20 +156,19 @@ printf 'BD_LINT_NEW_FROM_MERGE_BASE=%s\n' "${BD_LINT_NEW_FROM_MERGE_BASE-}" >>"$ ) cmd.Dir = sourceRepoRoot() cmd.Env = environment(map[string]string{ - "BASH_ENV": "", - "BASHOPTS": "", - "BD_LINT_NEW_FROM_MERGE_BASE": "origin/main", - "BEADS_BUILD_TAGS": "stale", - "CGO_ENABLED": "", - "ENV": "", - "GOFLAGS": "-mod=readonly", - "GOFMT": shellVisiblePath(gofmt), - "GO_ARGS_MARKER": shellVisiblePath(argsMarker), - "GO_ENV_MARKER": shellVisiblePath(envMarker), - "LANG": "C", - "LC_ALL": "C", - "PATH": path, - "SHELLOPTS": "", + "BASH_ENV": "", + "BASHOPTS": "", + "BEADS_BUILD_TAGS": "stale", + "CGO_ENABLED": "", + "ENV": "", + "GOFLAGS": "-mod=readonly", + "GOFMT": shellVisiblePath(gofmt), + "GO_ARGS_MARKER": shellVisiblePath(argsMarker), + "GO_ENV_MARKER": shellVisiblePath(envMarker), + "LANG": "C", + "LC_ALL": "C", + "PATH": path, + "SHELLOPTS": "", }) output, runErr := cmd.CombinedOutput() args, argsErr := os.ReadFile(argsMarker) diff --git a/third_party/patches/BUILD.bazel b/third_party/patches/BUILD.bazel index f15db188c..086c77351 100644 --- a/third_party/patches/BUILD.bazel +++ b/third_party/patches/BUILD.bazel @@ -11,6 +11,12 @@ # dolt uses, so `--config=pure` (CGO off) can compile store/nbs, which rules_go # builds as a declared dep even though no pure-Go file imports it. Their init # panics, so a pure binary that does link gozstd fails when started. +# +# rules_go_nogo_golangci_nolint.patch and x_tools_ctrlflow_stdlib_noreturn.patch +# are gascity's (third_party/patches there), byte-identical: nogo (//tools/nogo) +# honors //nolint with golangci-lint's ranges, and ctrlflow knows the standard +# library's no-return functions, which nogo has no facts for. Upstreaming is +# tracked in gascity's ga-96smfk.44. package(default_visibility = ["//visibility:public"]) diff --git a/third_party/patches/rules_go_nogo_golangci_nolint.patch b/third_party/patches/rules_go_nogo_golangci_nolint.patch new file mode 100644 index 000000000..829662db9 --- /dev/null +++ b/third_party/patches/rules_go_nogo_golangci_nolint.patch @@ -0,0 +1,100 @@ +diff -ruN a/go/tools/builders/nogo_main.go b/go/tools/builders/nogo_main.go +--- a/go/tools/builders/nogo_main.go 2026-10-06 23:54:04.475812427 +0000 ++++ b/go/tools/builders/nogo_main.go 2026-10-06 23:55:14.974887313 +0000 +@@ -342,6 +342,7 @@ + } + } + } ++ addGolangciNolintRanges(pkg.fset, f, actions) + // CommentMap will correctly associate comments to the largest node group + // applicable. This handles inline comments that might trail a large + // assignment and will apply the comment to the entire assignment. +@@ -358,7 +359,7 @@ + continue + } + for analyzer, act := range actions { +- if linters == nil || linters[analyzer.Name] { ++ if nolintCovers(linters, analyzer.Name) { + act.nolint = append(act.nolint, rng) + } + } +@@ -379,6 +380,79 @@ + to int + } + ++// staticcheckCheck matches the names of honnef.co/go/tools' per-check ++// analyzers (SA1019, S1016, ST1005, QF1001, ...). ++var staticcheckCheck = regexp.MustCompile(`^(SA|S|ST|QF)[0-9]+$`) ++ ++// nolintCovers reports whether a directive naming linters (nil: all) covers ++// the analyzer. As in golangci-lint, `staticcheck` names every staticcheck ++// check. (gascity third_party/patches) ++func nolintCovers(linters map[string]bool, analyzer string) bool { ++ return linters == nil || linters[analyzer] || (linters["staticcheck"] && staticcheckCheck.MatchString(analyzer)) ++} ++ ++// addGolangciNolintRanges adds golangci-lint's nolint ranges for f, which the ++// directives in a tree linted by golangci-lint were written against, beside ++// the CommentMap ranges above: each directive covers its comment group's own ++// lines, extended over any node that starts on the next line at the group's ++// column. Without them a directive trailing `func f(a, b int) {` misses the ++// signature line, because the CommentMap attaches it to the body's first ++// statement. (gascity third_party/patches) ++func addGolangciNolintRanges(fset *token.FileSet, f *ast.File, actions map[*analysis.Analyzer]*action) { ++ type inlineRange struct { ++ from, to, col int ++ linters map[string]bool ++ } ++ var inline []inlineRange ++ for _, group := range f.Comments { ++ for _, comm := range group.List { ++ linters, ok := parseNolint(comm.Text) ++ if !ok { ++ continue ++ } ++ start := fset.Position(group.Pos()) ++ inline = append(inline, inlineRange{ ++ from: start.Line, ++ to: fset.Position(group.End()).Line, ++ col: start.Column, ++ linters: linters, ++ }) ++ } ++ } ++ if len(inline) == 0 { ++ return ++ } ++ filename := fset.Position(f.Pos()).Filename ++ add := func(from, to int, linters map[string]bool) { ++ rng := &Range{from: token.Position{Filename: filename, Line: from}, to: to} ++ for analyzer, act := range actions { ++ if nolintCovers(linters, analyzer.Name) { ++ act.nolint = append(act.nolint, rng) ++ } ++ } ++ } ++ for _, r := range inline { ++ add(r.from, r.to, r.linters) ++ } ++ ast.Inspect(f, func(n ast.Node) bool { ++ if n == nil { ++ return true ++ } ++ start := fset.Position(n.Pos()) ++ for _, r := range inline { ++ if r.to == start.Line-1 && start.Column == r.col { ++ end := fset.Position(n.End()).Line ++ if end < r.to { ++ end = r.to ++ } ++ add(r.from, end, r.linters) ++ break ++ } ++ } ++ return true ++ }) ++} ++ + // factProducers returns the set of analyzers that declare facts among the + // transitive closure of as. + func factProducers(as []*analysis.Analyzer) []*analysis.Analyzer { diff --git a/third_party/patches/x_tools_ctrlflow_stdlib_noreturn.patch b/third_party/patches/x_tools_ctrlflow_stdlib_noreturn.patch new file mode 100644 index 000000000..f4f143446 --- /dev/null +++ b/third_party/patches/x_tools_ctrlflow_stdlib_noreturn.patch @@ -0,0 +1,34 @@ +diff -ruN a/go/analysis/passes/ctrlflow/ctrlflow.go b/go/analysis/passes/ctrlflow/ctrlflow.go +--- a/go/analysis/passes/ctrlflow/ctrlflow.go 2026-10-06 23:27:28.576172198 +0000 ++++ b/go/analysis/passes/ctrlflow/ctrlflow.go 2026-10-06 23:27:28.604645688 +0000 +@@ -222,6 +222,16 @@ + } + + // Not declared in this package. ++ // Known intrinsically? (gascity third_party/patches: Bazel's nogo does ++ // not analyze the standard library, so its no-return functions, listed ++ // in knownIntrinsic, have no facts to import.) ++ if noreturn, known := knownIntrinsic(fn); known { ++ if noreturn { ++ c.noReturn[fn] = true ++ } ++ return !noreturn ++ } ++ + // Is there a fact from another package? + if c.pass.ImportObjectFact(fn, new(noReturn)) { + c.noReturn[fn] = true +@@ -245,6 +255,13 @@ + // Functions known intrinsically never to return. + if typesinternal.IsFunctionNamed(fn, "syscall", "Exit", "ExitProcess", "ExitThread") || + typesinternal.IsFunctionNamed(fn, "runtime", "Goexit", "fatalthrow", "fatalpanic", "exit") || ++ // Standard-library functions that never return. Analysis of the ++ // standard library normally derives these as facts; Bazel's nogo ++ // does not analyze it (gascity third_party/patches). ++ typesinternal.IsFunctionNamed(fn, "os", "Exit") || ++ typesinternal.IsFunctionNamed(fn, "log", "Fatal", "Fatalf", "Fatalln", "Panic", "Panicf", "Panicln") || ++ typesinternal.IsMethodNamed(fn, "log", "Logger", "Fatal", "Fatalf", "Fatalln", "Panic", "Panicf", "Panicln") || ++ typesinternal.IsMethodNamed(fn, "testing", "common", "FailNow", "Fatal", "Fatalf", "SkipNow", "Skip", "Skipf") || + // Following staticcheck (see go/ir/exits.go) we include functions + // in several popular logging packages whose no-return status is + // beyond the analysis to infer. diff --git a/tools/bazel/equivalence.py b/tools/bazel/equivalence.py index f45998fde..40b67c875 100644 --- a/tools/bazel/equivalence.py +++ b/tools/bazel/equivalence.py @@ -43,6 +43,10 @@ does; the default mode keeps the worst. A failure in any target still fails a job comparison (listed per target), and so does a missing test.xml of any target in the job's packages, variant or not. +Packages of nested Go modules (a go.mod below the root: tools/nogo, the nogo +analyzers' own module) are outside `go test ./...`, so their Bazel tests are +neither expected nor reported as extra. + Divergences listed in the allowlist (tools/bazel/equivalence_allowlist.txt) are expected; anything else fails. The allowlist format is one entry per line: @@ -122,6 +126,13 @@ def go_expected(root, go_json_path=None): return expected, dirs +def in_nested_module(root, pkg): + """Whether pkg (a directory relative to root) belongs to a Go module + other than root's: a go.mod at or above it, below root.""" + parts = pkg.split("/") if pkg else [] + return any(os.path.exists(os.path.join(root, *parts[:i], "go.mod")) for i in range(1, len(parts) + 1)) + + def go_test_statuses(path, dirs): """Return {pkg_dir: {test: status}} for top-level tests in `go test -json` output. Packages go list does not know are skipped.""" @@ -333,6 +344,7 @@ def main(argv=None): bep_tested, testlogs, bep_configured, args.job, observed, failures if args.job else None, no_xml if args.job else None) problems += bep_problems + observed = {pkg: seen for pkg, seen in observed.items() if not in_nested_module(root, pkg)} target_pkgs = None if (args.no_query or args.job) else query_go_test_pkgs(root, args.bazel) go_status = go_test_statuses(args.go_test_json, dirs) if args.go_test_json else None if args.job: diff --git a/tools/bazel/go_srcs.py b/tools/bazel/go_srcs.py index 896c129af..e4ca6b6d3 100755 --- a/tools/bazel/go_srcs.py +++ b/tools/bazel/go_srcs.py @@ -38,7 +38,8 @@ scripts/ci/bazel-release-cross-compile.sh builds for every release platform, listing every go_library and go_binary it can see (the packages `go build ./...` compiles; a private library is compiled by the go_binary that embeds it) except the cgo-only ones a pure build cannot link (tagged "cgo-only" and -incompatible with //tools/bazel:pure) and testonly fixtures. The script checks +incompatible with //tools/bazel:pure), testonly fixtures, and the packages of +nested Go modules (tools/nogo), which `go build ./...` skips. The script checks against `bazel query` before it builds that every Bazel package holding Go targets is reached, so a package this parser misses fails CI instead of going uncompiled. @@ -230,10 +231,21 @@ def repo_files_block(pkg: str, packages: list[str]) -> str: return "\n".join(lines) + "\n" +def in_nested_module(pkg: str) -> bool: + """Whether pkg belongs to a Go module other than the root one (its own + go.mod at or above it, below the root), which `go build ./...` skips.""" + parts = [] if pkg == "." else pkg.split("/") + return any(os.path.exists(os.path.join(*parts[:i], "go.mod")) for i in range(1, len(parts) + 1)) + + def go_targets(packages: list[str]) -> list[str]: """Every go_library and go_binary that a pure build can compile.""" labels = [] for pkg in packages: + # tools/nogo (the nogo analyzers' own module) is not part of + # `go build ./...` or of any release. + if in_nested_module(pkg): + continue with open(os.path.join(pkg, "BUILD.bazel")) as f: src = f.read() for kind, name, body in GO_RULE_RE.findall(src): diff --git a/tools/nogo/BUILD.bazel b/tools/nogo/BUILD.bazel new file mode 100644 index 000000000..1e44a0bca --- /dev/null +++ b/tools/nogo/BUILD.bazel @@ -0,0 +1,66 @@ +load("@rules_go//go:def.bzl", "go_test", "nogo") +load(":analyzers.bzl", "NOGO_ANALYZERS") + +# gazelle:prefix github.com/steveyegge/beads/tools/nogo + +# nogo: lint and vet as part of every Go compile. MODULE.bazel registers this +# target with go_sdk.nogo, so `bazel build //...` (locally or on RBE) fails on +# any finding. Replaces `go vet` (go test's checks) and golangci-lint; gofmt +# stays with //scripts/repochecks:fmt_test. +nogo( + name = "nogo", + config = "config.json", + visibility = ["//visibility:public"], + deps = NOGO_ANALYZERS, +) + +go_test( + name = "nogo_test", + srcs = [ + "analyzers_test.go", + "modules_test.go", + ], + data = [ + "analyzers.bzl", + "config.json", + "go.mod", + "//:.golangci.yml", + "//:go.mod", + ], # keep + deps = [ + "//tools/nogo/analyzers/depguard", + "//tools/nogo/analyzers/errcheck", + "//tools/nogo/analyzers/forbidigo", + "//tools/nogo/analyzers/gosec", + "//tools/nogo/analyzers/misspell", + "//tools/nogo/analyzers/sloglint", + "//tools/nogo/analyzers/unconvert", + "//tools/nogo/analyzers/unparam", + "//tools/nogo/internal/golangci", + "@org_golang_x_mod//modfile", + "@org_golang_x_mod//semver", + "@org_golang_x_tools//go/analysis", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers.bzl b/tools/nogo/analyzers.bzl new file mode 100644 index 000000000..00a351e56 --- /dev/null +++ b/tools/nogo/analyzers.bzl @@ -0,0 +1,47 @@ +"""The nogo analyzer set: what `go vet` and golangci-lint checked, as go/analysis passes. + +This is the single list of analyzers `//tools/nogo` runs on every Go compile: + + - VET_PASSES: the vet checks `go test` runs (cmd/go's defaultVetFlags), which + pr.yml ran over ./... as `go vet` (scripts/ci/go-test-vet.sh) because + rules_go's go_test runs none. scripts/pr_lanes_bazel_coverage_test.go keeps + the list equal to the Go toolchain's. They see every file, tests included, + as `go vet ./...` did. + - GOLANGCI_LINTERS: the linters .golangci.yml enables, each wrapped by + //tools/nogo/internal/golangci so that .golangci.yml's settings, + run.tests, generated-file handling and exclusion rules apply as they did + under golangci-lint. //tools/nogo:nogo_test keeps the list equal to + .golangci.yml's linters.enable. +""" + +# cmd/go's defaultVetFlags (Go 1.26), as analyzer names (-bool is "bools", +# -buildtags "buildtag"). +VET_PASSES = [ + "atomic", + "bools", + "buildtag", + "directive", + "errorsas", + "ifaceassert", + "nilfunc", + "printf", + "slog", + "stringintconv", + "tests", +] + +GOLANGCI_LINTERS = [ + "depguard", + "errcheck", + "forbidigo", + "gosec", + "misspell", + "sloglint", + "unconvert", + "unparam", +] + +NOGO_ANALYZERS = ["@org_golang_x_tools//go/analysis/passes/" + p for p in VET_PASSES] + [ + "//tools/nogo/analyzers/" + linter + for linter in GOLANGCI_LINTERS +] diff --git a/tools/nogo/analyzers/depguard/BUILD.bazel b/tools/nogo/analyzers/depguard/BUILD.bazel new file mode 100644 index 000000000..8dda85443 --- /dev/null +++ b/tools/nogo/analyzers/depguard/BUILD.bazel @@ -0,0 +1,44 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "depguard", + srcs = ["depguard.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/depguard", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "@com_github_openpeedeep_depguard_v2//:depguard", + "@org_golang_x_tools//go/analysis", + ], +) + +go_test( + name = "depguard_test", + srcs = ["depguard_test.go"], + deps = [ + ":depguard", + "//tools/nogo/internal/analyzertest", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/depguard/depguard.go b/tools/nogo/analyzers/depguard/depguard.go new file mode 100644 index 000000000..1a6e12ec4 --- /dev/null +++ b/tools/nogo/analyzers/depguard/depguard.go @@ -0,0 +1,110 @@ +// Package depguard runs github.com/OpenPeeDeeP/depguard/v2 with the rules in +// .golangci.yml's linters.settings.depguard, as golangci-lint does. +package depguard + +import ( + "go/token" + "strings" + + "github.com/OpenPeeDeeP/depguard/v2" + "golang.org/x/tools/go/analysis" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +// Analyzer reports imports the configured depguard rules deny. +var Analyzer = golangci.Wrap("depguard", &analysis.Analyzer{ + Name: "depguard", + Doc: "checks package imports against the depguard rules in .golangci.yml", + Run: run, +}) + +const goStd = "$gostd" + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + std := stdPrefixes(pass) + settings := depguard.LinterSettings{} + for name, rule := range cfg.Linters.Settings.Depguard.Rules { + deny := map[string]string{} + for _, d := range rule.Deny { + if d.Pkg == goStd { + for _, p := range std { + deny[p] = d.Desc + } + continue + } + deny[d.Pkg] = d.Desc + } + settings[name] = &depguard.List{ + ListMode: rule.ListMode, + Files: rule.Files, + Allow: expandGoStd(rule.Allow, std), + Deny: deny, + } + } + a, err := depguard.NewAnalyzer(&settings) + if err != nil { + return nil, err + } + rooted := *pass + rooted.Fset = Rooted(pass.Fset) + return a.Run(&rooted) +} + +// stdPrefixes stands in for depguard's $gostd, which lists $GOROOT/src's +// top-level directories (a nogo action has no GOROOT source tree). It returns +// the first path element of every standard-library import in the pass, by +// cmd/go's own rule: a standard import path has no dot in its first element. +// "C" (cgo) is not a GOROOT directory. "unsafe" keeps an allow list non-empty +// for a package with no standard imports; it is a GOROOT directory too. +func stdPrefixes(pass *analysis.Pass) []string { + seen := map[string]bool{"unsafe": true} + out := []string{"unsafe"} + for _, f := range pass.Files { + for _, imp := range f.Imports { + path := strings.Trim(imp.Path.Value, "\"`") + first, _, _ := strings.Cut(path, "/") + if first == "C" || strings.Contains(first, ".") || seen[first] { + continue + } + seen[first] = true + out = append(out, first) + } + } + return out +} + +func expandGoStd(list, std []string) []string { + var out []string + for _, p := range list { + if p == goStd { + out = append(out, std...) + continue + } + out = append(out, p) + } + return out +} + +// Rooted returns a copy of fset whose file names are rooted at "/": the +// absolute paths golangci-lint hands depguard, so that a files pattern such +// as "**/internal/storage/**" or "$all" ("**/*.go") also matches a file at +// the repository root, where nogo's execroot-relative name has no leading +// directory. Every token.Pos keeps its meaning. +func Rooted(fset *token.FileSet) *token.FileSet { + out := token.NewFileSet() + fset.Iterate(func(f *token.File) bool { + name := f.Name() + if !strings.HasPrefix(name, "/") { + name = "/" + name + } + nf := out.AddFile(name, f.Base(), f.Size()) + nf.SetLines(f.Lines()) + return true + }) + return out +} diff --git a/tools/nogo/analyzers/depguard/depguard_test.go b/tools/nogo/analyzers/depguard/depguard_test.go new file mode 100644 index 000000000..3b960da47 --- /dev/null +++ b/tools/nogo/analyzers/depguard/depguard_test.go @@ -0,0 +1,48 @@ +package depguard_test + +import ( + "strings" + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/depguard" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" +) + +// The rules come from the repository's .golangci.yml. + +func TestFilesGlobsMatchRepositoryRelativeNames(t *testing.T) { + got := analyzertest.RunInRepo(t, depguard.Analyzer, "internal/workapi", map[string]string{ + "w.go": "package workapi\n\nimport _ \"github.com/spf13/cobra\"\n", + }) + if len(got) != 1 || !strings.HasPrefix(got[0].Message, "import 'github.com/spf13/cobra' is not allowed from list 'workapi-frontend-boundary'") { + t.Fatalf("diagnostics = %+v, want the workapi-frontend-boundary denial", got) + } +} + +// "$all" is "**/*.go": golangci-lint hands depguard absolute paths, so it +// covers files at the repository root too. +func TestAllCoversTheRepositoryRoot(t *testing.T) { + got := analyzertest.RunInRepo(t, depguard.Analyzer, "", map[string]string{ + "root.go": "package beads\n\nimport _ \"github.com/dolthub/dolt/go/libraries/doltcore/doltdb\"\n", + }) + if len(got) != 1 || !strings.Contains(got[0].Message, "'dolt-storage-boundary'") { + t.Fatalf("diagnostics = %+v, want the dolt-storage-boundary denial", got) + } +} + +func TestNegatedFilesAreExempt(t *testing.T) { + got := analyzertest.RunInRepo(t, depguard.Analyzer, "internal/storage/x", map[string]string{ + "x.go": "package x\n\nimport _ \"github.com/dolthub/dolt/go/libraries/doltcore/doltdb\"\n", + }) + analyzertest.Equal(t, got, nil) +} + +// beadserrors' strict rule allows only $gostd. +func TestGoStdAllowList(t *testing.T) { + got := analyzertest.RunInRepo(t, depguard.Analyzer, "beadserrors", map[string]string{ + "e.go": "package beadserrors\n\nimport (\n\t_ \"errors\"\n\t_ \"net/http\"\n\t_ \"github.com/steveyegge/beads/internal/types\"\n)\n", + }) + if len(got) != 1 || !strings.HasPrefix(got[0].Message, "import 'github.com/steveyegge/beads/internal/types' is not allowed from list 'beadserrors-leaf'") { + t.Fatalf("diagnostics = %+v, want only the internal/types denial", got) + } +} diff --git a/tools/nogo/analyzers/errcheck/BUILD.bazel b/tools/nogo/analyzers/errcheck/BUILD.bazel new file mode 100644 index 000000000..2d1eaa1b2 --- /dev/null +++ b/tools/nogo/analyzers/errcheck/BUILD.bazel @@ -0,0 +1,46 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "errcheck", + srcs = ["errcheck.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/errcheck", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "//tools/nogo/internal/position", + "@com_github_kisielk_errcheck//errcheck", + "@org_golang_x_tools//go/analysis", + "@org_golang_x_tools//go/packages", + ], +) + +go_test( + name = "errcheck_test", + srcs = ["errcheck_test.go"], + deps = [ + ":errcheck", + "//tools/nogo/internal/analyzertest", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/errcheck/errcheck.go b/tools/nogo/analyzers/errcheck/errcheck.go new file mode 100644 index 000000000..4b6e5ebe7 --- /dev/null +++ b/tools/nogo/analyzers/errcheck/errcheck.go @@ -0,0 +1,75 @@ +// Package errcheck runs github.com/kisielk/errcheck as golangci-lint's errcheck +// linter does, with .golangci.yml's linters.settings.errcheck. +package errcheck + +import ( + "cmp" + "fmt" + "regexp" + "strings" + + "github.com/kisielk/errcheck/errcheck" + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/packages" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" + "github.com/steveyegge/beads/tools/nogo/internal/position" +) + +// Analyzer reports unchecked errors. +var Analyzer = golangci.Wrap("errcheck", &analysis.Analyzer{ + Name: "errcheck", + Doc: "reports unchecked errors", + Run: run, +}) + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + settings := cfg.Linters.Settings.Errcheck + checker := errcheck.Checker{ + Exclusions: errcheck.Exclusions{ + BlankAssignments: !settings.CheckAssignToBlank, + TypeAssertions: !settings.CheckTypeAssertions, + SymbolRegexpsByPackage: map[string]*regexp.Regexp{}, + }, + } + if !settings.DisableDefaultExclusions { + checker.Exclusions.Symbols = append(checker.Exclusions.Symbols, errcheck.DefaultExcludedSymbols...) + } + checker.Exclusions.Symbols = append(checker.Exclusions.Symbols, settings.ExcludeFunctions...) + + pkg := &packages.Package{ + Fset: pass.Fset, + Syntax: pass.Files, + Types: pass.Pkg, + TypesInfo: pass.TypesInfo, + } + idx := position.NewIndex(pass) + for _, u := range checker.CheckPackage(pkg).Unique().UncheckedErrors { + pos, ok := idx.Pos(u.Pos) + if !ok { + continue + } + pass.Report(analysis.Diagnostic{Pos: pos, Message: Message(u, settings.Verbose)}) + } + return nil, nil +} + +// Message is golangci-lint's text for an unchecked error, which +// .golangci.yml's exclusion rules match against. +func Message(u errcheck.UncheckedError, verbose bool) string { + if u.FuncName == "" { + return "Error return value is not checked" + } + code := cmp.Or(u.SelectorName, u.FuncName) + if verbose { + code = u.FuncName + } + if !strings.Contains(code, "`") { + code = "`" + code + "`" + } + return fmt.Sprintf("Error return value of %s is not checked", code) +} diff --git a/tools/nogo/analyzers/errcheck/errcheck_test.go b/tools/nogo/analyzers/errcheck/errcheck_test.go new file mode 100644 index 000000000..33c68fb81 --- /dev/null +++ b/tools/nogo/analyzers/errcheck/errcheck_test.go @@ -0,0 +1,35 @@ +package errcheck_test + +import ( + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/errcheck" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" +) + +// The expected messages are golangci-lint v2.10.1's for the same package. +func TestReportsUncheckedErrorsAsGolangciLintDoes(t *testing.T) { + got := analyzertest.RunInRepo(t, errcheck.Analyzer, "p", map[string]string{ + "p.go": `package p + +type closer struct{} + +func (closer) Close() error { return nil } + +func f() error { return nil } + +func g() { + f() + _ = f() + closer{}.Close() + if err := f(); err != nil { + return + } +} +`, + }) + analyzertest.Equal(t, got, []analyzertest.Diagnostic{ + {File: "p.go", Line: 10, Message: "Error return value is not checked"}, + {File: "p.go", Line: 12, Message: "Error return value of `(example.com/repo/p.closer).Close` is not checked"}, + }) +} diff --git a/tools/nogo/analyzers/forbidigo/BUILD.bazel b/tools/nogo/analyzers/forbidigo/BUILD.bazel new file mode 100644 index 000000000..ae87048b5 --- /dev/null +++ b/tools/nogo/analyzers/forbidigo/BUILD.bazel @@ -0,0 +1,45 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "forbidigo", + srcs = ["forbidigo.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/forbidigo", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "@com_github_ashanbrown_forbidigo_v2//forbidigo", + "@in_gopkg_yaml_v3//:yaml_v3", + "@org_golang_x_tools//go/analysis", + ], +) + +go_test( + name = "forbidigo_test", + srcs = ["forbidigo_test.go"], + deps = [ + ":forbidigo", + "//tools/nogo/internal/analyzertest", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/forbidigo/forbidigo.go b/tools/nogo/analyzers/forbidigo/forbidigo.go new file mode 100644 index 000000000..360cc61a2 --- /dev/null +++ b/tools/nogo/analyzers/forbidigo/forbidigo.go @@ -0,0 +1,77 @@ +// Package forbidigo runs github.com/ashanbrown/forbidigo/v2 as golangci-lint's +// forbidigo linter does, with .golangci.yml's linters.settings.forbidigo. +package forbidigo + +import ( + "fmt" + + "github.com/ashanbrown/forbidigo/v2/forbidigo" + "golang.org/x/tools/go/analysis" + "gopkg.in/yaml.v3" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +// Analyzer reports uses of forbidden identifiers. +var Analyzer = golangci.Wrap("forbidigo", &analysis.Analyzer{ + Name: "forbidigo", + Doc: "forbids the identifiers .golangci.yml lists", + Run: run, +}) + +// pattern is golangci-lint's ForbidigoPattern as forbidigo.NewLinter reads it. +type pattern struct { + Pattern string `yaml:"p"` + Package string `yaml:"pkg,omitempty"` + Msg string `yaml:"msg,omitempty"` +} + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + settings := cfg.Linters.Settings.Forbidigo + linter, err := NewLinter(settings) + if err != nil { + return nil, err + } + for _, file := range pass.Files { + rc := forbidigo.RunConfig{Fset: pass.Fset} + if settings.AnalyzeTypes { + rc.TypesInfo = pass.TypesInfo + } + hints, err := linter.RunWithConfig(rc, file) + if err != nil { + return nil, fmt.Errorf("forbidigo on %s: %w", pass.Fset.File(file.Pos()).Name(), err) + } + for _, hint := range hints { + pass.Report(analysis.Diagnostic{Pos: hint.Pos(), Message: hint.Details()}) + } + } + return nil, nil +} + +// NewLinter builds forbidigo's linter from the settings, with golangci-lint's +// options: //permit directives are ignored (only //nolint counts), and godoc +// examples are skipped unless exclude-godoc-examples is false. +func NewLinter(settings golangci.ForbidigoSettings) (*forbidigo.Linter, error) { + excludeExamples := settings.ExcludeGodocExamples == nil || *settings.ExcludeGodocExamples + var patterns []string + for _, p := range settings.Forbid { + buf, err := yaml.Marshal(pattern{Pattern: p.Pattern, Package: p.Pkg, Msg: p.Msg}) + if err != nil { + return nil, err + } + patterns = append(patterns, string(buf)) + } + linter, err := forbidigo.NewLinter(patterns, + forbidigo.OptionExcludeGodocExamples(excludeExamples), + forbidigo.OptionIgnorePermitDirectives(true), + forbidigo.OptionAnalyzeTypes(settings.AnalyzeTypes), + ) + if err != nil { + return nil, fmt.Errorf("forbidigo: %w", err) + } + return linter, nil +} diff --git a/tools/nogo/analyzers/forbidigo/forbidigo_test.go b/tools/nogo/analyzers/forbidigo/forbidigo_test.go new file mode 100644 index 000000000..41ab2b784 --- /dev/null +++ b/tools/nogo/analyzers/forbidigo/forbidigo_test.go @@ -0,0 +1,37 @@ +package forbidigo_test + +import ( + "strings" + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/forbidigo" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" +) + +const labelUse = `package main + +type uow struct{} + +func (uow) LabelUseCase() int { return 0 } + +func f(u uow) int { return u.LabelUseCase() } +` + +// The LabelUseCase rule applies across cmd/bd; .golangci.yml's path-except +// rule keeps forbidigo off the rest of the tree. +func TestRulesApplyOnlyWhereTheExclusionsLeaveThem(t *testing.T) { + got := analyzertest.RunInRepo(t, forbidigo.Analyzer, "cmd/bd", map[string]string{"x.go": labelUse}) + if len(got) != 1 || got[0].Line != 7 || !strings.HasPrefix(got[0].Message, "use of `u.LabelUseCase` forbidden because \"reach the label plane") { + t.Fatalf("diagnostics = %+v, want the LabelUseCase rule", got) + } + analyzertest.Equal(t, analyzertest.RunInRepo(t, forbidigo.Analyzer, "internal/other", map[string]string{"x.go": labelUse}), nil) +} + +// run.tests: false: a test compile unit is not analyzed at all. +func TestTestUnitsAreSkipped(t *testing.T) { + got := analyzertest.RunInRepo(t, forbidigo.Analyzer, "cmd/bd", map[string]string{ + "x.go": labelUse, + "x_test.go": "package main\n", + }) + analyzertest.Equal(t, got, nil) +} diff --git a/tools/nogo/analyzers/gosec/BUILD.bazel b/tools/nogo/analyzers/gosec/BUILD.bazel new file mode 100644 index 000000000..0f13fb1c9 --- /dev/null +++ b/tools/nogo/analyzers/gosec/BUILD.bazel @@ -0,0 +1,49 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "gosec", + srcs = ["gosec.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/gosec", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "//tools/nogo/internal/position", + "@com_github_securego_gosec_v2//:gosec", + "@com_github_securego_gosec_v2//analyzers", + "@com_github_securego_gosec_v2//issue", + "@com_github_securego_gosec_v2//rules", + "@org_golang_x_tools//go/analysis", + "@org_golang_x_tools//go/packages", + ], +) + +go_test( + name = "gosec_test", + srcs = ["gosec_test.go"], + deps = [ + ":gosec", + "//tools/nogo/internal/analyzertest", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/gosec/gosec.go b/tools/nogo/analyzers/gosec/gosec.go new file mode 100644 index 000000000..aeb80d73b --- /dev/null +++ b/tools/nogo/analyzers/gosec/gosec.go @@ -0,0 +1,113 @@ +// Package gosec runs github.com/securego/gosec/v2 as golangci-lint's gosec +// linter does, with .golangci.yml's linters.settings.gosec: every rule and +// SSA analyzer (less G407, which golangci-lint always drops), #nosec honored, +// and findings reported as ": ". +package gosec + +import ( + "fmt" + "go/token" + "io" + "log" + "strconv" + "strings" + + "github.com/securego/gosec/v2" + "github.com/securego/gosec/v2/analyzers" + "github.com/securego/gosec/v2/issue" + "github.com/securego/gosec/v2/rules" + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/packages" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" + "github.com/steveyegge/beads/tools/nogo/internal/position" +) + +// Analyzer reports security problems. +var Analyzer = golangci.Wrap("gosec", &analysis.Analyzer{ + Name: "gosec", + Doc: "inspects source code for security problems", + Run: run, +}) + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + settings := cfg.Linters.Settings.Gosec + severity, err := score(settings.Severity) + if err != nil { + return nil, err + } + confidence, err := score(settings.Confidence) + if err != nil { + return nil, err + } + // golangci-lint drops G407 (securego/gosec#1209, #1211) on top of the + // configured excludes. + excludes := append([]string{"G407"}, settings.Excludes...) + var ruleFilters []rules.RuleFilter + var analyzerFilters []analyzers.AnalyzerFilter + if len(settings.Includes) > 0 { + ruleFilters = append(ruleFilters, rules.NewRuleFilter(false, settings.Includes...)) + analyzerFilters = append(analyzerFilters, analyzers.NewAnalyzerFilter(false, settings.Includes...)) + } + ruleFilters = append(ruleFilters, rules.NewRuleFilter(true, excludes...)) + analyzerFilters = append(analyzerFilters, analyzers.NewAnalyzerFilter(true, excludes...)) + + g := gosec.NewAnalyzer(gosec.NewConfig(), true, false, false, 1, log.New(io.Discard, "", 0)) + ruleDefs := rules.Generate(false, ruleFilters...) + analyzerDefs := analyzers.Generate(false, analyzerFilters...) + g.LoadRules(ruleDefs.RulesInfo()) + g.LoadAnalyzers(analyzerDefs.AnalyzersInfo()) + + pkg := &packages.Package{ + Fset: pass.Fset, + Syntax: pass.Files, + Types: pass.Pkg, + TypesInfo: pass.TypesInfo, + } + g.CheckRules(pkg) + g.CheckAnalyzers(pkg) + found, _, _ := g.Report() + + idx := position.NewIndex(pass) + for _, i := range found { + if i.Severity < severity || i.Confidence < confidence { + continue + } + line, err := strconv.Atoi(i.Line) + if err != nil { + // A multi-line finding: "from-to". + first, _, ok := strings.Cut(i.Line, "-") + if line, err = strconv.Atoi(first); !ok || err != nil { + return nil, fmt.Errorf("gosec: line %q of %s in %s", i.Line, i.RuleID, i.File) + } + } + col, err := strconv.Atoi(i.Col) + if err != nil { + return nil, fmt.Errorf("gosec: column %q of %s in %s", i.Col, i.RuleID, i.File) + } + pos, ok := idx.Pos(token.Position{Filename: i.File, Line: line, Column: col}) + if !ok { + continue + } + pass.Report(analysis.Diagnostic{Pos: pos, Message: fmt.Sprintf("%s: %s", i.RuleID, i.What)}) + } + return nil, nil +} + +// score is golangci-lint's convertToScore: empty means low (everything). +func score(s string) (issue.Score, error) { + switch strings.ToLower(s) { + case "", "low": + return issue.Low, nil + case "medium": + return issue.Medium, nil + case "high": + return issue.High, nil + default: + return issue.Low, fmt.Errorf("gosec: severity/confidence %q: want low, medium or high", s) + } +} diff --git a/tools/nogo/analyzers/gosec/gosec_test.go b/tools/nogo/analyzers/gosec/gosec_test.go new file mode 100644 index 000000000..ad9a750c0 --- /dev/null +++ b/tools/nogo/analyzers/gosec/gosec_test.go @@ -0,0 +1,25 @@ +package gosec_test + +import ( + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/gosec" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" +) + +// golangci-lint v2.10.1 reports the same finding for the same package. +func TestReportsRuleIDAndText(t *testing.T) { + got := analyzertest.RunInRepo(t, gosec.Analyzer, "p", map[string]string{ + "p.go": "package p\n\nconst apiKey = \"AKIAIOSFODNN7EXAMPLE\"\n\n// G115 is excluded tree-wide by .golangci.yml.\nfunc narrow(x int64) int32 { return int32(x) }\n", + }) + analyzertest.Equal(t, got, []analyzertest.Diagnostic{ + {File: "p.go", Line: 3, Message: "G101: Potential hardcoded credentials"}, + }) +} + +func TestNosecIsHonored(t *testing.T) { + got := analyzertest.RunInRepo(t, gosec.Analyzer, "p", map[string]string{ + "p.go": "package p\n\nconst apiKey = \"AKIAIOSFODNN7EXAMPLE\" // #nosec G101 -- fixture\n", + }) + analyzertest.Equal(t, got, nil) +} diff --git a/tools/nogo/analyzers/misspell/BUILD.bazel b/tools/nogo/analyzers/misspell/BUILD.bazel new file mode 100644 index 000000000..0b2053638 --- /dev/null +++ b/tools/nogo/analyzers/misspell/BUILD.bazel @@ -0,0 +1,44 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "misspell", + srcs = ["misspell.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/misspell", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "@com_github_golangci_misspell//:misspell", + "@org_golang_x_tools//go/analysis", + ], +) + +go_test( + name = "misspell_test", + srcs = ["misspell_test.go"], + deps = [ + ":misspell", + "//tools/nogo/internal/analyzertest", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/misspell/misspell.go b/tools/nogo/analyzers/misspell/misspell.go new file mode 100644 index 000000000..2a1cb9819 --- /dev/null +++ b/tools/nogo/analyzers/misspell/misspell.go @@ -0,0 +1,110 @@ +// Package misspell runs github.com/golangci/misspell as golangci-lint's +// misspell linter does, with .golangci.yml's linters.settings.misspell. +// misspell is a library with no analysis.Analyzer. +package misspell + +import ( + "fmt" + "go/token" + "os" + "strings" + "sync" + "unicode" + + "github.com/golangci/misspell" + "golang.org/x/tools/go/analysis" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +// Analyzer reports commonly misspelled English words. +var Analyzer = golangci.Wrap("misspell", &analysis.Analyzer{ + Name: "misspell", + Doc: "reports commonly misspelled English words", + Run: run, +}) + +var ( + once sync.Once + replacer *misspell.Replacer + replacerErr error +) + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + settings := cfg.Linters.Settings.Misspell + once.Do(func() { replacer, replacerErr = NewReplacer(settings) }) + if replacerErr != nil { + return nil, replacerErr + } + // The default mode checks the whole file text; "restricted" only + // comments. + replace := replacer.Replace + if strings.EqualFold(settings.Mode, "restricted") { + replace = replacer.ReplaceGo + } + for _, file := range pass.Files { + tf := pass.Fset.File(file.Pos()) + if !strings.HasSuffix(tf.Name(), ".go") { + continue + } + content, err := os.ReadFile(tf.Name()) + if err != nil { + return nil, fmt.Errorf("misspell: reading %s: %w", tf.Name(), err) + } + _, diffs := replace(string(content)) + for _, diff := range diffs { + if diff.Line < 1 || diff.Line > tf.LineCount() { + continue + } + start := tf.LineStart(diff.Line) + token.Pos(diff.Column) + end := start + token.Pos(len(diff.Original)) + pass.Report(analysis.Diagnostic{ + Pos: start, + End: end, + Message: fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, diff.Corrected), + SuggestedFixes: []analysis.SuggestedFix{{ + Message: "fix spelling", + TextEdits: []analysis.TextEdit{{Pos: start, End: end, NewText: []byte(diff.Corrected)}}, + }}, + }) + } + } + return nil, nil +} + +// NewReplacer builds golangci-lint's misspell replacer for the settings. +func NewReplacer(s golangci.MisspellSettings) (*misspell.Replacer, error) { + r := &misspell.Replacer{Replacements: misspell.DictMain} + switch strings.ToUpper(s.Locale) { + case "": + case "US": + r.AddRuleList(misspell.DictAmerican) + case "UK", "GB": + r.AddRuleList(misspell.DictBritish) + default: + return nil, fmt.Errorf("misspell: unknown locale %q", s.Locale) + } + if len(s.ExtraWords) > 0 { + extra := make([]string, 0, len(s.ExtraWords)*2) + for _, w := range s.ExtraWords { + if w.Typo == "" || w.Correction == "" { + return nil, fmt.Errorf("misspell: extra word typo %q / correction %q must not be empty", w.Typo, w.Correction) + } + notLetter := func(r rune) bool { return !unicode.IsLetter(r) } + if strings.ContainsFunc(w.Typo, notLetter) || strings.ContainsFunc(w.Correction, notLetter) { + return nil, fmt.Errorf("misspell: extra word %q -> %q must contain only letters", w.Typo, w.Correction) + } + extra = append(extra, strings.ToLower(w.Typo), strings.ToLower(w.Correction)) + } + r.AddRuleList(extra) + } + if len(s.IgnoreRules) > 0 { + r.RemoveRule(s.IgnoreRules) + } + r.Compile() + return r, nil +} diff --git a/tools/nogo/analyzers/misspell/misspell_test.go b/tools/nogo/analyzers/misspell/misspell_test.go new file mode 100644 index 000000000..3bdbcd50c --- /dev/null +++ b/tools/nogo/analyzers/misspell/misspell_test.go @@ -0,0 +1,35 @@ +package misspell_test + +import ( + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/misspell" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" +) + +// The fixture words are assembled from halves so this file does not itself +// trip the analyzer it tests. +var ( + receive = "rec" + "ieve" + color = "col" + "our" + cancelled = "cancel" + "led" +) + +func TestReportsMisspellingsAnywhereInTheFileUSLocale(t *testing.T) { + got := analyzertest.RunInRepo(t, misspell.Analyzer, "p", map[string]string{ + "p.go": "// Package p is fine.\npackage p\n\n// " + receive + " is misspelled in a comment.\nvar s = \"" + color + "\"\n", + }) + analyzertest.Equal(t, got, []analyzertest.Diagnostic{ + {File: "p.go", Line: 4, Message: "`" + receive + "` is a misspelling of `receive`"}, + {File: "p.go", Line: 5, Message: "`" + color + "` is a misspelling of `color`"}, + }) +} + +// .golangci.yml keeps GitHub's spelling in cmd/bd/gate.go only. +func TestPathTextExclusion(t *testing.T) { + src := map[string]string{"gate.go": "package main\n\nconst status = \"" + cancelled + "\"\n"} + analyzertest.Equal(t, analyzertest.RunInRepo(t, misspell.Analyzer, "cmd/bd", src), nil) + analyzertest.Equal(t, analyzertest.RunInRepo(t, misspell.Analyzer, "cmd/other", src), []analyzertest.Diagnostic{ + {File: "gate.go", Line: 3, Message: "`" + cancelled + "` is a misspelling of `canceled`"}, + }) +} diff --git a/tools/nogo/analyzers/sloglint/BUILD.bazel b/tools/nogo/analyzers/sloglint/BUILD.bazel new file mode 100644 index 000000000..9289cddb1 --- /dev/null +++ b/tools/nogo/analyzers/sloglint/BUILD.bazel @@ -0,0 +1,46 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "sloglint", + srcs = ["sloglint.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/sloglint", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "@org_go_simpler_sloglint//:sloglint", + "@org_golang_x_tools//go/analysis", + "@org_golang_x_tools//go/analysis/passes/inspect", + ], +) + +go_test( + name = "sloglint_test", + srcs = ["sloglint_test.go"], + deps = [ + ":sloglint", + "//tools/nogo/internal/analyzertest", + "//tools/nogo/internal/golangci", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/sloglint/sloglint.go b/tools/nogo/analyzers/sloglint/sloglint.go new file mode 100644 index 000000000..32e5270d2 --- /dev/null +++ b/tools/nogo/analyzers/sloglint/sloglint.go @@ -0,0 +1,67 @@ +// Package sloglint runs go-simpler.org/sloglint with .golangci.yml's +// linters.settings.sloglint and golangci-lint's defaults. +package sloglint + +import ( + "strings" + "sync" + + "go-simpler.org/sloglint" + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +// Analyzer checks log/slog calls for a consistent style. +var Analyzer = golangci.Wrap("sloglint", &analysis.Analyzer{ + Name: "sloglint", + Doc: "ensures a consistent code style when using log/slog", + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Run: run, +}) + +var ( + once sync.Once + inner *analysis.Analyzer +) + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + once.Do(func() { inner = sloglint.New(Options(cfg.Linters.Settings.Sloglint)) }) + return inner.Run(WithModule(pass)) +} + +// WithModule returns pass with Module set when nogo left it nil (rules_go does +// not set it; sloglint reads Module.GoVersion). The language version is the +// package's, which rules_go sets from go.mod's go directive, as golangci-lint +// reads the module's. +func WithModule(pass *analysis.Pass) *analysis.Pass { + if pass.Module != nil { + return pass + } + p := *pass + p.Module = &analysis.Module{GoVersion: strings.TrimPrefix(pass.Pkg.GoVersion(), "go")} + return &p +} + +// Options maps the settings onto sloglint's options; no-mixed-args defaults +// to true, as in golangci-lint. +func Options(s golangci.SloglintSettings) *sloglint.Options { + return &sloglint.Options{ + NoMixedArgs: s.NoMixedArgs == nil || *s.NoMixedArgs, + KVOnly: s.KVOnly, + AttrOnly: s.AttrOnly, + NoGlobal: s.NoGlobal, + ContextOnly: s.Context, + StaticMsg: s.StaticMsg, + MsgStyle: s.MsgStyle, + NoRawKeys: s.NoRawKeys, + KeyNamingCase: s.KeyNamingCase, + ForbiddenKeys: s.ForbiddenKeys, + ArgsOnSepLines: s.ArgsOnSepLines, + } +} diff --git a/tools/nogo/analyzers/sloglint/sloglint_test.go b/tools/nogo/analyzers/sloglint/sloglint_test.go new file mode 100644 index 000000000..8e6d52e43 --- /dev/null +++ b/tools/nogo/analyzers/sloglint/sloglint_test.go @@ -0,0 +1,26 @@ +package sloglint_test + +import ( + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/sloglint" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +func TestOptionsDefaultLikeGolangciLint(t *testing.T) { + opts := sloglint.Options(golangci.SloglintSettings{}) + if !opts.NoMixedArgs || opts.KVOnly || opts.AttrOnly || opts.StaticMsg { + t.Errorf("default options = %+v, want golangci-lint's (no-mixed-args only)", opts) + } + off := false + if sloglint.Options(golangci.SloglintSettings{NoMixedArgs: &off}).NoMixedArgs { + t.Error("no-mixed-args: false was not honored") + } +} + +// nogo passes carry no Module; sloglint reads its Go version. +func TestRunsWithoutAModule(t *testing.T) { + got := analyzertest.RunInRepo(t, sloglint.Analyzer, "p", map[string]string{"p.go": "package p\n\nfunc f() {}\n"}) + analyzertest.Equal(t, got, nil) +} diff --git a/tools/nogo/analyzers/unconvert/BUILD.bazel b/tools/nogo/analyzers/unconvert/BUILD.bazel new file mode 100644 index 000000000..2f988fd1a --- /dev/null +++ b/tools/nogo/analyzers/unconvert/BUILD.bazel @@ -0,0 +1,45 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "unconvert", + srcs = ["unconvert.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/unconvert", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "//tools/nogo/internal/position", + "@com_github_golangci_unconvert//:unconvert", + "@org_golang_x_tools//go/analysis", + ], +) + +go_test( + name = "unconvert_test", + srcs = ["unconvert_test.go"], + deps = [ + ":unconvert", + "//tools/nogo/internal/analyzertest", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/unconvert/unconvert.go b/tools/nogo/analyzers/unconvert/unconvert.go new file mode 100644 index 000000000..182f1d38e --- /dev/null +++ b/tools/nogo/analyzers/unconvert/unconvert.go @@ -0,0 +1,42 @@ +// Package unconvert runs golangci-lint's fork of mdempsky/unconvert, which +// exposes Run(pass) rather than an analysis.Analyzer. +package unconvert + +import ( + "sync" + + "github.com/golangci/unconvert" + "golang.org/x/tools/go/analysis" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" + "github.com/steveyegge/beads/tools/nogo/internal/position" +) + +// Analyzer reports type conversions whose operand already has the target type. +var Analyzer = golangci.Wrap("unconvert", &analysis.Analyzer{ + Name: "unconvert", + Doc: "reports unnecessary type conversions", + Run: run, +}) + +var configure sync.Once + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + configure.Do(func() { + unconvert.SetFastMath(cfg.Linters.Settings.Unconvert.FastMath) + unconvert.SetSafe(cfg.Linters.Settings.Unconvert.Safe) + }) + idx := position.NewIndex(pass) + for _, p := range unconvert.Run(pass) { + pos, ok := idx.Pos(p) + if !ok { + continue + } + pass.Report(analysis.Diagnostic{Pos: pos, Message: "unnecessary conversion"}) + } + return nil, nil +} diff --git a/tools/nogo/analyzers/unconvert/unconvert_test.go b/tools/nogo/analyzers/unconvert/unconvert_test.go new file mode 100644 index 000000000..412d29fce --- /dev/null +++ b/tools/nogo/analyzers/unconvert/unconvert_test.go @@ -0,0 +1,15 @@ +package unconvert_test + +import ( + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/unconvert" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" +) + +func TestReportsUnnecessaryConversions(t *testing.T) { + got := analyzertest.RunInRepo(t, unconvert.Analyzer, "p", map[string]string{ + "p.go": "package p\n\nfunc f(x int) int { return int(x) }\n\nfunc g(x int32) int64 { return int64(x) }\n", + }) + analyzertest.Equal(t, got, []analyzertest.Diagnostic{{File: "p.go", Line: 3, Message: "unnecessary conversion"}}) +} diff --git a/tools/nogo/analyzers/unparam/BUILD.bazel b/tools/nogo/analyzers/unparam/BUILD.bazel new file mode 100644 index 000000000..f4c612814 --- /dev/null +++ b/tools/nogo/analyzers/unparam/BUILD.bazel @@ -0,0 +1,46 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "unparam", + srcs = ["unparam.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/analyzers/unparam", + visibility = ["//visibility:public"], + deps = [ + "//tools/nogo/internal/golangci", + "@cc_mvdan_unparam//check", + "@org_golang_x_tools//go/analysis", + "@org_golang_x_tools//go/analysis/passes/buildssa", + "@org_golang_x_tools//go/packages", + ], +) + +go_test( + name = "unparam_test", + srcs = ["unparam_test.go"], + deps = [ + ":unparam", + "//tools/nogo/internal/analyzertest", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/analyzers/unparam/unparam.go b/tools/nogo/analyzers/unparam/unparam.go new file mode 100644 index 000000000..864bf1c54 --- /dev/null +++ b/tools/nogo/analyzers/unparam/unparam.go @@ -0,0 +1,47 @@ +// Package unparam runs mvdan.cc/unparam as golangci-lint's unparam linter +// does, with .golangci.yml's linters.settings.unparam. +package unparam + +import ( + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/buildssa" + "golang.org/x/tools/go/packages" + "mvdan.cc/unparam/check" + + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +// Analyzer reports function parameters and results that are always unused +// or always receive the same value. +var Analyzer = golangci.Wrap("unparam", &analysis.Analyzer{ + Name: "unparam", + Doc: "reports unused function parameters and results", + Requires: []*analysis.Analyzer{buildssa.Analyzer}, + Run: run, +}) + +func run(pass *analysis.Pass) (any, error) { + cfg, err := golangci.Load() + if err != nil { + return nil, err + } + ssa := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) + pkg := &packages.Package{ + Fset: pass.Fset, + Syntax: pass.Files, + Types: pass.Pkg, + TypesInfo: pass.TypesInfo, + } + c := &check.Checker{} + c.CheckExportedFuncs(cfg.Linters.Settings.Unparam.CheckExported) + c.Packages([]*packages.Package{pkg}) + c.ProgramSSA(ssa.Pkg.Prog) + issues, err := c.Check() + if err != nil { + return nil, err + } + for _, issue := range issues { + pass.Report(analysis.Diagnostic{Pos: issue.Pos(), Message: issue.Message()}) + } + return nil, nil +} diff --git a/tools/nogo/analyzers/unparam/unparam_test.go b/tools/nogo/analyzers/unparam/unparam_test.go new file mode 100644 index 000000000..2ea84500b --- /dev/null +++ b/tools/nogo/analyzers/unparam/unparam_test.go @@ -0,0 +1,46 @@ +package unparam_test + +import ( + "testing" + + "github.com/steveyegge/beads/tools/nogo/analyzers/unparam" + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" +) + +const lib = `package p + +func render(n int, unused string) int { + if n > 1 { + return n * 2 + } + return n +} + +// Use is exported, so unparam does not check it (check-exported is off). +func Use(x int) int { return render(1, "a") + render(2, "b") } +` + +// golangci-lint v2.10.1 reports the same finding for the same package (a +// body this small but no smaller: unparam skips trivial functions). +func TestReportsUnusedParameters(t *testing.T) { + analyzertest.Equal(t, analyzertest.RunInRepo(t, unparam.Analyzer, "p", map[string]string{"p.go": lib}), + []analyzertest.Diagnostic{{File: "p.go", Line: 3, Message: "render - unused is unused"}}) +} + +// run.tests: false: golangci-lint judged the package without its tests, +// which is Bazel's library unit; the test unit is not analyzed. +func TestTestUnitIsSkipped(t *testing.T) { + got := analyzertest.RunInRepo(t, unparam.Analyzer, "p", map[string]string{ + "p.go": lib, + "p_test.go": "package p\n\nvar _ = render(1, \"c\")\n", + }) + analyzertest.Equal(t, got, nil) +} + +// .golangci.yml drops unparam's getConfig findings in the tracker adapters. +func TestPathTextExclusion(t *testing.T) { + src := map[string]string{"tracker.go": "package jira\n\nfunc getConfig(k string, unused int) string {\n\tif k == \"\" {\n\t\treturn \"none\"\n\t}\n\treturn k\n}\n\n// Use is exported.\nfunc Use() string { return getConfig(\"a\", 1) + getConfig(\"b\", 2) }\n"} + analyzertest.Equal(t, analyzertest.RunInRepo(t, unparam.Analyzer, "internal/jira", src), nil) + analyzertest.Equal(t, analyzertest.RunInRepo(t, unparam.Analyzer, "internal/other", src), + []analyzertest.Diagnostic{{File: "tracker.go", Line: 3, Message: "getConfig - unused is unused"}}) +} diff --git a/tools/nogo/analyzers_test.go b/tools/nogo/analyzers_test.go new file mode 100644 index 000000000..a36b5ca5e --- /dev/null +++ b/tools/nogo/analyzers_test.go @@ -0,0 +1,104 @@ +package nogo_test + +import ( + "os" + "regexp" + "slices" + "strings" + "testing" + + "golang.org/x/tools/go/analysis" + + "github.com/steveyegge/beads/tools/nogo/analyzers/depguard" + "github.com/steveyegge/beads/tools/nogo/analyzers/errcheck" + "github.com/steveyegge/beads/tools/nogo/analyzers/forbidigo" + "github.com/steveyegge/beads/tools/nogo/analyzers/gosec" + "github.com/steveyegge/beads/tools/nogo/analyzers/misspell" + "github.com/steveyegge/beads/tools/nogo/analyzers/sloglint" + "github.com/steveyegge/beads/tools/nogo/analyzers/unconvert" + "github.com/steveyegge/beads/tools/nogo/analyzers/unparam" + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +// starlarkList returns the string elements of `name = [...]` in analyzers.bzl. +func starlarkList(t *testing.T, name string) []string { + t.Helper() + data, err := os.ReadFile("analyzers.bzl") + if err != nil { + t.Fatal(err) + } + m := regexp.MustCompile(`(?ms)^` + name + ` = \[(.*?)^\]`).FindSubmatch(data) + if m == nil { + t.Fatalf("analyzers.bzl has no %s list", name) + } + var out []string + for _, s := range regexp.MustCompile(`"([^"]+)"`).FindAllSubmatch(m[1], -1) { + out = append(out, string(s[1])) + } + return out +} + +// TestGolangciLintersAreTheEnabledOnes keeps analyzers.bzl's GOLANGCI_LINTERS +// equal to .golangci.yml's linters.enable: a linter enabled there and missing +// here would silently stop gating, and one listed here but not enabled would +// do nothing (Wrap skips it). +func TestGolangciLintersAreTheEnabledOnes(t *testing.T) { + data, err := os.ReadFile("../../.golangci.yml") + if err != nil { + t.Fatal(err) + } + cfg, err := golangci.Parse(data) + if err != nil { + t.Fatal(err) + } + enabled := slices.Sorted(slices.Values(cfg.Linters.Enable)) + listed := slices.Sorted(slices.Values(starlarkList(t, "GOLANGCI_LINTERS"))) + if !slices.Equal(enabled, listed) { + t.Errorf(".golangci.yml enables %v; analyzers.bzl's GOLANGCI_LINTERS is %v", enabled, listed) + } +} + +// Each wrapper is named after its golangci-lint linter, which is what +// //nolint: directives and .golangci.yml's exclusion rules name. +func TestWrapperNamesAreLinterNames(t *testing.T) { + for _, a := range []*analysis.Analyzer{ + depguard.Analyzer, errcheck.Analyzer, forbidigo.Analyzer, gosec.Analyzer, + misspell.Analyzer, sloglint.Analyzer, unconvert.Analyzer, unparam.Analyzer, + } { + if !slices.Contains(starlarkList(t, "GOLANGCI_LINTERS"), a.Name) { + t.Errorf("analyzer %q is not a GOLANGCI_LINTERS entry", a.Name) + } + } +} + +// The embedded copy Wrap applies is the repository's .golangci.yml. +func TestEmbeddedConfigIsTheRepositoryConfig(t *testing.T) { + data, err := os.ReadFile("../../.golangci.yml") + if err != nil { + t.Fatal(err) + } + want, err := golangci.Parse(data) + if err != nil { + t.Fatal(err) + } + got, err := golangci.Load() + if err != nil { + t.Fatal(err) + } + if !slices.Equal(got.Linters.Enable, want.Linters.Enable) || len(got.Linters.Exclusions.Rules) != len(want.Linters.Exclusions.Rules) { + t.Errorf("embedded .golangci.yml differs from the repository's") + } +} + +// config.json's _base scope: external repos, Bazel outputs and testdata only. +func TestBaseScope(t *testing.T) { + data, err := os.ReadFile("config.json") + if err != nil { + t.Fatal(err) + } + for _, want := range []string{`"^external/"`, `"(^|/)bazel-out/"`, `"(^|/)testdata/"`} { + if !strings.Contains(string(data), want) { + t.Errorf("config.json does not exclude %s", want) + } + } +} diff --git a/tools/nogo/config.json b/tools/nogo/config.json new file mode 100644 index 000000000..0c990c3a5 --- /dev/null +++ b/tools/nogo/config.json @@ -0,0 +1,10 @@ +{ + "_base": { + "description": "Scope every analyzer to first-party code as `go vet ./...` and golangci-lint loaded it: no external repos, Bazel-generated sources or testdata trees (./... skips them). The golangci-lint linters additionally apply .golangci.yml's run.tests, generated-file and exclusion rules themselves (analyzers/internal/golangci).", + "exclude_files": { + "^external/": "third-party module repositories", + "(^|/)bazel-out/": "Bazel-generated sources (test mains, cgo, embedcfg)", + "(^|/)testdata/": "fixtures; ./... never loaded them" + } + } +} diff --git a/tools/nogo/go.mod b/tools/nogo/go.mod new file mode 100644 index 000000000..936b993ad --- /dev/null +++ b/tools/nogo/go.mod @@ -0,0 +1,26 @@ +module github.com/steveyegge/beads/tools/nogo + +go 1.26.5 + +require ( + github.com/OpenPeeDeeP/depguard/v2 v2.2.1 + github.com/ashanbrown/forbidigo/v2 v2.3.0 + github.com/golangci/misspell v0.8.0 + github.com/golangci/unconvert v0.0.0-20250410112200-a129a6e6413e + github.com/kisielk/errcheck v1.9.0 + github.com/securego/gosec/v2 v2.23.0 + go-simpler.org/sloglint v0.11.1 + golang.org/x/mod v0.40.0 + golang.org/x/tools v0.49.0 + gopkg.in/yaml.v3 v3.0.1 + mvdan.cc/unparam v0.0.0-20251027182757-5beb8c8f8f15 +) + +require ( + github.com/ccojocar/zxcvbn-go v1.0.4 // indirect + github.com/ettle/strcase v0.2.0 // indirect + github.com/gobwas/glob v0.2.3 // indirect + github.com/google/go-cmp v0.7.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/text v0.41.0 // indirect +) diff --git a/tools/nogo/go.sum b/tools/nogo/go.sum new file mode 100644 index 000000000..c54a38928 --- /dev/null +++ b/tools/nogo/go.sum @@ -0,0 +1,60 @@ +github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0= +github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM= +github.com/OpenPeeDeeP/depguard/v2 v2.2.1 h1:vckeWVESWp6Qog7UZSARNqfu/cZqvki8zsuj3piCMx4= +github.com/OpenPeeDeeP/depguard/v2 v2.2.1/go.mod h1:q4DKzC4UcVaAvcfd41CZh0PWpGgzrVxUYBlgKNGquUo= +github.com/ashanbrown/forbidigo/v2 v2.3.0 h1:OZZDOchCgsX5gvToVtEBoV2UWbFfI6RKQTir2UZzSxo= +github.com/ashanbrown/forbidigo/v2 v2.3.0/go.mod h1:5p6VmsG5/1xx3E785W9fouMxIOkvY2rRV9nMdWadd6c= +github.com/ccojocar/zxcvbn-go v1.0.4 h1:FWnCIRMXPj43ukfX000kvBZvV6raSxakYr1nzyNrUcc= +github.com/ccojocar/zxcvbn-go v1.0.4/go.mod h1:3GxGX+rHmueTUMvm5ium7irpyjmm7ikxYFOSJB21Das= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ettle/strcase v0.2.0 h1:fGNiVF21fHXpX1niBgk0aROov1LagYsOwV/xqKDKR/Q= +github.com/ettle/strcase v0.2.0/go.mod h1:DajmHElDSaX76ITe3/VHVyMin4LWSJN5Z909Wp+ED1A= +github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= +github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI= +github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= +github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= +github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= +github.com/golangci/misspell v0.8.0 h1:qvxQhiE2/5z+BVRo1kwYA8yGz+lOlu5Jfvtx2b04Jbg= +github.com/golangci/misspell v0.8.0/go.mod h1:WZyyI2P3hxPY2UVHs3cS8YcllAeyfquQcKfdeE9AFVg= +github.com/golangci/unconvert v0.0.0-20250410112200-a129a6e6413e h1:gD6P7NEo7Eqtt0ssnqSJNNndxe69DOQ24A5h7+i3KpM= +github.com/golangci/unconvert v0.0.0-20250410112200-a129a6e6413e/go.mod h1:h+wZwLjUTJnm/P2rwlbJdRPZXOzaT36/FwnPnY2inzc= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 h1:z2ogiKUYzX5Is6zr/vP9vJGqPwcdqsWjOt+V8J7+bTc= +github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI= +github.com/kisielk/errcheck v1.9.0 h1:9xt1zI9EBfcYBvdU1nVrzMzzUPUtPKs9bVSIM3TAb3M= +github.com/kisielk/errcheck v1.9.0/go.mod h1:kQxWMMVZgIkDq7U8xtG/n2juOjbLgZtedi0D+/VL/i8= +github.com/onsi/ginkgo/v2 v2.28.1 h1:S4hj+HbZp40fNKuLUQOYLDgZLwNUVn19N3Atb98NCyI= +github.com/onsi/ginkgo/v2 v2.28.1/go.mod h1:CLtbVInNckU3/+gC8LzkGUb9oF+e8W8TdUsxPwvdOgE= +github.com/onsi/gomega v1.39.1 h1:1IJLAad4zjPn2PsnhH70V4DKRFlrCzGBNrNaru+Vf28= +github.com/onsi/gomega v1.39.1/go.mod h1:hL6yVALoTOxeWudERyfppUcZXjMwIMLnuSfruD2lcfg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/securego/gosec/v2 v2.23.0 h1:h4TtF64qFzvnkqvsHC/knT7YC5fqyOCItlVR8+ptEBo= +github.com/securego/gosec/v2 v2.23.0/go.mod h1:qRHEgXLFuYUDkI2T7W7NJAmOkxVhkR0x9xyHOIcMNZ0= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +go-simpler.org/sloglint v0.11.1 h1:xRbPepLT/MHPTCA6TS/wNfZrDzkGvCCqUv4Bdwc3H7s= +go-simpler.org/sloglint v0.11.1/go.mod h1:2PowwiCOK8mjiF+0KGifVOT8ZsCNiFzvfyJeJOIt8MQ= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/mod v0.40.0 h1:hUv+3cXcdRHz08UmSiOob7sadHig73uo5bkXxQ/tvUs= +golang.org/x/mod v0.40.0/go.mod h1:0/weTWkPWGBikyTWAX3dkjVztMmBA5hM0DH6BElSupE= +golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= +golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= +golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= +golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI= +golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +mvdan.cc/unparam v0.0.0-20251027182757-5beb8c8f8f15 h1:ssMzja7PDPJV8FStj7hq9IKiuiKhgz9ErWw+m68e7DI= +mvdan.cc/unparam v0.0.0-20251027182757-5beb8c8f8f15/go.mod h1:4M5MMXl2kW6fivUT6yRGpLLPNfuGtU2Z0cPvFquGDYU= diff --git a/tools/nogo/internal/analyzertest/BUILD.bazel b/tools/nogo/internal/analyzertest/BUILD.bazel new file mode 100644 index 000000000..c7734cf4d --- /dev/null +++ b/tools/nogo/internal/analyzertest/BUILD.bazel @@ -0,0 +1,35 @@ +load("@rules_go//go:def.bzl", "go_library") + +go_library( + name = "analyzertest", + srcs = ["analyzertest.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/internal/analyzertest", + visibility = ["//tools/nogo:__subpackages__"], + deps = [ + "@org_golang_x_tools//go/analysis", + "@org_golang_x_tools//go/analysis/checker", + "@org_golang_x_tools//go/packages", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/internal/analyzertest/analyzertest.go b/tools/nogo/internal/analyzertest/analyzertest.go new file mode 100644 index 000000000..2cdd6b54c --- /dev/null +++ b/tools/nogo/internal/analyzertest/analyzertest.go @@ -0,0 +1,151 @@ +// Package analyzertest runs an analyzer over a small self-contained package +// written to a temporary directory. Unlike x/tools' analysistest it needs no +// `go` command or GOPATH layout, so it works the same under `go test` and in +// a Bazel sandbox. +// +// Imports resolve to empty packages: a fixture may import any path (depguard +// only reads the import declarations), but cannot use what it imports. +package analyzertest + +import ( + "go/ast" + "go/parser" + "go/token" + "go/types" + "os" + "path" + "path/filepath" + "sort" + "testing" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/checker" + "golang.org/x/tools/go/packages" +) + +// Diagnostic is a reported finding, positioned by file base name and line. +type Diagnostic struct { + File string + Line int + Message string +} + +// Run writes files (base name to source) into dir/ of a fresh +// temporary directory, type-checks them as one package, runs a, and returns +// its diagnostics sorted by position. +func Run(t *testing.T, a *analysis.Analyzer, pkgPath string, files map[string]string) []Diagnostic { + t.Helper() + dir := filepath.Join(t.TempDir(), filepath.FromSlash(pkgPath)) + return run(t, a, pkgPath, dir, dir, files) +} + +// RunInRepo is Run for a package at relDir ("" for the root) of a fresh +// temporary repository. The test's working directory becomes the repository +// root and file names are repository-relative, as in a nogo action, where +// the execution root is the repository root. +func RunInRepo(t *testing.T, a *analysis.Analyzer, relDir string, files map[string]string) []Diagnostic { + t.Helper() + root := t.TempDir() + t.Chdir(root) + pkgPath := path.Join("example.com/repo", relDir) + return run(t, a, pkgPath, filepath.Join(root, filepath.FromSlash(relDir)), filepath.FromSlash(relDir), files) +} + +func run(t *testing.T, a *analysis.Analyzer, pkgPath, dir, nameDir string, files map[string]string) []Diagnostic { + t.Helper() + if err := os.MkdirAll(dir, 0o750); err != nil { + t.Fatal(err) + } + fset := token.NewFileSet() + var syntax []*ast.File + var names []string + for name := range files { + names = append(names, name) + } + sort.Strings(names) + var paths []string + for _, name := range names { + if err := os.WriteFile(filepath.Join(dir, name), []byte(files[name]), 0o600); err != nil { + t.Fatal(err) + } + fileName := filepath.Join(nameDir, name) + f, err := parser.ParseFile(fset, fileName, files[name], parser.ParseComments) + if err != nil { + t.Fatalf("parsing %s: %v", name, err) + } + syntax = append(syntax, f) + paths = append(paths, fileName) + } + info := &types.Info{ + Types: map[ast.Expr]types.TypeAndValue{}, + Instances: map[*ast.Ident]types.Instance{}, + Defs: map[*ast.Ident]types.Object{}, + Uses: map[*ast.Ident]types.Object{}, + Implicits: map[ast.Node]types.Object{}, + Selections: map[*ast.SelectorExpr]*types.Selection{}, + Scopes: map[ast.Node]*types.Scope{}, + FileVersions: map[*ast.File]string{}, + } + sizes := types.SizesFor("gc", "amd64") + conf := types.Config{GoVersion: "go1.26", Sizes: sizes, Importer: emptyImporter{}} + tpkg, err := conf.Check(pkgPath, fset, syntax, info) + if err != nil { + t.Fatalf("type-checking %s: %v", pkgPath, err) + } + pkg := &packages.Package{ + ID: pkgPath, + Name: tpkg.Name(), + PkgPath: pkgPath, + GoFiles: paths, + CompiledGoFiles: paths, + Fset: fset, + Syntax: syntax, + Types: tpkg, + TypesInfo: info, + TypesSizes: sizes, + Imports: map[string]*packages.Package{}, + } + graph, err := checker.Analyze([]*analysis.Analyzer{a}, []*packages.Package{pkg}, nil) + if err != nil { + t.Fatalf("analyzing: %v", err) + } + var out []Diagnostic + for _, act := range graph.Roots { + if act.Err != nil { + t.Fatalf("%s: %v", act.Analyzer.Name, act.Err) + } + for _, d := range act.Diagnostics { + p := fset.Position(d.Pos) + out = append(out, Diagnostic{File: filepath.Base(p.Filename), Line: p.Line, Message: d.Message}) + } + } + sort.Slice(out, func(i, j int) bool { + if out[i].File != out[j].File { + return out[i].File < out[j].File + } + return out[i].Line < out[j].Line + }) + return out +} + +// emptyImporter resolves every import to an empty, complete package. +type emptyImporter struct{} + +func (emptyImporter) Import(importPath string) (*types.Package, error) { + pkg := types.NewPackage(importPath, path.Base(importPath)) + pkg.MarkComplete() + return pkg, nil +} + +// Equal fails t unless got equals want. +func Equal(t *testing.T, got, want []Diagnostic) { + t.Helper() + if len(got) != len(want) { + t.Fatalf("diagnostics = %+v, want %+v", got, want) + } + for i := range want { + if got[i] != want[i] { + t.Errorf("diagnostic %d = %+v, want %+v", i, got[i], want[i]) + } + } +} diff --git a/tools/nogo/internal/golangci/BUILD.bazel b/tools/nogo/internal/golangci/BUILD.bazel new file mode 100644 index 000000000..835acc846 --- /dev/null +++ b/tools/nogo/internal/golangci/BUILD.bazel @@ -0,0 +1,57 @@ +load("@rules_go//go:def.bzl", "go_library", "go_test") + +# config.go embeds .golangci.yml; go:embed cannot reach outside the package, +# so Bazel copies it in. (The tools/nogo module is built by Bazel only.) +genrule( + name = "golangci_yml", + srcs = ["//:.golangci.yml"], + outs = ["golangci.yml"], + cmd = "cp $< $@", +) + +go_library( + name = "golangci", + srcs = [ + "config.go", + "wrap.go", + ], + embedsrcs = [":golangci.yml"], # keep + importpath = "github.com/steveyegge/beads/tools/nogo/internal/golangci", + visibility = ["//tools/nogo:__subpackages__"], + deps = [ + "@in_gopkg_yaml_v3//:yaml_v3", + "@org_golang_x_tools//go/analysis", + ], +) + +go_test( + name = "golangci_test", + srcs = ["wrap_test.go"], + deps = [ + ":golangci", + "//tools/nogo/internal/analyzertest", + "@org_golang_x_tools//go/analysis", + ], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/internal/golangci/config.go b/tools/nogo/internal/golangci/config.go new file mode 100644 index 000000000..1fecca691 --- /dev/null +++ b/tools/nogo/internal/golangci/config.go @@ -0,0 +1,307 @@ +// Package golangci applies the repository's .golangci.yml to nogo analyzers. +// +// .golangci.yml stays the single description of what the linters check: which +// are enabled, their settings, `run.tests`, generated-file handling and the +// path/text exclusion rules. Bazel copies it into this package (see +// BUILD.bazel), and Wrap gives every golangci-lint linter's nogo analyzer the +// semantics golangci-lint gave it, so editing .golangci.yml changes what the +// nogo build reports. +// +// Decoding is strict: a key this package does not implement fails every +// wrapped analyzer instead of being ignored, so the nogo build can never +// silently drop a setting golangci-lint would have honored. +package golangci + +import ( + "bytes" + _ "embed" // .golangci.yml, copied in by BUILD.bazel + "errors" + "fmt" + "regexp" + "slices" + "sync" + + "gopkg.in/yaml.v3" +) + +//go:embed golangci.yml +var embedded []byte + +// Config is the subset of golangci-lint v2's configuration this repository +// uses. Field names follow golangci-lint's keys. +type Config struct { + Version string `yaml:"version"` + Run RunSection `yaml:"run"` + Linters Linters `yaml:"linters"` + Issues Issues `yaml:"issues"` + + rules []compiledRule +} + +// RunSection is golangci-lint's `run` section. +type RunSection struct { + // Timeout bounds a golangci-lint run; nogo has no equivalent. + Timeout string `yaml:"timeout"` + // Tests reports whether test files are analyzed (golangci-lint's default: + // true). + Tests *bool `yaml:"tests"` +} + +// Issues is golangci-lint's `issues` section. Its keys shape golangci-lint's +// report output only; any finding still fails the nogo build. +type Issues struct { + UniqByLine *bool `yaml:"uniq-by-line"` +} + +// Linters is golangci-lint's `linters` section. +type Linters struct { + Default string `yaml:"default"` + Enable []string `yaml:"enable"` + Settings Settings `yaml:"settings"` + Exclusions Exclusions `yaml:"exclusions"` +} + +// Settings holds the settings of the linters this repository enables. +type Settings struct { + Depguard DepguardSettings `yaml:"depguard"` + Errcheck ErrcheckSettings `yaml:"errcheck"` + Forbidigo ForbidigoSettings `yaml:"forbidigo"` + Gosec GosecSettings `yaml:"gosec"` + Misspell MisspellSettings `yaml:"misspell"` + Sloglint SloglintSettings `yaml:"sloglint"` + Unconvert UnconvertSettings `yaml:"unconvert"` + Unparam UnparamSettings `yaml:"unparam"` +} + +// DepguardSettings is depguard's settings block. +type DepguardSettings struct { + Rules map[string]DepguardList `yaml:"rules"` +} + +// DepguardList is one named depguard rule. +type DepguardList struct { + ListMode string `yaml:"list-mode"` + Files []string `yaml:"files"` + Allow []string `yaml:"allow"` + Deny []DepguardDeny `yaml:"deny"` +} + +// DepguardDeny is one denied package prefix and the reason shown for it. +type DepguardDeny struct { + Pkg string `yaml:"pkg"` + Desc string `yaml:"desc"` +} + +// ErrcheckSettings is errcheck's settings block. +type ErrcheckSettings struct { + DisableDefaultExclusions bool `yaml:"disable-default-exclusions"` + CheckTypeAssertions bool `yaml:"check-type-assertions"` + CheckAssignToBlank bool `yaml:"check-blank"` + ExcludeFunctions []string `yaml:"exclude-functions"` + Verbose bool `yaml:"verbose"` +} + +// ForbidigoSettings is forbidigo's settings block. +type ForbidigoSettings struct { + Forbid []ForbidigoPattern `yaml:"forbid"` + // ExcludeGodocExamples defaults to true, as in golangci-lint. + ExcludeGodocExamples *bool `yaml:"exclude-godoc-examples"` + AnalyzeTypes bool `yaml:"analyze-types"` +} + +// ForbidigoPattern is one forbidden identifier pattern. +type ForbidigoPattern struct { + Pattern string `yaml:"pattern"` + Pkg string `yaml:"pkg,omitempty"` + Msg string `yaml:"msg,omitempty"` +} + +// GosecSettings is gosec's settings block. Only rule selection is supported; +// gosec's per-rule `config` map is not. +type GosecSettings struct { + Includes []string `yaml:"includes"` + Excludes []string `yaml:"excludes"` + Severity string `yaml:"severity"` + Confidence string `yaml:"confidence"` +} + +// MisspellSettings is misspell's settings block. +type MisspellSettings struct { + Mode string `yaml:"mode"` + Locale string `yaml:"locale"` + ExtraWords []MisspellExtraWord `yaml:"extra-words"` + IgnoreRules []string `yaml:"ignore-rules"` +} + +// MisspellExtraWord adds a typo and its correction. +type MisspellExtraWord struct { + Typo string `yaml:"typo"` + Correction string `yaml:"correction"` +} + +// SloglintSettings is sloglint's settings block. NoMixedArgs defaults to +// true, as in golangci-lint. +type SloglintSettings struct { + NoMixedArgs *bool `yaml:"no-mixed-args"` + KVOnly bool `yaml:"kv-only"` + AttrOnly bool `yaml:"attr-only"` + NoGlobal string `yaml:"no-global"` + Context string `yaml:"context"` + StaticMsg bool `yaml:"static-msg"` + MsgStyle string `yaml:"msg-style"` + NoRawKeys bool `yaml:"no-raw-keys"` + KeyNamingCase string `yaml:"key-naming-case"` + ForbiddenKeys []string `yaml:"forbidden-keys"` + ArgsOnSepLines bool `yaml:"args-on-sep-lines"` +} + +// UnconvertSettings is unconvert's settings block. +type UnconvertSettings struct { + FastMath bool `yaml:"fast-math"` + Safe bool `yaml:"safe"` +} + +// UnparamSettings is unparam's settings block. +type UnparamSettings struct { + CheckExported bool `yaml:"check-exported"` +} + +// Exclusions is golangci-lint v2's `linters.exclusions` section. +type Exclusions struct { + // Generated is "lax" (the default), "strict" or "disable". + Generated string `yaml:"generated"` + Rules []ExclusionRule `yaml:"rules"` +} + +// ExclusionRule drops the findings that match every condition it sets. +type ExclusionRule struct { + Path string `yaml:"path"` + PathExcept string `yaml:"path-except"` + Text string `yaml:"text"` + Linters []string `yaml:"linters"` +} + +// Generated-file modes of linters.exclusions.generated. +const ( + GeneratedLax = "lax" + GeneratedStrict = "strict" + GeneratedDisable = "disable" +) + +// Parse strictly decodes a golangci-lint v2 configuration and validates it. +func Parse(data []byte) (*Config, error) { + dec := yaml.NewDecoder(bytes.NewReader(data)) + dec.KnownFields(true) + var c Config + if err := dec.Decode(&c); err != nil { + return nil, fmt.Errorf("decoding .golangci.yml: %w", err) + } + if c.Version != "2" { + return nil, fmt.Errorf(".golangci.yml version %q: want \"2\"", c.Version) + } + if c.Linters.Default != "none" { + return nil, fmt.Errorf(".golangci.yml linters.default %q: only \"none\" (an explicit enable list) is supported", c.Linters.Default) + } + switch c.Linters.Exclusions.Generated { + case "", GeneratedLax, GeneratedStrict, GeneratedDisable: + default: + return nil, fmt.Errorf(".golangci.yml linters.exclusions.generated %q: want lax, strict or disable", c.Linters.Exclusions.Generated) + } + for i, r := range c.Linters.Exclusions.Rules { + cr, err := r.compile() + if err != nil { + return nil, fmt.Errorf(".golangci.yml linters.exclusions.rules[%d]: %w", i, err) + } + c.rules = append(c.rules, cr) + } + return &c, nil +} + +// TestsAnalyzed reports run.tests (golangci-lint's default: true). +func (c *Config) TestsAnalyzed() bool { + return c.Run.Tests == nil || *c.Run.Tests +} + +// Enabled reports whether linters.enable lists the linter. +func (c *Config) Enabled(linter string) bool { + return slices.Contains(c.Linters.Enable, linter) +} + +// GeneratedMode returns linters.exclusions.generated, defaulted to lax. +func (c *Config) GeneratedMode() string { + if c.Linters.Exclusions.Generated == "" { + return GeneratedLax + } + return c.Linters.Exclusions.Generated +} + +var ( + loadOnce sync.Once + loaded *Config + loadErr error +) + +// Load returns the repository's .golangci.yml, parsed once. +func Load() (*Config, error) { + loadOnce.Do(func() { + loaded, loadErr = Parse(embedded) + }) + return loaded, loadErr +} + +type compiledRule struct { + path, pathExcept, text *regexp.Regexp + linters []string +} + +func (r ExclusionRule) compile() (compiledRule, error) { + var out compiledRule + var err error + if r.Path == "" && r.PathExcept == "" && r.Text == "" && len(r.Linters) == 0 { + return out, errors.New("empty rule") + } + compile := func(field, expr string) *regexp.Regexp { + if expr == "" || err != nil { + return nil + } + re, cerr := regexp.Compile(expr) + if cerr != nil { + err = fmt.Errorf("%s %q: %w", field, expr, cerr) + } + return re + } + out.path = compile("path", r.Path) + out.pathExcept = compile("path-except", r.PathExcept) + out.text = compile("text", r.Text) + out.linters = r.Linters + return out, err +} + +// match is golangci-lint's baseRule.match: every condition the rule sets +// must hold. path is relative to the repository root, with / separators. +func (r compiledRule) match(linter, path, text string) bool { + if r.text != nil && !r.text.MatchString(text) { + return false + } + if r.path != nil && !r.path.MatchString(path) { + return false + } + if r.pathExcept != nil && r.pathExcept.MatchString(path) { + return false + } + if len(r.linters) != 0 && !slices.Contains(r.linters, linter) { + return false + } + return true +} + +// Excluded reports whether a finding of linter at path (repository-relative, +// / separators) with the given text matches any exclusion rule. +func (c *Config) Excluded(linter, path, text string) bool { + for _, r := range c.rules { + if r.match(linter, path, text) { + return true + } + } + return false +} diff --git a/tools/nogo/internal/golangci/wrap.go b/tools/nogo/internal/golangci/wrap.go new file mode 100644 index 000000000..e799b05ac --- /dev/null +++ b/tools/nogo/internal/golangci/wrap.go @@ -0,0 +1,143 @@ +package golangci + +import ( + "go/ast" + "os" + "path/filepath" + "strings" + + "golang.org/x/tools/go/analysis" +) + +// Wrap returns an analyzer named linter that runs inner the way golangci-lint +// ran that linter under .golangci.yml: +// +// - nothing, unless linters.enable lists it; +// - with run.tests false, only on compile units without _test.go files. +// golangci-lint then loads each package without its tests; Bazel compiles +// that library unit on its own (go_library) beside the test unit, so the +// library unit sees exactly what golangci-lint saw, unused-parameter and +// unused-result verdicts included; +// - no findings in generated files (linters.exclusions.generated); +// - no findings an exclusion rule matches (linters.exclusions.rules). +// +// nogo honors //nolint itself (MODULE.bazel's rules_go patch). +func Wrap(linter string, inner *analysis.Analyzer) *analysis.Analyzer { + return &analysis.Analyzer{ + Name: linter, + Doc: inner.Doc, + URL: inner.URL, + Flags: inner.Flags, + Requires: inner.Requires, + ResultType: inner.ResultType, + FactTypes: inner.FactTypes, + RunDespiteErrors: inner.RunDespiteErrors, + Run: func(pass *analysis.Pass) (any, error) { + cfg, err := Load() + if err != nil { + return nil, err + } + return Run(cfg, linter, inner, pass) + }, + } +} + +// Run runs inner on pass under cfg's rules for linter (see Wrap). +func Run(cfg *Config, linter string, inner *analysis.Analyzer, pass *analysis.Pass) (any, error) { + if !cfg.Enabled(linter) { + return nil, nil + } + if !cfg.TestsAnalyzed() && HasTestFiles(pass) { + return nil, nil + } + generated := generatedFiles(cfg.GeneratedMode(), pass) + cwd, err := os.Getwd() + if err != nil { + return nil, err + } + filtered := *pass + filtered.Report = func(d analysis.Diagnostic) { + tf := pass.Fset.File(d.Pos) + if tf != nil && generated[tf.Name()] { + return + } + path := RelPath(cwd, pass.Fset.Position(d.Pos).Filename) + if cfg.Excluded(linter, path, d.Message) { + return + } + pass.Report(d) + } + return inner.Run(&filtered) +} + +// HasTestFiles reports whether the pass's compile unit includes _test.go +// files: Bazel's test unit of a package, not its library unit. +func HasTestFiles(pass *analysis.Pass) bool { + for _, f := range pass.Files { + if strings.HasSuffix(pass.Fset.File(f.Pos()).Name(), "_test.go") { + return true + } + } + return false +} + +// RelPath returns name relative to dir (nogo runs in the execution root, the +// repository root for source files) with / separators, the form +// golangci-lint's exclusion paths match. +func RelPath(dir, name string) string { + if filepath.IsAbs(name) && dir != "" { + if rel, err := filepath.Rel(dir, name); err == nil { + name = rel + } + } + return filepath.ToSlash(name) +} + +// The markers golangci-lint's lax mode looks for, lower case. +var laxGeneratedMarkers = []string{ + "code generated", + "do not edit", + "autogenerated file", + "* generated by: swagger codegen ", +} + +// generatedFiles returns the names of the pass's generated files, as +// golangci-lint's generated-file matcher decides them. +func generatedFiles(mode string, pass *analysis.Pass) map[string]bool { + out := map[string]bool{} + if mode == GeneratedDisable { + return out + } + for _, f := range pass.Files { + var gen bool + if mode == GeneratedStrict { + gen = ast.IsGenerated(f) + } else { + gen = IsGeneratedLax(f) + } + if gen { + out[pass.Fset.File(f.Pos()).Name()] = true + } + } + return out +} + +// IsGeneratedLax is golangci-lint's lax test: a comment before the first +// token after the package clause (what parser.PackageClauseOnly reads) +// contains one of the markers, in any case. +func IsGeneratedLax(f *ast.File) bool { + var doc []string + for _, group := range f.Comments { + if len(f.Decls) > 0 && group.Pos() >= f.Decls[0].Pos() { + break + } + doc = append(doc, strings.TrimSpace(group.Text())) + } + text := strings.ToLower(strings.Join(doc, "\n")) + for _, marker := range laxGeneratedMarkers { + if strings.Contains(text, marker) { + return true + } + } + return false +} diff --git a/tools/nogo/internal/golangci/wrap_test.go b/tools/nogo/internal/golangci/wrap_test.go new file mode 100644 index 000000000..926064f81 --- /dev/null +++ b/tools/nogo/internal/golangci/wrap_test.go @@ -0,0 +1,150 @@ +package golangci_test + +import ( + "go/ast" + "testing" + + "golang.org/x/tools/go/analysis" + + "github.com/steveyegge/beads/tools/nogo/internal/analyzertest" + "github.com/steveyegge/beads/tools/nogo/internal/golangci" +) + +// funcs reports every function declaration as "func ". +var funcs = &analysis.Analyzer{ + Name: "funcs", + Doc: "reports every function", + Run: func(pass *analysis.Pass) (any, error) { + for _, f := range pass.Files { + for _, d := range f.Decls { + if fn, ok := d.(*ast.FuncDecl); ok { + pass.Report(analysis.Diagnostic{Pos: fn.Pos(), Message: "func " + fn.Name.Name}) + } + } + } + return nil, nil + }, +} + +func wrapped(t *testing.T, yml string) *analysis.Analyzer { + t.Helper() + cfg, err := golangci.Parse([]byte(yml)) + if err != nil { + t.Fatal(err) + } + return &analysis.Analyzer{ + Name: "fake", + Doc: "funcs under cfg", + Run: func(pass *analysis.Pass) (any, error) { + return golangci.Run(cfg, "fake", funcs, pass) + }, + } +} + +const header = `version: "2" +linters: + default: none + enable: [fake] +` + +func TestNotEnabledReportsNothing(t *testing.T) { + a := wrapped(t, "version: \"2\"\nlinters:\n default: none\n enable: [other]\n") + got := analyzertest.RunInRepo(t, a, "p", map[string]string{"p.go": "package p\n\nfunc f() {}\n"}) + analyzertest.Equal(t, got, nil) +} + +func TestTestsFalseSkipsTestUnits(t *testing.T) { + a := wrapped(t, header+"run:\n tests: false\n") + lib := map[string]string{"p.go": "package p\n\nfunc f() {}\n"} + analyzertest.Equal(t, analyzertest.RunInRepo(t, a, "p", lib), + []analyzertest.Diagnostic{{File: "p.go", Line: 3, Message: "func f"}}) + withTest := map[string]string{ + "p.go": "package p\n\nfunc f() {}\n", + "p_test.go": "package p\n\nfunc g() {}\n", + } + analyzertest.Equal(t, analyzertest.RunInRepo(t, a, "p", withTest), nil) +} + +func TestTestsDefaultAnalyzesTestUnits(t *testing.T) { + a := wrapped(t, header) + got := analyzertest.RunInRepo(t, a, "p", map[string]string{ + "p.go": "package p\n\nfunc f() {}\n", + "p_test.go": "package p\n\nfunc g() {}\n", + }) + analyzertest.Equal(t, got, []analyzertest.Diagnostic{ + {File: "p.go", Line: 3, Message: "func f"}, + {File: "p_test.go", Line: 3, Message: "func g"}, + }) +} + +func TestGeneratedFilesAreSkipped(t *testing.T) { + files := map[string]string{ + // lax: any marker before the first declaration, in any case. + "gen.go": "// Package p is AutoGenerated File output.\npackage p\n\nfunc g() {}\n", + "late.go": "package p\n\nfunc h() {}\n\n// Code generated by nothing; a marker after the first declaration.\n", + "p.go": "package p\n\n// Do Not Edit this one, says a lax reader.\nfunc f() {}\n", + } + analyzertest.Equal(t, analyzertest.RunInRepo(t, wrapped(t, header), "p", files), + []analyzertest.Diagnostic{{File: "late.go", Line: 3, Message: "func h"}}) + strict := header + " exclusions:\n generated: strict\n" + analyzertest.Equal(t, analyzertest.RunInRepo(t, wrapped(t, strict), "p", files), + []analyzertest.Diagnostic{ + {File: "gen.go", Line: 4, Message: "func g"}, + {File: "late.go", Line: 3, Message: "func h"}, + {File: "p.go", Line: 4, Message: "func f"}, + }) +} + +func TestExclusionRules(t *testing.T) { + yml := header + ` exclusions: + rules: + - path: '^a/' + text: 'func drop' + linters: [fake] + - path-except: '^a/' + linters: [fake] + - text: 'func other' + linters: [other] +` + a := wrapped(t, yml) + src := map[string]string{"p.go": "package p\n\nfunc keep() {}\n\nfunc drop() {}\n\nfunc other() {}\n"} + analyzertest.Equal(t, analyzertest.RunInRepo(t, a, "a", src), []analyzertest.Diagnostic{ + {File: "p.go", Line: 3, Message: "func keep"}, + {File: "p.go", Line: 7, Message: "func other"}, + }) + // Outside a/, the path-except rule drops everything. + analyzertest.Equal(t, analyzertest.RunInRepo(t, a, "b", src), nil) +} + +func TestParseIsStrict(t *testing.T) { + for name, yml := range map[string]string{ + "unknown key": header + " settings:\n gosec:\n config: {}\n", + "unknown linter key": header + " settings:\n revive: {}\n", + "version": "version: \"1\"\nlinters:\n default: none\n", + "default": "version: \"2\"\nlinters:\n default: standard\n", + "bad regexp": header + " exclusions:\n rules:\n - text: '('\n", + "empty rule": header + " exclusions:\n rules:\n - {}\n", + } { + if _, err := golangci.Parse([]byte(yml)); err == nil { + t.Errorf("%s: Parse accepted %q", name, yml) + } + } +} + +func TestRepositoryConfigLoads(t *testing.T) { + if _, err := golangci.Load(); err != nil { + t.Fatal(err) + } +} + +func TestRelPath(t *testing.T) { + for _, c := range []struct{ dir, name, want string }{ + {"/x/root", "/x/root/cmd/bd/a.go", "cmd/bd/a.go"}, + {"/x/root", "cmd/bd/a.go", "cmd/bd/a.go"}, + {"", "/abs/a.go", "/abs/a.go"}, + } { + if got := golangci.RelPath(c.dir, c.name); got != c.want { + t.Errorf("RelPath(%q, %q) = %q, want %q", c.dir, c.name, got, c.want) + } + } +} diff --git a/tools/nogo/internal/position/BUILD.bazel b/tools/nogo/internal/position/BUILD.bazel new file mode 100644 index 000000000..200493e23 --- /dev/null +++ b/tools/nogo/internal/position/BUILD.bazel @@ -0,0 +1,31 @@ +load("@rules_go//go:def.bzl", "go_library") + +go_library( + name = "position", + srcs = ["position.go"], + importpath = "github.com/steveyegge/beads/tools/nogo/internal/position", + visibility = ["//tools/nogo:__subpackages__"], + deps = ["@org_golang_x_tools//go/analysis"], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tools/nogo/internal/position/position.go b/tools/nogo/internal/position/position.go new file mode 100644 index 000000000..0884439fc --- /dev/null +++ b/tools/nogo/internal/position/position.go @@ -0,0 +1,39 @@ +// Package position converts the token.Position values that some third-party +// checkers return back into token.Pos values an analysis.Pass can report. +package position + +import ( + "go/token" + + "golang.org/x/tools/go/analysis" +) + +// Index maps the file names of a pass's syntax files to their token.File. +type Index map[string]*token.File + +// NewIndex indexes the files of pass by name. +func NewIndex(pass *analysis.Pass) Index { + idx := make(Index, len(pass.Files)) + for _, f := range pass.Files { + tf := pass.Fset.File(f.Pos()) + idx[tf.Name()] = tf + } + return idx +} + +// Pos returns the token.Pos for p, and false when p is not in one of the +// pass's files or lies outside its file. +func (idx Index) Pos(p token.Position) (token.Pos, bool) { + tf, ok := idx[p.Filename] + if !ok || p.Line < 1 || p.Line > tf.LineCount() { + return token.NoPos, false + } + pos := tf.LineStart(p.Line) + if p.Column > 1 { + pos += token.Pos(p.Column - 1) + } + if int(pos)-tf.Base() > tf.Size() { + return token.NoPos, false + } + return pos, true +} diff --git a/tools/nogo/modules_test.go b/tools/nogo/modules_test.go new file mode 100644 index 000000000..8ac5566b3 --- /dev/null +++ b/tools/nogo/modules_test.go @@ -0,0 +1,49 @@ +package nogo_test + +import ( + "os" + "testing" + + "golang.org/x/mod/modfile" + "golang.org/x/mod/semver" +) + +func parseGoMod(t *testing.T, path string) *modfile.File { + t.Helper() + data, err := os.ReadFile(path) + if err != nil { + t.Fatalf("reading %s: %v", path, err) + } + f, err := modfile.Parse(path, data, nil) + if err != nil { + t.Fatalf("parsing %s: %v", path, err) + } + return f +} + +// TestModuleRequirementsAgreeWithRoot keeps the two modules gazelle.go.work +// joins at identical versions. go_deps resolves them as one graph (highest +// version wins), so a module this one requires at a higher version than the +// root go.mod would silently change what the bd build links under Bazel but +// not under `go build`; a lower one would build the analyzers here against +// versions Bazel never uses. +func TestModuleRequirementsAgreeWithRoot(t *testing.T) { + root := parseGoMod(t, "../../go.mod") + nogo := parseGoMod(t, "go.mod") + rootVersions := map[string]string{} + for _, r := range root.Require { + rootVersions[r.Mod.Path] = r.Mod.Version + } + for _, r := range nogo.Require { + want, ok := rootVersions[r.Mod.Path] + if !ok { + continue + } + if semver.Compare(r.Mod.Version, want) != 0 { + t.Errorf("tools/nogo/go.mod requires %s %s; the root go.mod requires %s (align them)", r.Mod.Path, r.Mod.Version, want) + } + } + if root.Go.Version != nogo.Go.Version { + t.Errorf("tools/nogo/go.mod go %s; root go.mod go %s", nogo.Go.Version, root.Go.Version) + } +}