diff --git a/.bazelignore b/.bazelignore index 02f8e8fdb..446a903e1 100644 --- a/.bazelignore +++ b/.bazelignore @@ -1,5 +1,3 @@ -examples/library-usage -examples/bd-example-extension-go npm-package/node_modules website node_modules diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index 07355f39c..772ce64f3 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -1339,12 +1339,13 @@ jobs: echo "bazel test --config=integration: exit $rc, $(( $(date +%s) - start ))s wall" | tee -a "$GITHUB_STEP_SUMMARY" exit "$rc" - # As in bazel-embedded. dolt_version_test is a plain sh_test (no Go - # testcases in its test.xml), which --config=integration's tag filter - # keeps, unlike the embedded one. + # As in bazel-embedded. dolt_version_test and the repository guards in + # //scripts/repochecks are plain sh_tests (no Go testcases in their + # test.xml), which --config=integration's tag filter keeps, unlike the + # embedded one. - name: Every target and shard ran tests if: ${{ always() && steps.test.outcome != 'skipped' }} - run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" --not-go //tools/bazel:dolt_version_test + run: python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" --not-go //tools/bazel:dolt_version_test --not-go //scripts/repochecks:all - name: Upload test logs if: ${{ failure() && steps.test.outcome != 'skipped' }} diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index bfaf9f66e..89470edf9 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -148,31 +148,29 @@ jobs: retention-days: 1 if-no-files-found: error - # F7a: fold of five independent, no-cache pr.yml checks that previously ran - # as five separate ubuntu-latest jobs (check-build-tags, - # check-version-consistency, check-migration-hygiene, - # check-no-beads-changes, fmt-check) into one Blacksmith job with five - # steps. Each step gets its own id and `if: ${{ !cancelled() }}` so one - # check's failure does not block the others from running, and the job - # exposes each step's outcome as a job output so ci-gate can still red the - # exact same CI_GATE_REQUIRED token it did before the fold - - # TestPRCIGateFastChecksTokens pins the mapping. FAST_CHECKS (the job's own - # `result`) is an added backstop token: if the whole job is lost (e.g. a - # cancelled runner) before any step sets its output, the per-check outputs - # read empty and the CI_GATE_REQUIRED fallback below treats that as - # "skipped" - which is NOT skipped_ok, so the gate still reds, but - # FAST_CHECKS gives ci-gate.sh one clear error line ("FAST_CHECKS=cancelled") - # instead of five confusing ones. + # The checks that need the PR's base commit, so no hermetic Bazel target + # can hold them: check-migration-hygiene.sh's delta half (C: shipped + # migrations are frozen, D: ignored-plane twins, E: no prepared DML in new + # migrations, all over `git diff `) and the .beads/issues.jsonl + # guard. Everything else this job ran is a Bazel test in bazel.yml's `test` + # lane (ga-96smfk.20): //scripts/repochecks:build_tags_test, + # :go_install_guidance_test, :winget_portable_alias_test, + # :migration_hygiene_test (checks A and B) and :fmt_test, and + # //internal/versioncheck:versioncheck_test for check-versions.sh. No Go: + # both steps are bash and git. + # + # Each step has its own id and `if: ${{ !cancelled() }}`, so one failure + # does not hide the other, and its outcome is a job output that ci-gate + # requires (TestPRCIGateFastChecksTokens). FAST_CHECKS (the job's own + # `result`) is the backstop token for a job lost before any step set its + # output. fast-checks: - name: Fast checks (build tags, versions, migrations, beads diff, fmt) + name: Fast checks (frozen migrations, .beads diff) 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-2vcpu-ubuntu-2404' || 'ubuntu-latest' }} timeout-minutes: 10 outputs: - build-tags: ${{ steps.build-tags.outcome }} - version-consistency: ${{ steps.version-consistency.outcome }} migration-hygiene: ${{ steps.migration-hygiene.outcome }} no-beads-changes: ${{ steps.no-beads-changes.outcome }} - fmt: ${{ steps.fmt.outcome }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 with: @@ -180,40 +178,18 @@ jobs: # the base branch; a shallow checkout starves both. fetch-depth: 0 - - name: Set up Go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - # Fast shell/Make guard for direct source-time build-tag declarations. - # Workflow `run` steps are checked structurally in the required policy - # wrapper. - - name: Check build-tag policy - id: build-tags - if: ${{ !cancelled() }} - run: | - ./scripts/check-build-tags.sh - ./scripts/check-go-install-guidance.sh - ./scripts/check-winget-portable-alias.sh - - # Fast check to ensure all version files are in sync - - name: Check all versions match - id: version-consistency - if: ${{ !cancelled() }} - run: ./scripts/check-versions.sh - - # Migration hygiene: duplicate version numbers, nondeterministic SQL - # (UUID()/NOW()/RAND(), the #4259 root-cause class), and edits to - # migration files that already exist on the base branch (shipped - # migrations are frozen; fix forward with a new version). Fails at PR - # time, before any test even compiles. Details: + # Migration hygiene, delta half: edits to migration files that already + # exist on the base branch (shipped migrations are frozen; fix forward + # with a new version), and the twin and prepared-DML rules for new + # migrations. Checks A and B (duplicate versions, nondeterministic SQL) + # read only the tree and run in Bazel. Details: # scripts/check-migration-hygiene.sh. - name: Run migration hygiene checks id: migration-hygiene if: ${{ !cancelled() }} env: BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} + MIGRATION_HYGIENE_SCOPE: delta run: ./scripts/check-migration-hygiene.sh # Fast check to catch accidental .beads/issues.jsonl changes from @@ -247,11 +223,6 @@ jobs: fi echo "No .beads/issues.jsonl changes detected" - - name: Check gofmt - id: fmt - if: ${{ !cancelled() }} - run: make fmt-check - # Lock in two CGO-disabled portability boundaries. cmd/bd carries a mix of # cgo-only test helpers (embedded Dolt, sql.DB) and pure-Go tests; compiling # and running its pure-Go subset prevents test contamination (mybd-ycx / @@ -849,6 +820,14 @@ jobs: # Check documentation references match actual CLI flags # + # Stays a job: the generated CLI docs describe the release pinned in + # docs/cli-docs.pin, and check-doc-flags.sh validates them against bd built + # from that tag (scripts/resolve-docs-bd.sh: git fetch the tag, pure-Go go + # build), attributing drift against the merge-base. Neither input can enter + # a Bazel action key: releases publish no pure-Go linux binary to pin, and + # the attribution needs the PR's base. check-doc-freshness.sh, which this + # job also ran, is //scripts/repochecks:doc_freshness_test. + # # F7a: moved to Blacksmith for same-repo PRs/merge_group. This job's module # cache restore step was already absent before the fold (every run cold- # builds bd), so there is no GitHub-saved cache to lose here. @@ -881,9 +860,7 @@ jobs: # would eject every queued PR. Diff against the queue's base # commit instead (empty on pull_request: origin/$GITHUB_BASE_REF). BD_DOCS_DIFF_BASE: ${{ github.event.merge_group.base_sha }} - run: | - ./scripts/check-doc-flags.sh ./bd - ./scripts/check-doc-freshness.sh + run: ./scripts/check-doc-flags.sh ./bd # When the drift check fails, it writes the exact regenerated-docs fix # (produced with CI's canonical build) so contributors can apply it with @@ -898,6 +875,13 @@ jobs: path: ${{ runner.temp }}/cli-docs-freshness.patch if-no-files-found: ignore + # The Linux leg is Bazel's (ga-96smfk.20): //scripts:doc_freshness_required_test + # (integration lane: the doc-freshness suite under -required-suite) and + # //scripts/gitattributespolicy:gitattributespolicy_required_host_test + # (-required-host -expected-goos linux), both on rbe-west's Linux workers. + # The macOS and Windows legs stay here, on Blacksmith for same-repo PRs and + # merge groups (GitHub-hosted for forks and Dependabot). + # # F7b: same-repo Blacksmith for the Linux/Windows legs via the "matrix # marker" form (spec F7/F7b ยง2.1): Actions does not expand `${{ }}` inside a # matrix array/include value, so each leg's `include` entry instead carries @@ -929,9 +913,6 @@ jobs: fail-fast: false matrix: include: - - os: ubuntu-latest - expected_goos: linux - runner: same-repo-linux - os: macos-latest expected_goos: darwin runner: same-repo-macos @@ -1146,69 +1127,6 @@ jobs: GOCACHE: ${{ runner.temp }}/go-cache/non-race run: bash scripts/ci/test-windows-env-helpers.sh - # F3: package-mcp and package-npm (and the detect job that fed them) moved - # into bazel.yml, which pr.yml calls with package-gates: "on" below. They - # now depend on nothing but the rbe job's decision, so they no longer wait - # on build-artifacts, bazel-coverage or the rest of the bazel call. - # - # F7a: moved to Blacksmith for same-repo PRs/merge_group. setup-go runs with - # cache: false and the module download step below always hits the network, - # so this job restores no GitHub-saved cache. - pr-policy-wrapper: - name: PR Policy (wrapper timing) - 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: 15 - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6 - with: - fetch-depth: 0 - - - name: Set up Go - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 - with: - go-version-file: 'go.mod' - cache: false - - # The module fetch is the only part of this job that talks to the network, - # and setup-go above runs with cache: false, so every run pulls the whole - # module graph from the proxy. Doing it here, retried, means a proxy blip - # costs seconds instead of failing a required check. - # - # Deliberately NOT wrapped around the build: `make ci-pr-policy` runs a - # `go build ./cmd/bd` (scripts/ci/pr-policy.sh, build_docs_binary), and a - # compile error is not transient. Retrying it would make a genuinely broken - # build take three attempts to report and look like a slow one. The fetch - # has a legitimate transient failure mode; the build does not. - # - # 3 attempts x 120s + 2 backoffs x 10s = 380s worst case. 8m leaves ~100s - # of headroom, matching the bounded apt-get steps in migration-test.yml. - - name: Download modules - timeout-minutes: 8 - run: | - downloaded="" - for attempt in 1 2 3; do - if timeout 120 go mod download; then - downloaded=yes - break - fi - echo "::warning::go mod download attempt $attempt failed or timed out" - # No backoff after the final attempt - there is nothing left to wait for. - if [ "$attempt" -lt 3 ]; then - sleep 10 - fi - done - # Unlike the best-effort apt-get retries elsewhere in this repo, this - # one is load-bearing: nothing downstream can build without the - # modules. Fail here, naming the fetch, so a module outage is not - # reported as a build failure. - if [ -z "$downloaded" ]; then - echo "::error::go mod download failed after 3 attempts - module fetch failure, not a build failure" - exit 1 - fi - - - name: Run PR policy wrapper - run: make ci-pr-policy - # 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 @@ -1279,7 +1197,7 @@ jobs: # (//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-policy-wrapper/pr-core-wrapper above. + # 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 }}) @@ -2380,7 +2298,6 @@ jobs: - check-doc-flags - check-doc-freshness-platforms - pr-preflight-platforms - - pr-policy-wrapper - pr-core-wrapper - scripts-go-checks - pr-lint-wrapper @@ -2399,11 +2316,8 @@ jobs: CI_GATE_REQUIRED: >- BUILD_ARTIFACTS FAST_CHECKS - CHECK_BUILD_TAGS - CHECK_VERSION_CONSISTENCY CHECK_MIGRATION_HYGIENE CHECK_NO_BEADS_CHANGES - FMT_CHECK CHECK_CMD_BD_PUREGEO_TESTS CHECK_RELEASE_TARGET_CROSS_COMPILATION CHECK_DOC_FLAGS @@ -2411,7 +2325,6 @@ jobs: PR_PREFLIGHT_PLATFORMS PACKAGE_MCP PACKAGE_NPM - PR_POLICY_WRAPPER PR_CORE_WRAPPER SCRIPTS_GO_CHECKS PR_LINT_WRAPPER @@ -2431,21 +2344,19 @@ jobs: BAZEL_DOLT_SERVER_RETIRED BAZEL_PR_LANES_RETIRED BUILD_ARTIFACTS: ${{ needs.build-artifacts.result }} - # F7a: fast-checks folds five formerly-separate jobs into one job - # with five steps; each token below now reads that step's outcome - # (via the job's outputs) instead of a sibling job's result, with a - # 'skipped' fallback for a lost job (see FAST_CHECKS below). - # TestPRCIGateFastChecksTokens pins this mapping. - CHECK_BUILD_TAGS: ${{ needs.fast-checks.outputs.build-tags || 'skipped' }} - CHECK_VERSION_CONSISTENCY: ${{ needs.fast-checks.outputs.version-consistency || 'skipped' }} + # fast-checks' two base-diff steps; each token reads that step's + # outcome (via the job's outputs), with a 'skipped' fallback for a + # lost job (see FAST_CHECKS below). TestPRCIGateFastChecksTokens + # pins this mapping. The repository guards the job used to run + # (build tags, versions, migration checks A and B, gofmt) are Bazel + # tests and gate through BAZEL_TEST. CHECK_MIGRATION_HYGIENE: ${{ needs.fast-checks.outputs.migration-hygiene || 'skipped' }} CHECK_NO_BEADS_CHANGES: ${{ needs.fast-checks.outputs.no-beads-changes || 'skipped' }} - FMT_CHECK: ${{ needs.fast-checks.outputs.fmt || 'skipped' }} # Backstop: if fast-checks itself is lost (e.g. a cancelled runner) - # before any step sets an output, the five tokens above all read - # 'skipped' (none are in CI_GATE_SKIPPED_OK by default, so the gate - # still reds) - FAST_CHECKS gives ci-gate.sh one clear line naming - # the job instead of five step-shaped ones. + # before any step sets an output, the two tokens above read + # 'skipped' (neither is in CI_GATE_SKIPPED_OK, so the gate still + # reds) - FAST_CHECKS gives ci-gate.sh one clear line naming the + # job. FAST_CHECKS: ${{ needs.fast-checks.result }} CHECK_CMD_BD_PUREGEO_TESTS: ${{ needs.check-cmd-bd-puregeo-tests.result }} CHECK_RELEASE_TARGET_CROSS_COMPILATION: ${{ needs.check-release-target-cross-compilation.result }} @@ -2466,7 +2377,6 @@ jobs: # here). PACKAGE_MCP: ${{ needs.bazel.outputs.package-mcp || 'skipped' }} PACKAGE_NPM: ${{ needs.bazel.outputs.package-npm || 'skipped' }} - PR_POLICY_WRAPPER: ${{ needs.pr-policy-wrapper.result }} PR_CORE_WRAPPER: ${{ needs.pr-core-wrapper.result }} SCRIPTS_GO_CHECKS: ${{ needs.scripts-go-checks.result }} PR_LINT_WRAPPER: ${{ needs.pr-lint-wrapper.result }} diff --git a/BUILD.bazel b/BUILD.bazel index dd3c4b4a1..752091059 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -11,6 +11,12 @@ load("@rules_go//go:def.bzl", "go_library", "go_test") # so //:repo_files covers it for //test/docsync's link checks and the # repository-policy tests in //scripts. # gazelle:exclude integrations +# The two example Go modules (own go.mod, built against a released beads) are +# not part of this build either, but they are tracked files the repository +# guards (gofmt, build-tag and install-guidance checks in +# //scripts/repochecks) must see, so they are not in .bazelignore. +# gazelle:exclude examples/library-usage +# gazelle:exclude examples/bd-example-extension-go gazelle(name = "gazelle") exports_files( @@ -361,6 +367,7 @@ filegroup( "//scripts/migration-test/catalog:repo_files", "//scripts/pr-lint:repo_files", "//scripts/prlintmake:repo_files", + "//scripts/repochecks:repo_files", "//scripts/repro-dolt-hang:repo_files", "//scripts/repro-dolt-prod-timeouts:repo_files", "//scripts/workflowtags:repo_files", @@ -374,6 +381,7 @@ filegroup( ], visibility = [ "//scripts:__pkg__", + "//scripts/repochecks:__pkg__", "//test/docsync:__pkg__", ], ) diff --git a/MODULE.bazel b/MODULE.bazel index 186368330..079f91640 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -220,16 +220,20 @@ dolt_release( platform = "darwin-arm64", ) -# The tracked release formula, for //scripts:scripts_test. .beads/ also holds -# live bd data and is in .bazelignore, so no main-repo target can declare the -# formula; this local repository exposes only .beads/formulas. +# The tracked release formula, for //scripts:scripts_test and the +# whole-checkout guard //scripts/repochecks:go_install_guidance_test. .beads/ +# also holds live bd data and is in .bazelignore, so no main-repo target can +# declare the formula; this local repository exposes only .beads/formulas. new_local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "new_local_repository") new_local_repository( name = "beads_formulas", build_file_content = """exports_files( glob(["*.formula.toml"]), - visibility = ["@@//scripts:__pkg__"], + visibility = [ + "@@//scripts:__pkg__", + "@@//scripts/repochecks:__pkg__", + ], ) """, path = ".beads/formulas", diff --git a/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md b/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md index 6ec63d432..08615789b 100644 --- a/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md +++ b/engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md @@ -630,11 +630,9 @@ would only be needed if maintainers still want exactly one required check. Do not require these existing check names directly: - `Detect CI tier` -- `Fast checks (build tags, versions, migrations, beads diff, fmt)` (F7a fold - of the former standalone `Check build-tag policy`, `Check version - consistency`, `Check for .beads changes` and `Check formatting` checks into - one job's steps; see - [F7a: Same-Repo Blacksmith Moves and Job Folds](#f7a-same-repo-blacksmith-moves-and-job-folds)) +- `Fast checks (frozen migrations, .beads diff)` (the base-diff checks; the + tree checks it used to run are Bazel tests, see + [Repository Guards as Bazel Tests](#repository-guards-as-bazel-tests)) - `Check pure-Go and js/wasm boundaries (CGO_ENABLED=0)` - `Check doc flags freshness` - `Check release target cross-compilation (unix)` and `(desktop)` (F7a fold @@ -1255,6 +1253,49 @@ scope, not this slice's. Windows job to Blacksmith (there is no Windows Blacksmith pool), and neither job is one F4's concurrent Windows-region edits touch. +### Repository Guards as Bazel Tests + +ga-96smfk.20 moved the repository guards that pr.yml ran on GitHub runners +into Bazel tests over narrow slices of the checkout +(`tools/bazel/repo_subset.bzl`, laid out as a throwaway git checkout by +`scripts/repochecks/run_check.sh`), so they run remotely in bazel.yml's +`test` lane and a pass is reused until one of the files the guard reads +changes. They gate through `BAZEL_TEST`; pinned by +`TestPRPolicyChecksRunAsBazelTargets`. + +| Former job / step | Bazel target | +| --- | --- | +| fast-checks: `check-build-tags.sh` | `//scripts/repochecks:build_tags_test` | +| fast-checks: `check-go-install-guidance.sh` | `//scripts/repochecks:go_install_guidance_test` | +| fast-checks: `check-winget-portable-alias.sh` | `//scripts/repochecks:winget_portable_alias_test` | +| fast-checks: `check-versions.sh` | `//internal/versioncheck:versioncheck_test` (same `versioncheck.Check` over the release metadata) | +| fast-checks: migration hygiene checks A and B | `//scripts/repochecks:migration_hygiene_test` (`MIGRATION_HYGIENE_SCOPE=tree`) | +| fast-checks: `make fmt-check` | `//scripts/repochecks:fmt_test` (the registered SDK's gofmt) | +| PR Policy: `checkworkflowtags` | `//scripts/repochecks:workflow_tags_test` | +| PR Policy: `check-testing-short.sh` | `//scripts:scripts_test` (`TestCheckTestingShortPassesOnCleanRepoTree`) | +| PR Policy: workapi frontend boundary | `//scripts/repochecks:workapi_frontend_boundary_test` | +| PR Policy: `make api-check` drift | `//scripts/repochecks:types_gen_drift_test`; its `go test ./internal/httpapi/...` half is the httpapi targets in the same lane | +| PR Policy / check-doc-flags: `check-doc-freshness.sh` | `//scripts/repochecks:doc_freshness_test` | +| check-doc-freshness-platforms, Linux leg | `//scripts:doc_freshness_required_test` (integration lane) and `//scripts/gitattributespolicy:gitattributespolicy_required_host_test` | + +What stays a job, and why: + +- `fast-checks` keeps the two checks that need the PR's base commit: + check-migration-hygiene.sh's delta half (`MIGRATION_HYGIENE_SCOPE=delta`: + frozen shipped migrations, ignored-plane twins, prepared DML in new + migrations) and the `.beads/issues.jsonl` guard. No action key can hold a + base commit. +- `check-doc-flags` validates the generated CLI docs against bd built from + the release tag in `docs/cli-docs.pin` (git fetch plus a pure-Go + `go build`), and attributes drift against the merge-base. Releases publish + no pure-Go Linux binary a Bazel repository could pin by sha256. +- The macOS and Windows legs of `check-doc-freshness-platforms` (no remote + workers for those hosts), on Blacksmith for same-repo PRs and merge groups. + +`check-doc-freshness.sh` compares review dates with today's date, which is +not in the action key: a cached pass stands until one of its inputs changes, +and nightly's `--config=fresh` run re-checks it against the date. + ### Same-Repo Blacksmith macOS Legs pr.yml's two mixed-OS matrix jobs, `pr-preflight-platforms` and diff --git a/internal/httpapi/apigen/BUILD.bazel b/internal/httpapi/apigen/BUILD.bazel index f777dcdd4..9aa4a0b29 100644 --- a/internal/httpapi/apigen/BUILD.bazel +++ b/internal/httpapi/apigen/BUILD.bazel @@ -15,6 +15,13 @@ go_library( ], ) +# //scripts/repochecks:types_gen_drift_test regenerates types.gen.go from the +# spec (`make api-check`'s drift half). +exports_files( + ["types.gen.go"], + visibility = ["//scripts/repochecks:__pkg__"], +) + # --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- filegroup( diff --git a/internal/httpapi/spec/BUILD.bazel b/internal/httpapi/spec/BUILD.bazel index 5fe190f42..81d0f6532 100644 --- a/internal/httpapi/spec/BUILD.bazel +++ b/internal/httpapi/spec/BUILD.bazel @@ -13,10 +13,14 @@ go_library( # (the go:embed above), not a relative os.ReadFile path, so no `data` # dependency on this file is actually needed for that one read -- this export # stays as defense in depth for any other reader of the raw YAML that package -# gains. +# gains. //scripts/repochecks:types_gen_drift_test regenerates the wire +# types from it. exports_files( ["openapi.v0.yaml"], - visibility = ["//internal/httpclient/encode:__pkg__"], + visibility = [ + "//internal/httpclient/encode:__pkg__", + "//scripts/repochecks:__pkg__", + ], ) # --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- diff --git a/internal/workapi/doc.go b/internal/workapi/doc.go index 6df2ee589..bda4b6c15 100644 --- a/internal/workapi/doc.go +++ b/internal/workapi/doc.go @@ -14,5 +14,5 @@ // // The boundary is enforced mechanically, not by review: see the // workapi-frontend-boundary depguard rule in .golangci.yml and the -// banned-accessor check in scripts/ci/pr-policy.sh. +// banned-accessor check in scripts/check-workapi-frontend-boundary.sh. package workapi diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index 0eaa8a45f..4c619b4e1 100644 --- a/scripts/BUILD.bazel +++ b/scripts/BUILD.bazel @@ -47,6 +47,7 @@ go_test( "pull_dolt_image_test.go", "rbe_worker_env_test.go", "release_script_test.go", + "repo_helpers_test.go", "required_suite_test.go", "test_script_test.go", "upgrade_smoke_loop_test.go", @@ -73,6 +74,37 @@ go_test( ], ) +# pr.yml's check-doc-freshness-platforms Linux leg: `go test +# -tags integration ./scripts -count=1 -run '^(TestDocFreshness.*| +# TestRequiredSuiteContract)$' -args -required-suite=doc-freshness`, whose +# TestMain fails unless exactly the doc-freshness inventory is compiled and +# selected. scripts_test runs those tests too (in the integration lane); this +# target adds the inventory and selection contract, over only the files the +# suite reads (its fixtures copy check-doc-freshness.sh). The macOS and +# Windows legs stay in pr.yml. +# Tags: +# integration-only: every TestDocFreshness test is //go:build integration, +# so --config=integration's lane runs it. +go_test( + name = "doc_freshness_required_test", + srcs = [ + "check_doc_freshness_test.go", + "repo_helpers_test.go", + "required_suite_test.go", + ], + args = [ + "-test.count=1", + "-test.run=^(TestDocFreshness.*|TestRequiredSuiteContract)$$", + "-required-suite=doc-freshness", + ], + data = ["check-doc-freshness.sh"], + tags = ["integration-only"], + deps = [ + "//internal/testutil/bazeltest", + "//scripts/internal/testbash", + ], +) + exports_files( ["migration-test/release-catalog.json"], visibility = ["//scripts/migration-test/catalog:__pkg__"], diff --git a/scripts/check-migration-hygiene.sh b/scripts/check-migration-hygiene.sh index 7a1db5af4..1e1b4a70c 100755 --- a/scripts/check-migration-hygiene.sh +++ b/scripts/check-migration-hygiene.sh @@ -89,6 +89,12 @@ # Checks C, D, and E compare against $BASE_SHA if set (CI passes the PR base), # else origin/main, else main; they are skipped with a warning when no base # is resolvable (e.g. shallow clone without the base commit). +# +# MIGRATION_HYGIENE_SCOPE splits the two halves for CI (default: all): +# tree checks A and B, which read only the checked-out migrations: the +# hermetic half, //scripts/repochecks:migration_hygiene_test. +# delta checks C, D and E, which diff against the base: pr.yml's +# pr-diff-checks job, the only place a PR's base is known. set -euo pipefail @@ -100,6 +106,16 @@ MIG_DIR="internal/storage/schema/migrations" ALLOWLIST="$MIG_DIR/nondeterminism-allowlist.txt" fail=0 +scope="${MIGRATION_HYGIENE_SCOPE:-all}" +case "$scope" in + all|tree|delta) ;; + *) echo "MIGRATION_HYGIENE_SCOPE must be all, tree or delta, not '$scope'" >&2; exit 2 ;; +esac + +# Checks A and B (and the allowlist audit) run unless scope is delta. Their +# bodies are not indented under this `if`: Check B's heredoc is column-0. +if [ "$scope" != delta ]; then + # --- Check A: duplicate version numbers ------------------------------------ # Numbering is per directory: migrations/ and migrations/ignored/ are # independent sequences (see ignored/ for local-state tables). @@ -183,9 +199,14 @@ if [ -f "$ALLOWLIST" ]; then done < "$ALLOWLIST" fi +fi # scope != delta + # --- Check C: shipped migrations are frozen ---------------------------------- +# Scope tree leaves base empty, so C, D and E below skip without a warning. base="${BASE_SHA:-}" -if [ -z "$base" ]; then +if [ "$scope" = tree ]; then + base="" +elif [ -z "$base" ]; then for candidate in origin/main main; do if git rev-parse --verify -q "$candidate^{commit}" >/dev/null 2>&1; then base="$candidate" @@ -195,7 +216,7 @@ if [ -z "$base" ]; then fi if [ -z "$base" ] || ! merge_base=$(git merge-base "$base" HEAD 2>/dev/null); then - echo "WARN (frozen migrations) no usable base ref; skipping check C." >&2 + [ "$scope" = tree ] || echo "WARN (frozen migrations) no usable base ref; skipping check C." >&2 else # Diff merge-base against the working tree (no HEAD) so local uncommitted # edits are caught too; in CI the tree is clean so this equals the PR diff. @@ -228,7 +249,7 @@ fi # backstop for the same invariant is # internal/storage/embeddeddolt/migrate_ignored_plane_shape_test.go. if [ -z "$base" ] || [ -z "${merge_base:-}" ]; then - echo "WARN (ignored twins) no usable base ref; skipping check D." >&2 + [ "$scope" = tree ] || echo "WARN (ignored twins) no usable base ref; skipping check D." >&2 else ignored_tables='wisps|wisp_[a-z_]+|repo_mtimes|local_metadata|leases|events|bd_events_journal|bd_events_seq|ignored_schema_migrations' ddl_re="(alter|create|rename|drop)[[:space:]]+table([[:space:]]+if[[:space:]]+(not[[:space:]]+)?exists)?[[:space:]]+[\`']?(${ignored_tables})\b" @@ -373,7 +394,7 @@ EOF } if [ -z "$base" ] || [ -z "${merge_base:-}" ]; then - echo "WARN (prepared DML) no usable base ref; skipping check E." >&2 + [ "$scope" = tree ] || echo "WARN (prepared DML) no usable base ref; skipping check E." >&2 else # Only *.up.sql is embedded into the CLI fresh bundle (`//go:embed # migrations/*.up.sql`, schema.go:218), so a .down.sql can never meet the bug diff --git a/scripts/check-workapi-frontend-boundary.sh b/scripts/check-workapi-frontend-boundary.sh new file mode 100755 index 000000000..2031166bc --- /dev/null +++ b/scripts/check-workapi-frontend-boundary.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# internal/workapi must not read cwd- or environment-derived state. +# +# The import half of this boundary is the workapi-frontend-boundary depguard +# rule in .golangci.yml. This is the symbol half depguard cannot express: +# internal/workapi legitimately imports internal/config (for +# GetCustomTypesFromYAML, a workspace-scoped read), but process-local reads +# derived from a client's cwd or environment are meaningless in a long-lived +# server and must not leak into the shared contract. +# +# Runs in scripts/ci/pr-policy.sh and as +# //scripts/repochecks:workapi_frontend_boundary_test. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR/.." + +banned='config\.GetDirectoryLabels|os\.Getenv' + +if [[ ! -d internal/workapi ]]; then + printf 'internal/workapi does not exist; skipping frontend-boundary check.\n' + exit 0 +fi + +if grep -rnE "$banned" internal/workapi --include='*.go'; then + cat >&2 <<'MSG' + +internal/workapi must not read cwd- or environment-derived state. + +Those reads describe the client's process, which a server process does not +share. Resolve them in the frontend and pass the result in as a parameter. +See internal/workapi/doc.go. +MSG + exit 1 +fi + +printf 'No banned cwd/env accessors in internal/workapi.\n' diff --git a/scripts/ci/pr-policy.sh b/scripts/ci/pr-policy.sh index 4a0436bcd..d450085c7 100755 --- a/scripts/ci/pr-policy.sh +++ b/scripts/ci/pr-policy.sh @@ -62,35 +62,6 @@ EOF printf 'No .beads/issues.jsonl changes detected.\n' } -check_workapi_frontend_boundary() { - # The import half of this boundary is the workapi-frontend-boundary - # depguard rule in .golangci.yml. This is the symbol half depguard cannot - # express: internal/workapi legitimately imports internal/config (for - # GetCustomTypesFromYAML, a workspace-scoped read), but process-local - # reads derived from a client's cwd or environment are meaningless in a - # long-lived server and must not leak into the shared contract. - local banned='config\.GetDirectoryLabels|os\.Getenv' - - if [[ ! -d internal/workapi ]]; then - printf 'internal/workapi does not exist; skipping frontend-boundary check.\n' - return 0 - fi - - if grep -rnE "$banned" internal/workapi --include='*.go'; then - cat >&2 <<'EOF' - -internal/workapi must not read cwd- or environment-derived state. - -Those reads describe the client's process, which a server process does not -share. Resolve them in the frontend and pass the result in as a parameter. -See internal/workapi/doc.go. -EOF - return 1 - fi - - printf 'No banned cwd/env accessors in internal/workapi.\n' -} - build_docs_binary() { tmpdir="$(mktemp -d)" local build @@ -111,7 +82,7 @@ ci_time "build bd for docs checks" -- build_docs_binary ci_time "check doc flags" -- ./scripts/check-doc-flags.sh "$tmpdir/bd" ci_time "check doc freshness" -- ./scripts/check-doc-freshness.sh ci_time "check testing.Short boundaries" -- ./scripts/check-testing-short.sh -ci_time "check workapi frontend boundary" -- check_workapi_frontend_boundary +ci_time "check workapi frontend boundary" -- ./scripts/check-workapi-frontend-boundary.sh ci_time "check no .beads/issues.jsonl changes" -- check_no_beads_jsonl_changes # The OpenAPI drift gate belongs to a REQUIRED PR job. The Main workflow is # perpetually concurrency-cancelled, so a gate that only ran there would be diff --git a/scripts/ci_blacksmith_runner_test.go b/scripts/ci_blacksmith_runner_test.go index 651f41499..fb7ed17f8 100644 --- a/scripts/ci_blacksmith_runner_test.go +++ b/scripts/ci_blacksmith_runner_test.go @@ -34,7 +34,7 @@ import ( const sameRepoBlacksmith2vcpu = "${{ (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-2vcpu-ubuntu-2404' || 'ubuntu-latest' }}" // F7a: the same same-repo expression at 4 vCPU and 8 vCPU, for jobs sized -// larger than the 2 vCPU default (check-doc-flags, pr-policy-wrapper, +// larger than the 2 vCPU default (check-doc-flags, // pr-risk.yml's test-nix at 4 vCPU; check-release-target-cross-compilation at // 8 vCPU). F7c's advisory workflows also use the 4 vCPU size. const sameRepoBlacksmith4vcpu = "${{ (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' }}" diff --git a/scripts/ci_workflow_test.go b/scripts/ci_workflow_test.go index fcd7006e2..59bca0ca0 100644 --- a/scripts/ci_workflow_test.go +++ b/scripts/ci_workflow_test.go @@ -36,7 +36,23 @@ func TestCIWorkflowArtifactOwnership(t *testing.T) { } } - assertJobRunsExactly(t, workflow.job(t, "pr-policy-wrapper"), "make ci-pr-policy") + // pr.yml's PR Policy job is retired: its checks are Bazel tests + // (TestPRPolicyChecksRunAsBazelTargets). main.yml keeps its own + // copy until main.yml's legacy jobs go. + if workflowName == "pr.yml" { + if _, found := workflow.Jobs["pr-policy-wrapper"]; found { + t.Errorf("pr.yml has a pr-policy-wrapper job; its checks run as Bazel tests") + } + for name, job := range workflow.Jobs { + for _, step := range job.Steps { + if strings.Contains(step.Run, "make ci-pr-policy") { + t.Errorf("pr.yml job %s step %q runs make ci-pr-policy; its checks run as Bazel tests", name, step.Name) + } + } + } + } 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 @@ -132,25 +148,29 @@ func TestPRCIGateRequiresPolicyAndLintWrappers(t *testing.T) { gate := readCIWorkflow(t, "pr.yml").job(t, "ci-gate") gateEnv := gate.step(t, "Evaluate CI gate").Env - for _, job := range []string{"pr-policy-wrapper", "pr-lint-wrapper"} { - if !contains(gate.Needs, job) { - t.Errorf("ci-gate needs %q: %v", job, gate.Needs) - } + 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") } - for key, want := range map[string]string{ - "PR_POLICY_WRAPPER": "${{ needs.pr-policy-wrapper.result }}", - "PR_LINT_WRAPPER": "${{ needs.pr-lint-wrapper.result }}", - } { - if got := gateEnv[key]; got != want { - t.Errorf("ci-gate env %s = %q, want %q", key, got, want) - } + // The retired PR Policy job's checks gate through BAZEL_TEST (the Bazel + // `test` lane), so its token must not linger. + if contains(gate.Needs, "pr-policy-wrapper") { + t.Errorf("ci-gate needs the retired pr-policy-wrapper: %v", gate.Needs) } - - for _, required := range []string{"PR_POLICY_WRAPPER", "PR_LINT_WRAPPER"} { - if !strings.Contains(gateEnv["CI_GATE_REQUIRED"], required) { - t.Errorf("ci-gate CI_GATE_REQUIRED does not include %q", required) - } + if _, found := gateEnv["PR_POLICY_WRAPPER"]; found { + t.Errorf("ci-gate env still sets PR_POLICY_WRAPPER") + } + if contains(strings.Fields(gateEnv["CI_GATE_REQUIRED"]), "PR_POLICY_WRAPPER") { + t.Errorf("ci-gate CI_GATE_REQUIRED still lists PR_POLICY_WRAPPER") + } + if !contains(strings.Fields(gateEnv["CI_GATE_REQUIRED"]), "BAZEL_TEST") { + t.Errorf("ci-gate CI_GATE_REQUIRED does not include BAZEL_TEST, the lane that now runs the policy checks") } // F5.1/F5.4: the deleted `lint` job's LINT id must not linger in the gate. @@ -1221,17 +1241,17 @@ func TestRepositoryTextEOLPolicyWorkflow(t *testing.T) { if want := sameRepoPlatformsMatrixMarkerRunsOn; job.RunsOn != want { t.Errorf("check-doc-freshness-platforms runs-on = %q, want %q", job.RunsOn, want) } + // The Linux leg runs in Bazel (TestDocFreshnessLinuxLegRunsInBazel); + // macOS and Windows have no remote workers and stay here. wantMatrix := map[string]string{ - "ubuntu-latest": "linux", "macos-latest": "darwin", "windows-latest": "windows", } // F7b: each host's include tuple also carries a "runner" marker - // (same-repo-linux/-macos/-windows) that sameRepoPlatformsMatrixMarkerRunsOn + // (same-repo-macos/-windows) that sameRepoPlatformsMatrixMarkerRunsOn // resolves against; check it matches the host instead of treating it as an // unexpected field. wantRunnerMarker := map[string]string{ - "ubuntu-latest": "same-repo-linux", "macos-latest": "same-repo-macos", "windows-latest": "same-repo-windows", } @@ -2988,7 +3008,6 @@ func TestSameRepoBlacksmithRunners(t *testing.T) { "fast-checks": sameRepoBlacksmith2vcpu, "advisory-reports": sameRepoBlacksmith2vcpu, "check-doc-flags": sameRepoBlacksmith4vcpu, - "pr-policy-wrapper": sameRepoBlacksmith4vcpu, "check-release-target-cross-compilation": sameRepoBlacksmith8vcpu, "windows-test-binaries": sameRepoBlacksmith8vcpu, // F7b: scripts-go-checks/pr-lint-wrapper moved to same-repo @@ -3174,14 +3193,14 @@ func TestBlacksmithJobsReadNoSecrets(t *testing.T) { // Actions expression evaluator instead of a hand-written Go mirror // (review SF-3). That file is shared with F7c. -// TestPRCIGateFastChecksTokens pins pr.yml's F7a fold of five formerly -// standalone jobs (check-build-tags, check-version-consistency, -// check-migration-hygiene, check-no-beads-changes, fmt-check) into -// fast-checks' five steps: each step's id, its outputs entry, and the -// CI_GATE_REQUIRED token/env mapping that lets ci-gate still red the exact -// same token a failing check reddened before the fold. It also pins the -// FAST_CHECKS backstop token (the job's own .result), which gives ci-gate.sh -// one clear line if the whole job is lost before any step reports. +// TestPRCIGateFastChecksTokens pins pr.yml's fast-checks job, the base-diff +// checks no Bazel target can hold (check-migration-hygiene.sh's delta half +// and the .beads/issues.jsonl guard): each step's id, its outputs entry, and +// the CI_GATE_REQUIRED token/env mapping, plus the FAST_CHECKS backstop +// token (the job's own .result), which gives ci-gate.sh one clear line if the +// whole job is lost before any step reports. The job's former tree checks +// are Bazel tests (TestPRPolicyChecksRunAsBazelTargets); their tokens must +// not linger. func TestPRCIGateFastChecksTokens(t *testing.T) { pr := readCIWorkflow(t, "pr.yml") job := pr.job(t, "fast-checks") @@ -3196,23 +3215,22 @@ func TestPRCIGateFastChecksTokens(t *testing.T) { t.Errorf("ci-gate does not require fast-checks' own result as the FAST_CHECKS backstop: required=%v env=%q", required, gateStep.Env["FAST_CHECKS"]) } - // stepID -> (job output name, CI_GATE_REQUIRED token). - mapping := map[string]struct{ output, token string }{ - "build-tags": {"build-tags", "CHECK_BUILD_TAGS"}, - "version-consistency": {"version-consistency", "CHECK_VERSION_CONSISTENCY"}, - "migration-hygiene": {"migration-hygiene", "CHECK_MIGRATION_HYGIENE"}, - "no-beads-changes": {"no-beads-changes", "CHECK_NO_BEADS_CHANGES"}, - "fmt": {"fmt", "FMT_CHECK"}, + // stepID -> (step name, job output name, CI_GATE_REQUIRED token). + mapping := map[string]struct{ name, output, token string }{ + "migration-hygiene": {"Run migration hygiene checks", "migration-hygiene", "CHECK_MIGRATION_HYGIENE"}, + "no-beads-changes": {"Check for .beads/issues.jsonl changes", "no-beads-changes", "CHECK_NO_BEADS_CHANGES"}, + } + if len(job.Steps) != len(mapping)+1 { + t.Errorf("fast-checks has %d steps, want checkout plus %d: a tree-only check belongs in a Bazel target", len(job.Steps), len(mapping)) + } + if len(job.Outputs) != len(mapping) { + t.Errorf("fast-checks outputs = %v, want exactly %d", job.Outputs, len(mapping)) } for stepID, m := range mapping { - step := job.step(t, map[string]string{ - "build-tags": "Check build-tag policy", "version-consistency": "Check all versions match", - "migration-hygiene": "Run migration hygiene checks", "no-beads-changes": "Check for .beads/issues.jsonl changes", - "fmt": "Check gofmt", - }[stepID]) - // Every step runs on both PR triggers, merge_group included (the - // .beads diff takes merge_group.base_sha there), so the gate - // excuses none of their skips. + step := job.step(t, m.name) + // Both steps run on both PR triggers, merge_group included (the + // diffs take merge_group.base_sha there), so the gate excuses none + // of their skips. wantIf := "${{ !cancelled() }}" if step.ID != stepID || step.If != wantIf { t.Errorf("fast-checks step %q: id %q, if %q; want id %q, if %q", step.Name, step.ID, step.If, stepID, wantIf) @@ -3229,6 +3247,11 @@ func TestPRCIGateFastChecksTokens(t *testing.T) { t.Errorf("ci-gate env %s = %q, want %q", m.token, gateStep.Env[m.token], wantEnv) } } + for _, retired := range []string{"CHECK_BUILD_TAGS", "CHECK_VERSION_CONSISTENCY", "FMT_CHECK"} { + if contains(required, retired) || gateStep.Env[retired] != "" { + t.Errorf("ci-gate still carries %s; that check is a Bazel test now", retired) + } + } // Merge queue: the .beads guard runs on merge_group too, diffing // against the queue's base commit (a merge group has no base_ref), and // the gate no longer excuses its skip there. @@ -3242,25 +3265,87 @@ func TestPRCIGateFastChecksTokens(t *testing.T) { t.Error("ci-gate run script excuses a CHECK_NO_BEADS_CHANGES skip; the guard runs on merge_group too") } - // Review N-6 (2026-10-03): pin the folded steps' actual commands/env, not - // just their id/if/output/token wiring. These three gaps predate F7a (the - // standalone jobs were never pinned either), but fast-checks is now one - // job, so the cost of covering them here is marginal. - buildTags := job.step(t, "Check build-tag policy") - for _, want := range []string{ - "./scripts/check-build-tags.sh", - "./scripts/check-go-install-guidance.sh", - "./scripts/check-winget-portable-alias.sh", - } { - if !strings.Contains(buildTags.Run, want) { - t.Errorf("fast-checks build-tags step run does not call %s:\n%s", want, buildTags.Run) - } - } + // The migration step runs only the delta half (checks C, D and E) of + // check-migration-hygiene.sh against the PR's base; the tree half is + // //scripts/repochecks:migration_hygiene_test. migrationHygiene := job.step(t, "Run migration hygiene checks") const wantBaseSHA = "${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }}" if migrationHygiene.Env["BASE_SHA"] != wantBaseSHA { t.Errorf("fast-checks migration-hygiene step env BASE_SHA = %q, want %q", migrationHygiene.Env["BASE_SHA"], wantBaseSHA) } + if migrationHygiene.Env["MIGRATION_HYGIENE_SCOPE"] != "delta" { + t.Errorf("fast-checks migration-hygiene step MIGRATION_HYGIENE_SCOPE = %q, want delta", migrationHygiene.Env["MIGRATION_HYGIENE_SCOPE"]) + } + if migrationHygiene.Run != "./scripts/check-migration-hygiene.sh" { + t.Errorf("fast-checks migration-hygiene step run = %q", migrationHygiene.Run) + } +} + +// TestPRPolicyChecksRunAsBazelTargets: every repository guard pr.yml's +// fast-checks, PR Policy and doc-freshness jobs used to run on a GitHub +// runner is a Bazel test (ga-96smfk.20), with no tag, so bazel.yml's `test` +// lane (--config=ci, remote) runs it and CI Gate / Required gates on it, or, +// for the integration-tagged doc-freshness suite, the integration lane. +func TestPRPolicyChecksRunAsBazelTargets(t *testing.T) { + root := bazelPolicyRoot(t) + for _, c := range []struct { + build, target, runs string + tags string + }{ + {"scripts/repochecks/BUILD.bazel", "build_tags_test", `"scripts/check-build-tags.sh"`, ""}, + {"scripts/repochecks/BUILD.bazel", "workflow_tags_test", `"$(rlocationpath //scripts/checkworkflowtags)"`, ""}, + {"scripts/repochecks/BUILD.bazel", "go_install_guidance_test", `"scripts/check-go-install-guidance.sh"`, ""}, + {"scripts/repochecks/BUILD.bazel", "winget_portable_alias_test", `"scripts/check-winget-portable-alias.sh"`, ""}, + {"scripts/repochecks/BUILD.bazel", "migration_hygiene_test", `"MIGRATION_HYGIENE_SCOPE": "tree"`, ""}, + {"scripts/repochecks/BUILD.bazel", "fmt_test", `"scripts/ci/fmt-check.sh"`, ""}, + {"scripts/repochecks/BUILD.bazel", "doc_freshness_test", `"scripts/check-doc-freshness.sh"`, ""}, + {"scripts/repochecks/BUILD.bazel", "workapi_frontend_boundary_test", `"scripts/check-workapi-frontend-boundary.sh"`, ""}, + {"scripts/repochecks/BUILD.bazel", "types_gen_drift_test", `"types_gen_drift_test.sh"`, ""}, + {"internal/versioncheck/BUILD.bazel", "versioncheck_test", `"//:release_metadata_files"`, ""}, + {"scripts/BUILD.bazel", "scripts_test", `"check_testing_short_test.go"`, ""}, + {"scripts/BUILD.bazel", "doc_freshness_required_test", `"-required-suite=doc-freshness"`, `["integration-only"]`}, + {"scripts/gitattributespolicy/BUILD.bazel", "gitattributespolicy_required_host_test", `"-required-host"`, ""}, + } { + var rule string + for _, r := range bazelTopRules(readPolicyFile(t, root, c.build)) { + if strings.Contains(r, "\n name = \""+c.target+"\",\n") { + rule = r + } + } + if rule == "" { + t.Errorf("%s: no target %s", c.build, c.target) + continue + } + if !strings.Contains(rule, c.runs) { + t.Errorf("%s:%s no longer names %s", c.build, c.target, c.runs) + } + tags := regexp.MustCompile(`(?m)^ tags = (\[.*\]),$`).FindStringSubmatch(rule) + switch { + case c.tags == "" && tags != nil: + t.Errorf("%s:%s is tagged %s; untagged, it runs in the test lane", c.build, c.target, tags[1]) + case c.tags != "" && (tags == nil || tags[1] != c.tags): + t.Errorf("%s:%s tags = %v, want %s", c.build, c.target, tags, c.tags) + } + } + pr := readCIWorkflow(t, "pr.yml") + for name, job := range pr.Jobs { + for _, step := range job.Steps { + for _, moved := range []string{ + "check-build-tags.sh", "check-go-install-guidance.sh", "check-winget-portable-alias.sh", + "check-versions.sh", "make fmt-check", "check-doc-freshness.sh", "make ci-pr-policy", "make api-check", + } { + if strings.Contains(step.Run, moved) { + t.Errorf("pr.yml job %s step %q still runs %s, which is a Bazel test", name, step.Name, moved) + } + } + } + } + freshness := pr.job(t, "check-doc-freshness-platforms") + for _, tuple := range freshness.Strategy.Matrix.Include { + if tuple.ExpectedGOOS == "linux" { + t.Errorf("check-doc-freshness-platforms still has a Linux leg (%s); Bazel runs it", tuple.OS) + } + } } // Review N-6 (2026-10-03): test-windows-small's dbproxy-server leg must keep @@ -4722,7 +4807,7 @@ func TestBazelIntegrationJob(t *testing.T) { } assertTestStepKeepsExitStatus(t, test) count := job.step(t, "Every target and shard ran tests") - const wantCount = `python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" --not-go //tools/bazel:dolt_version_test` + const wantCount = `python3 tools/bazel/check_testcases.py --bep "$RUNNER_TEMP/bazel-bep.json" --not-go //tools/bazel:dolt_version_test --not-go //scripts/repochecks:all` if strings.TrimSpace(count.Run) != wantCount || count.If != "${{ always() && steps.test.outcome != 'skipped' }}" || (count.ContinueOnError != nil && count.ContinueOnError != false) { diff --git a/scripts/gitattributespolicy/BUILD.bazel b/scripts/gitattributespolicy/BUILD.bazel index cfe8b8098..c41371b52 100644 --- a/scripts/gitattributespolicy/BUILD.bazel +++ b/scripts/gitattributespolicy/BUILD.bazel @@ -10,6 +10,26 @@ go_test( data = ["//:.gitattributes"], ) +# pr.yml's check-doc-freshness-platforms Linux leg: `go test +# -tags integration ./scripts/gitattributespolicy -args -required-host +# -expected-goos linux` (the macOS and Windows legs stay in pr.yml): TestMain +# fails unless the EOL boundary test ran exactly once and passed on a Linux +# host. No file here is integration-tagged, so it runs in the ci lane. +go_test( + name = "gitattributespolicy_required_host_test", + srcs = [ + "git_boundary_test.go", + "gitattributes_policy_test.go", + "required_host_test.go", + ], + args = [ + "-required-host", + "-expected-goos", + "linux", + ], + data = ["//:.gitattributes"], +) + # --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- filegroup( diff --git a/scripts/pr_preflight_test.go b/scripts/pr_preflight_test.go index a49dd6c5f..1d5a9354b 100644 --- a/scripts/pr_preflight_test.go +++ b/scripts/pr_preflight_test.go @@ -3,7 +3,6 @@ package scripts_test import ( - "errors" "os" "os/exec" "path/filepath" @@ -766,14 +765,3 @@ func requireFlagValue(t *testing.T, args []string, flag string) string { t.Fatalf("missing %s value in %q", flag, args) return "" } - -func exitCode(err error) int { - if err == nil { - return 0 - } - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { - return exitErr.ExitCode() - } - return -1 -} diff --git a/scripts/pr_risk_bazel_coverage_test.go b/scripts/pr_risk_bazel_coverage_test.go index 291ae121e..200dfc7d8 100644 --- a/scripts/pr_risk_bazel_coverage_test.go +++ b/scripts/pr_risk_bazel_coverage_test.go @@ -1819,6 +1819,18 @@ func TestBazelRetiredLanesCannotBeNarrowed(t *testing.T) { // pinned ones above, and no .bzl macro, may select, skip or switch off // tests through its args, env or anything else. ruleNarrow := regexp.MustCompile(`-test\.(short|run|skip|list|bench)|BEADS_TEST_SKIP|BEADS_TEST_EMBEDDED_DOLT|TESTBRIDGE_TEST_ONLY|test_filter|flaky\s*=\s*(True|1|[A-Za-z_])`) + // Second runs over tests another target already runs in full: the + // selection is the point (a required-suite contract that checks it), and + // nothing leaves the lanes. Pinned to exactly these args. + extraRunVariants := map[string][]string{ + // The doc-freshness suite (also run by //scripts:scripts_test) under + // -required-suite, as pr.yml's former Linux doc-freshness leg ran it. + "//scripts:doc_freshness_required_test": { + "-test.count=1", + "-test.run=^(TestDocFreshness.*|TestRequiredSuiteContract)$$", + "-required-suite=doc-freshness", + }, + } err = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { if err != nil { return err @@ -1849,7 +1861,18 @@ func TestBazelRetiredLanesCannotBeNarrowed(t *testing.T) { for _, unit := range units { if !isBzl { if name := nameRe.FindStringSubmatch(unit); name != nil { - if _, pinned := want["//"+pkg+":"+name[1]]; pinned { + label := "//" + pkg + ":" + name[1] + if _, pinned := want[label]; pinned { + continue + } + if wantArgs, ok := extraRunVariants[label]; ok { + var args []string + for _, q := range quoted.FindAllStringSubmatch(bazelAttrBlock(unit, "args"), -1) { + args = append(args, q[1]) + } + if !reflect.DeepEqual(args, wantArgs) { + t.Errorf("%s args = %q, want exactly %q", label, args, wantArgs) + } continue } } diff --git a/scripts/release_script_test.go b/scripts/release_script_test.go index a4d7442a4..378f8c51a 100644 --- a/scripts/release_script_test.go +++ b/scripts/release_script_test.go @@ -218,17 +218,6 @@ func releaseFormulaPath(t *testing.T) string { return filepath.Join(sourceRepoRoot(t), ".beads", "formulas", "beads-release.formula.toml") } -func sourceRepoRoot(t *testing.T) string { - t.Helper() - _, file, _, ok := runtime.Caller(0) - if !ok { - t.Fatal("runtime.Caller failed") - } - // Under Bazel the caller path is workspace-relative; CallerDir rebuilds it - // under the runfiles root, which holds the files scripts_test declares. - return filepath.Dir(bazeltest.CallerDir(file, "scripts")) -} - func runReleaseDryRun(t *testing.T, repo, bin string) (string, error) { t.Helper() return runReleaseDryRunWithEnv(t, repo, bin, "BD=") @@ -259,15 +248,6 @@ func runReleaseDryRunWithEnv(t *testing.T, repo, bin string, extraEnv ...string) return string(out), err } -func msysPath(path string) string { - path = filepath.Clean(path) - path = filepath.ToSlash(path) - if len(path) >= 3 && path[1] == ':' && path[2] == '/' { - return "/" + strings.ToLower(path[:1]) + path[2:] - } - return path -} - func shellPath(t *testing.T, path string) string { t.Helper() clean := filepath.Clean(path) diff --git a/scripts/repo_helpers_test.go b/scripts/repo_helpers_test.go new file mode 100644 index 000000000..c44695897 --- /dev/null +++ b/scripts/repo_helpers_test.go @@ -0,0 +1,47 @@ +package scripts_test + +// Helpers shared by the package's test files, kept apart so a focused +// go_test over a few of them (//scripts:doc_freshness_required_test) compiles +// without the rest of the package. + +import ( + "errors" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/testutil/bazeltest" +) + +func sourceRepoRoot(t *testing.T) string { + t.Helper() + _, file, _, ok := runtime.Caller(0) + if !ok { + t.Fatal("runtime.Caller failed") + } + // Under Bazel the caller path is workspace-relative; CallerDir rebuilds it + // under the runfiles root, which holds the files scripts_test declares. + return filepath.Dir(bazeltest.CallerDir(file, "scripts")) +} + +func msysPath(path string) string { + path = filepath.Clean(path) + path = filepath.ToSlash(path) + if len(path) >= 3 && path[1] == ':' && path[2] == '/' { + return "/" + strings.ToLower(path[:1]) + path[2:] + } + return path +} + +func exitCode(err error) int { + if err == nil { + return 0 + } + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + return exitErr.ExitCode() + } + return -1 +} diff --git a/scripts/repochecks/BUILD.bazel b/scripts/repochecks/BUILD.bazel new file mode 100644 index 000000000..180425feb --- /dev/null +++ b/scripts/repochecks/BUILD.bazel @@ -0,0 +1,353 @@ +load("@rules_shell//shell:sh_test.bzl", "sh_test") +load("//tools/bazel:repo_subset.bzl", "repo_subset") + +# Repository guards that pr.yml's fast-checks and PR Policy jobs ran with +# `./scripts/check-*.sh` on a full checkout, as Bazel tests over exactly the +# files each guard reads (tools/bazel/repo_subset.bzl), so a cached pass is +# reused until one of those files changes. run_check.sh lays each slice out as +# a throwaway git checkout (the guards use git ls-files / git grep) and runs +# the guard from its root. No tags: they run in bazel.yml's `test` lane +# (--config=ci), remotely, and gate through CI Gate / Required. None of them +# is a Go test binary, so every target here is exempt from +# tools/bazel/check_testcases.py's Go testcase count (--not-go +# //scripts/repochecks:all in bazel.yml's integration lane). +# +# What each slice holds must cover what its guard reads: a renamed input fails +# the build (repo_subset rejects a pattern that matches nothing), and a guard +# over a slice missing a file it names fails rather than passing (e.g. +# check-doc-freshness.sh's "missing document" / "path does not exist"). + +# run_check.sh and repo_subset themselves: include and exclude patterns, the +# checkout layout and --env (testdata/assert_tree.sh says what it expects). +repo_subset( + name = "run_check_fixture", + srcs = ["//:repo_files"], + include = [ + "scripts/repochecks/testdata/*.txt", + "scripts/repochecks/testdata/assert_tree.sh", + "scripts/repochecks/testdata/sub/**", + ], + exclude = ["scripts/repochecks/testdata/sub/deep/**"], +) + +sh_test( + name = "run_check_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :run_check_fixture)", + "--env", + "PROBE=$(rlocationpath testdata/a.txt)", + "--", + "scripts/repochecks/testdata/assert_tree.sh", + ], + data = [ + "testdata/a.txt", + ":run_check_fixture", + ], +) + +# check-build-tags.sh: every `go build|test|run|generate|install` in a shell +# script, hook or the Makefile builds with gms_pure_go, and every Bazel user +# (incl. the workflows) is covered by .bazelrc's tag. Its git ls-files +# pathspecs, as a slice. +repo_subset( + name = "build_tags_files", + srcs = ["//:repo_files"], + include = [ + "**/*.sh", + ".bazelrc", + ".github/scripts/**", + ".github/workflows/*.yml", + ".githooks/**", + "Makefile", + ], +) + +sh_test( + name = "build_tags_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :build_tags_files)", + "--", + "scripts/check-build-tags.sh", + ], + data = [":build_tags_files"], +) + +repo_subset( + name = "workflow_files", + srcs = ["//:repo_files"], + include = [".github/workflows/*.yml"], +) + +# scripts/checkworkflowtags (`go run ./scripts/checkworkflowtags` in +# scripts/ci/pr-policy.sh): the workflows' direct go commands declare +# gms_pure_go. +sh_test( + name = "workflow_tags_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :workflow_files)", + "--", + "$(rlocationpath //scripts/checkworkflowtags)", + ], + data = [ + ":workflow_files", + "//scripts/checkworkflowtags", + ], +) + +# check-go-install-guidance.sh `git grep`s every tracked file, so its slice is +# the whole checkout: //:repo_files plus the one tracked file under the +# .bazelignore'd .beads/. +repo_subset( + name = "all_files", + srcs = ["//:repo_files"], + include = ["**/*"], +) + +repo_subset( + name = "beads_formula_files", + srcs = ["@beads_formulas//:beads-release.formula.toml"], + include = ["beads-release.formula.toml"], + tree_prefix = ".beads/formulas/", +) + +sh_test( + name = "go_install_guidance_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :all_files)", + "--tree", + "$(rlocationpath :beads_formula_files)", + "--", + "scripts/check-go-install-guidance.sh", + ], + data = [ + ":all_files", + ":beads_formula_files", + ], +) + +repo_subset( + name = "winget_files", + srcs = ["//:repo_files"], + include = [ + "scripts/check-winget-portable-alias.sh", + "winget/*.installer.yaml", + ], +) + +# check-winget-portable-alias.sh: every nested-portable winget installer +# carries PortableCommandAlias: bd (GH#4908). yq comes from the executor, as +# for //scripts:scripts_test. +sh_test( + name = "winget_portable_alias_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :winget_files)", + "--", + "scripts/check-winget-portable-alias.sh", + ], + data = [":winget_files"], +) + +repo_subset( + name = "migration_files", + srcs = ["//:repo_files"], + include = [ + "internal/storage/schema/migrations/**", + "scripts/check-migration-hygiene.sh", + ], +) + +# check-migration-hygiene.sh's tree half (checks A and B: duplicate versions, +# nondeterministic SQL and its allowlist). The delta half (C, D and E, which +# diff against the PR's base) stays in pr.yml's pr-diff-checks job. +sh_test( + name = "migration_hygiene_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :migration_files)", + "--", + "scripts/check-migration-hygiene.sh", + ], + data = [":migration_files"], + env = {"MIGRATION_HYGIENE_SCOPE": "tree"}, +) + +repo_subset( + name = "go_files", + srcs = ["//:repo_files"], + include = [ + "**/*.go", + "scripts/ci/fmt-check.sh", + "scripts/ci/gofmt-bin.sh", + ], +) + +# `make fmt-check` (scripts/ci/fmt-check.sh): gofmt -l over every Go file, +# with the registered SDK's gofmt (go.mod's toolchain; the policy tests pin +# the two together), which is what gofmt-bin.sh resolves from go.mod. +sh_test( + name = "fmt_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :go_files)", + "--env", + "GOFMT=$(rlocationpath @go_sdk//:bin/gofmt)", + "--", + "scripts/ci/fmt-check.sh", + ], + data = [ + ":go_files", + "@go_sdk//:bin/gofmt", + ], +) + +# check-doc-freshness.sh: the reference docs it lists, DOC_INVENTORY.md, and +# each doc's freshness sources, whose existence it checks (a Bazel package's +# BUILD.bazel stands in for a directory source). Keep in step with its DOCS +# list; a doc or source missing here fails the test. +# +# The guard compares Last reviewed markers with today's date, which is not in +# the action key: a cached pass stands until one of these files changes, and +# nightly (--config=fresh) re-runs it against the date. +repo_subset( + name = "doc_freshness_files", + srcs = ["//:repo_files"], + include = [ + ".buildflags", + ".github/workflows/main.yml", + ".github/workflows/pr.yml", + ".golangci.yml", + "Makefile", + "cmd/bd/ado*.go", + "cmd/bd/config.go", + "cmd/bd/dolt.go", + "cmd/bd/errors.go", + "cmd/bd/find_duplicates.go", + "cmd/bd/init.go", + "cmd/bd/init_safety.go", + "cmd/bd/init_safety_help.go", + "cmd/bd/init_safety_test.go", + "cmd/bd/main.go", + "cmd/bd/output.go", + "cmd/bd/protocol/json_contract_test.go", + "cmd/bd/serve.go", + "cmd/bd/setup*.go", + "docs/getting-started/ide-setup.md", + "docs/integrations/azure-devops.md", + "docs/recovery/init-safety.md", + "docs/reference/configuration.md", + "docs/reference/json-schema.md", + "engdocs/CI_CLEANUP_PLAN.md", + "engdocs/CI_TEST_SURFACE_AUDIT.md", + "engdocs/DOC_INVENTORY.md", + "engdocs/ERROR_HANDLING.md", + "engdocs/LINTING.md", + "engdocs/SERVE_RUNBOOK.md", + "engdocs/design/otel/otel-data-model.md", + "internal/ado/BUILD.bazel", + "internal/compact/haiku.go", + "internal/configfile/BUILD.bazel", + "internal/hooks/BUILD.bazel", + "internal/httpapi/auth.go", + "internal/httpapi/events_watch.go", + "internal/httpapi/server.go", + "internal/recipes/BUILD.bazel", + "internal/storage/dolt/store.go", + "internal/telemetry/BUILD.bazel", + "scripts/check-doc-freshness.sh", + "scripts/ci/pr-lint.sh", + "scripts/pr-lint/BUILD.bazel", + ], +) + +sh_test( + name = "doc_freshness_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :doc_freshness_files)", + "--", + "scripts/check-doc-freshness.sh", + ], + data = [":doc_freshness_files"], +) + +repo_subset( + name = "workapi_files", + srcs = ["//:repo_files"], + include = [ + "internal/workapi/**", + "scripts/check-workapi-frontend-boundary.sh", + ], +) + +# check-workapi-frontend-boundary.sh: internal/workapi reads no cwd- or +# environment-derived state (the symbol half of the depguard rule). +sh_test( + name = "workapi_frontend_boundary_test", + srcs = ["run_check.sh"], + args = [ + "--tree", + "$(rlocationpath :workapi_files)", + "--", + "scripts/check-workapi-frontend-boundary.sh", + ], + data = [":workapi_files"], +) + +# `make api-check`'s drift half: internal/httpapi/apigen/types.gen.go is +# exactly what oapi-codegen (go.mod's version, the `go tool` doc.go's +# go:generate runs) makes of the spec. Its `go test ./internal/httpapi/...` +# half is the httpapi targets in the same lane. +sh_test( + name = "types_gen_drift_test", + srcs = ["types_gen_drift_test.sh"], + data = [ + "//:go.mod", + "//internal/httpapi/apigen:types.gen.go", + "//internal/httpapi/spec:openapi.v0.yaml", + "@com_github_oapi_codegen_oapi_codegen_v2//cmd/oapi-codegen", + "@go_sdk//:bin/go", + "@go_sdk//:files", + ], + env = { + "COMMITTED": "$(rlocationpath //internal/httpapi/apigen:types.gen.go)", + "GO": "$(rlocationpath @go_sdk//:bin/go)", + "GO_MOD": "$(rlocationpath //:go.mod)", + "OAPI_CODEGEN": "$(rlocationpath @com_github_oapi_codegen_oapi_codegen_v2//cmd/oapi-codegen)", + "SPEC": "$(rlocationpath //internal/httpapi/spec:openapi.v0.yaml)", + }, +) + +# --- 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/scripts/repochecks/run_check.sh b/scripts/repochecks/run_check.sh new file mode 100755 index 000000000..3bfaaf125 --- /dev/null +++ b/scripts/repochecks/run_check.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash +# run_check.sh: run a repository guard over a declared slice of the checkout. +# +# run_check.sh [--tree MANIFEST]... [--env NAME=RLOCATION]... \ +# [--path RLOCATION]... -- PROGRAM [ARG...] +# +# The guards under scripts/ (check-build-tags.sh, fmt-check.sh, ...) walk the +# repository root above their own directory, often with `git ls-files` or +# `git grep`. Under `bazel test` they get instead a throwaway checkout built +# from exactly their declared inputs: +# +# --tree MANIFEST a repo_subset manifest (tools/bazel/repo_subset.bzl), +# as an rlocation path: every file it lists is copied +# (dereferenced) to its checkout path, then the tree is +# `git add`ed, so git ls-files/grep see those files. +# --env NAME=RLOC export NAME as the absolute path of a runfile (e.g. +# GOFMT=). +# --path RLOC put the runfile's directory first on PATH. +# +# PROGRAM is a checkout path (scripts/check-build-tags.sh) when the tree holds +# it, so the guard resolves its repository root to the tree; otherwise an +# rlocation path (a Bazel-built tool). It runs from the tree's root. +set -euo pipefail + +runfiles="${TEST_SRCDIR:?run_check.sh runs under bazel test}" +tree="${TEST_TMPDIR:?}/checkout" +manifests=() +path_dirs=() +envs=() +while [[ $# -gt 0 ]]; do + case "$1" in + --tree) manifests+=("$2"); shift 2 ;; + --env) envs+=("$2"); shift 2 ;; + --path) path_dirs+=("$2"); shift 2 ;; + --) shift; break ;; + *) echo "run_check.sh: unknown argument $1" >&2; exit 2 ;; + esac +done +[[ $# -ge 1 ]] || { echo "run_check.sh: no PROGRAM" >&2; exit 2; } +[[ ${#manifests[@]} -gt 0 ]] || { echo "run_check.sh: no --tree" >&2; exit 2; } + +rm -rf "$tree" +mkdir -p "$tree" +files=0 +for m in "${manifests[@]}"; do + while read -r rel rloc; do + [[ -n "$rel" ]] || continue + mkdir -p "$tree/$(dirname "$rel")" + cp -L "$runfiles/$rloc" "$tree/$rel" + files=$((files + 1)) + done <"$runfiles/$m" +done +echo "run_check.sh: checkout of $files declared files" + +git -C "$tree" init -q +git -C "$tree" add -f -A . + +for e in "${envs[@]+"${envs[@]}"}"; do + export "${e%%=*}=$runfiles/${e#*=}" +done +for p in "${path_dirs[@]+"${path_dirs[@]}"}"; do + PATH="$(dirname "$runfiles/$p"):$PATH" +done +export PATH + +program="$1" +shift +if [[ -e "$tree/$program" ]]; then + program="$tree/$program" +else + program="$runfiles/$program" +fi +cd "$tree" +exec "$program" "$@" diff --git a/scripts/repochecks/testdata/a.txt b/scripts/repochecks/testdata/a.txt new file mode 100644 index 000000000..789819226 --- /dev/null +++ b/scripts/repochecks/testdata/a.txt @@ -0,0 +1 @@ +a diff --git a/scripts/repochecks/testdata/assert_tree.sh b/scripts/repochecks/testdata/assert_tree.sh new file mode 100755 index 000000000..e8582467a --- /dev/null +++ b/scripts/repochecks/testdata/assert_tree.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Run by //scripts/repochecks:run_check_test through run_check.sh: the +# checkout holds exactly the slice's files, at their repository paths, under +# git, from the checkout root, with --env exported as an absolute path. +set -euo pipefail +want="scripts/repochecks/testdata/a.txt +scripts/repochecks/testdata/assert_tree.sh +scripts/repochecks/testdata/sub/b.sh" +got="$(git ls-files)" +if [[ "$got" != "$want" ]]; then + printf 'git ls-files:\n%s\nwant:\n%s\n' "$got" "$want" >&2 + exit 1 +fi +[[ "$(pwd -P)" == "$(git rev-parse --show-toplevel)" ]] || { echo "not run from the checkout root" >&2; exit 1; } +[[ ! -L scripts/repochecks/testdata/a.txt ]] || { echo "files must be copies, not runfiles symlinks" >&2; exit 1; } +[[ "$PROBE" == /* && "$(cat "$PROBE")" == a ]] || { echo "PROBE=$PROBE is not the absolute runfile" >&2; exit 1; } +echo "run_check.sh: tree, cwd and --env as expected" diff --git a/scripts/repochecks/testdata/skip.md b/scripts/repochecks/testdata/skip.md new file mode 100644 index 000000000..e32e76dd5 --- /dev/null +++ b/scripts/repochecks/testdata/skip.md @@ -0,0 +1 @@ +skip diff --git a/scripts/repochecks/testdata/sub/b.sh b/scripts/repochecks/testdata/sub/b.sh new file mode 100755 index 000000000..c4ced2b4e --- /dev/null +++ b/scripts/repochecks/testdata/sub/b.sh @@ -0,0 +1,2 @@ +#!/bin/sh +echo b diff --git a/scripts/repochecks/testdata/sub/deep/c.sh b/scripts/repochecks/testdata/sub/deep/c.sh new file mode 100755 index 000000000..ca5ed2595 --- /dev/null +++ b/scripts/repochecks/testdata/sub/deep/c.sh @@ -0,0 +1,2 @@ +#!/bin/sh +echo c diff --git a/scripts/repochecks/types_gen_drift_test.sh b/scripts/repochecks/types_gen_drift_test.sh new file mode 100755 index 000000000..0f72d15f6 --- /dev/null +++ b/scripts/repochecks/types_gen_drift_test.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# types_gen_drift_test.sh: `make api-check`'s drift half under Bazel. Regenerate +# types.gen.go from the spec as doc.go's `go generate` does, and fail with the +# diff when the committed copy is stale. Inputs are rlocation paths (BUILD +# env): COMMITTED, SPEC, GO_MOD, OAPI_CODEGEN and GO. +set -euo pipefail + +r="${TEST_SRCDIR:?}" +work="${TEST_TMPDIR:?}/apigen" +rm -rf "$work" +mkdir -p "$work" + +# The Bazel-built oapi-codegen has no module build info, so its "Code +# generated by" header names an unknown version; `go tool oapi-codegen` names +# go.mod's. +version=$(awk '$1 == "github.com/oapi-codegen/oapi-codegen/v2" { print $2; exit }' "$r/$GO_MOD") +if [[ -z "$version" ]]; then + echo "go.mod does not require github.com/oapi-codegen/oapi-codegen/v2" >&2 + exit 1 +fi + +# oapi-codegen formats its output with golang.org/x/tools/imports, which runs +# `go env`: the registered SDK, outside any module, with a private, empty +# module cache and no network. +( + cd "$work" + PATH="$(dirname "$r/$GO"):$PATH" HOME="$work" GOPATH="$work/gopath" \ + GOMODCACHE="$work/gopath/pkg/mod" GOCACHE="$work/gocache" GOENV=off \ + GOFLAGS='' GOTOOLCHAIN=local GOPROXY=off \ + "$r/$OAPI_CODEGEN" -generate types,skip-prune -package apigen \ + -o "$work/types.raw.go" "$r/$SPEC" +) +sed "s|^// Code generated by unknown module path version unknown version DO NOT EDIT[.]$|// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version $version DO NOT EDIT.|" \ + "$work/types.raw.go" >"$work/types.gen.go" + +if ! diff -u "$r/$COMMITTED" "$work/types.gen.go"; then + echo "openapi drift: internal/httpapi/apigen/types.gen.go is stale for the current spec (regenerating it changed it)." + echo "run 'make api-gen' and commit the result." + exit 1 +fi +echo "internal/httpapi/apigen/types.gen.go matches internal/httpapi/spec/openapi.v0.yaml (oapi-codegen $version)" diff --git a/tools/bazel/check_testcases.py b/tools/bazel/check_testcases.py index 0b89ee2ad..10cabcc35 100755 --- a/tools/bazel/check_testcases.py +++ b/tools/bazel/check_testcases.py @@ -18,7 +18,9 @@ target's test.xml, and every shard's, to list at least one top-level as //tools/bazel:dolt_version_test, whose test.xml Bazel writes with no Go testcases) from the count. The label must still be in the BEP: an exemption for a target the run did not test is an error, so a stale one -cannot linger. +cannot linger. //pkg:all exempts every test in that package (one that holds +only non-Go tests, such as //scripts/repochecks), and must match at least one +tested target. Exit status: 0 if every test.xml lists a test, 1 otherwise (including a BEP with no test results at all). @@ -48,10 +50,16 @@ def check(tested, testlogs, not_go=()): counts, problems = [], [] if not tested: problems.append("the BEP lists no test results") - for label in sorted(set(not_go) - set(tested)): - problems.append(f"{label}: --not-go, but the BEP has no result for it") + def exempt(label, entry): + if entry.endswith(":all"): + return label.split(":", 1)[0] == entry[: -len(":all")] + return label == entry + + for entry in sorted(set(not_go)): + if not any(exempt(label, entry) for label in tested): + problems.append(f"{entry}: --not-go, but the BEP has no result for it") for label, shards in sorted(tested.items()): - if label in not_go: + if any(exempt(label, entry) for entry in not_go): continue for xml_path in testlog_xmls(testlogs, label, shards): rel = os.path.relpath(xml_path, testlogs) diff --git a/tools/bazel/go_srcs.py b/tools/bazel/go_srcs.py index c06af7cfa..efb0cc977 100755 --- a/tools/bazel/go_srcs.py +++ b/tools/bazel/go_srcs.py @@ -133,6 +133,7 @@ ROOT_REPO_FILES_EXCLUDE = ( # Who may read //:repo_files: the repository-policy tests. REPO_FILES_VISIBILITY = ( "//scripts:__pkg__", + "//scripts/repochecks:__pkg__", "//test/docsync:__pkg__", ) diff --git a/tools/bazel/repo_subset.bzl b/tools/bazel/repo_subset.bzl new file mode 100644 index 000000000..882a98e85 --- /dev/null +++ b/tools/bazel/repo_subset.bzl @@ -0,0 +1,87 @@ +"""repo_subset: a narrow, path-selected slice of the checkout as test data. + +Repository guards (scripts/check-*.sh) read the tracked tree. Declaring all +of //:repo_files as their data would key every one of them to every file in +the repository, so an edit to any Markdown file re-runs the gofmt guard. +repo_subset selects, at analysis time, only the files a guard reads: the +test's action key then covers exactly those files. + +Patterns match a file's repository-relative path: + + "**/*.ext" the suffix, at any depth ("**/*.sh") + "dir/**" everything below dir + "dir/*.ext" files directly in dir with that suffix ("" dir: the root) + "path" exactly that file + +Every pattern must match at least one file, so a renamed input fails the +build loudly instead of letting the guard pass over an empty tree. + +The rule also writes a manifest, one " " line per +file, which scripts/repochecks/run_check.sh uses to lay the slice out as a +throwaway git checkout. `tree_prefix` places files from another repository +(e.g. @beads_formulas) at their checkout path. +""" + +def _matches(path, pattern): + if pattern.startswith("**/*"): + return path.endswith(pattern[len("**/*"):]) + if pattern.endswith("/**"): + return path.startswith(pattern[:-len("**")]) + if "*" in pattern: + prefix, suffix = pattern.split("*", 1) + if not (path.startswith(prefix) and path.endswith(suffix)): + return False + middle = path[len(prefix):len(path) - len(suffix)] + return "/" not in middle and len(path) >= len(prefix) + len(suffix) + return path == pattern + +def _rel(f): + """f's path within its own repository.""" + if f.short_path.startswith("../"): + return f.short_path[len("../"):].split("/", 1)[1] + return f.short_path + +def _rlocation(ctx, f): + """f's runfiles path (what $(rlocationpath) would give).""" + if f.short_path.startswith("../"): + return f.short_path[len("../"):] + return ctx.workspace_name + "/" + f.short_path + +def _repo_subset_impl(ctx): + files = ctx.files.srcs + picked = [] + unmatched = list(ctx.attr.include) + for f in files: + rel = _rel(f) + if any([_matches(rel, p) for p in ctx.attr.exclude]): + continue + hit = False + for p in ctx.attr.include: + if _matches(rel, p): + hit = True + if p in unmatched: + unmatched.remove(p) + if hit: + picked.append(f) + if unmatched: + fail("repo_subset %s: no file matches %s" % (ctx.label, unmatched)) + + lines = [] + for f in picked: + lines.append("%s%s %s" % (ctx.attr.tree_prefix, _rel(f), _rlocation(ctx, f))) + manifest = ctx.actions.declare_file(ctx.label.name + ".manifest") + ctx.actions.write(manifest, "\n".join(sorted(lines)) + "\n") + return [DefaultInfo( + files = depset([manifest]), + runfiles = ctx.runfiles(files = picked + [manifest]), + )] + +repo_subset = rule( + implementation = _repo_subset_impl, + attrs = { + "srcs": attr.label_list(allow_files = True, mandatory = True), + "include": attr.string_list(mandatory = True), + "exclude": attr.string_list(), + "tree_prefix": attr.string(doc = "Prepended to every file's checkout path."), + }, +)