beads/scripts/BUILD.bazel
Rongjun GENG 7838c4b449
fix(scripts): update-nix-vendorhash no longer reports a build failure as success (#7504)
* fix(scripts): update-nix-vendorhash no longer reports a build failure as success

scripts/update-nix-vendorhash.sh treated the absence of a "got:" hash
line as proof the vendorHash was already correct, so a build failure
unrelated to a hash mismatch (e.g. a Go toolchain too old for a
dependency) was misreported as success and the workflow went green on
a broken build (#7496). Fixed by capturing the intentional-failure
build's real exit status instead of discarding it with `|| true`, and
using that exit status (not a "hash mismatch" string grep) to tell a
genuine no-op apart from an unrelated build break.

The step-5 verification build had the same shape of bug: piping
through `tee` without pipefail meant the if-condition tested tee's
exit status, not the build's, so a failing verification build still
printed "Build successful!". Scoped `set -o pipefail` to just that one
line rather than the whole script, since the earlier hash-extraction
step legitimately relies on `grep -o` returning non-zero on no match.

Fixes gastownhall/beads#7498.

Added an independent `nix build .#default` step to
update-vendor-hash.yml so the workflow no longer trusts the script's
exit status alone, even if a future bug in the script reintroduces a
false-success path.

Fixes gastownhall/beads#7499.

Added TestPinnedDoltCLIAtLeastRecommendedMin, asserting the pinned
Dolt CLI version (scripts/ci/install-dolt.sh) is at least
doltversion.RecommendedMin, so the two policies can't silently drift
apart — RecommendedMin's own doc comment already claims this alignment
but nothing enforced it.

Fixes gastownhall/beads#7500.

Verified by reproducing both update-nix-vendorhash.sh failure modes
with a stub `nix` binary (a non-hash build failure, and a step-5
verification build failure) and confirming the intended successful
hash-update path still works end to end. go build/vet clean,
golangci-lint 0 issues, go test ./scripts/... passes except a
pre-existing unrelated failure (TestControlQueryScriptPreservesReadyProbeFailure,
tracked separately as beads#7293 and untouched by this change).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VN2SgXAHW1eTB1izwWwmyr

* chore: empty commit to retrigger CI -- retrying after a 3-hour quota recovery window

* chore: empty commit to retrigger CI -- isolated single retry after real-bug fixes landed on sibling PRs

* fix(scripts): add missing BUILD.bazel dep, sharpen vendorhash diagnostics

scripts_test now imports internal/doltversion (for
TestPinnedDoltCLIAtLeastRecommendedMin) but scripts/BUILD.bazel had no
matching dep row, which would fail the target's compile under
rules_go once Bazel actually runs against this branch (the Bazel /
jobs failing so far died in bazel.fork-cert, unrelated to this).

Also, per review: scope set -o pipefail to just the step-5 verify
block (turn it back off after, rather than leaving it on for the rest
of the script with a comment claiming it's scoped when it isn't), and
keep the old "hash mismatch" string check as a sub-case in the
ACTUAL_HASH-empty branch, so a hash mismatch whose got: line fails to
parse (e.g. a future Nix output-format change) is diagnosed
differently from a build that failed for an unrelated reason.

Re-verified all four failure/success paths with a stub nix binary
(non-hash failure, hash-mismatch-with-unparseable-got-line,
step-5-verify failure, and the genuine happy path). go build/vet
clean, golangci-lint 0 issues.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VN2SgXAHW1eTB1izwWwmyr

* fix(scripts): update-nix-vendorhash preserves operator backups, restores on any handled failure

The script backed up default.nix to a fixed name in the repo root,
default.nix.backup, before editing it -- but that is also the
conventional name an operator gives their own manual backup. Running
the script unconditionally overwrote (then later deleted) any such
file. A failed step-5 verification build also left the candidate hash
in default.nix with no backup at all to restore from, since the
backup had already been deleted right after step 4 applied the real
hash.

Moved the backup into a private per-run scratch directory
(mktemp -d, cleaned on every exit via an EXIT trap) instead of a
fixed repo-root name, so it can never collide with an operator's own
default.nix.backup. Copy with cp -p so a restore brings back the
exact original bytes AND mode, not just content. Kept the backup
around through step 5 instead of deleting it right after step 4, and
added a restore on the step-5 verification-failure path, which
previously left the unverified candidate hash in place.

Also: the "no hash mismatch, already correct" exit-0 path previously
inferred correctness purely from the fake-hash probe build's
unexpected success, without ever building the real, restored file.
It now runs a second build against the restored file and only exits 0
if that succeeds too, surfacing a diagnostic log if it doesn't.

Fixes gastownhall/beads#7529.

Verified with a stub nix binary across five scenarios: a pre-existing
operator default.nix.backup (distinct content + mode 600) survives
untouched in every one; a non-hash build failure restores the
original (content + mode 640); the already-correct path now runs two
builds and only succeeds if both do; the core bug repro (hash
extracted, step-5 verify fails) now restores the original instead of
leaving the unverified candidate hash with no backup; the genuine
happy path still updates the hash correctly. No leftover scratch
directories in any case. go build unaffected (shell-only change).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VN2SgXAHW1eTB1izwWwmyr

* fix(scripts): update-nix-vendorhash restores the original on an interrupted run

A SIGINT/SIGTERM during the probe build (the long step) reaches this
script and its nix child as one process group; the child dies, this
script exits through the EXIT trap with the placeholder hash still in
default.nix. The plain `rm -rf "$SCRATCH_DIR"` trap deleted the only
backup at that point, leaving no way back except `git checkout
default.nix` -- which also discards any uncommitted operator edits.
Worse than every handled failure case this file already restores
from.

The placeholder is never a valid end state, so the EXIT trap now
checks for it and restores first, before dropping the scratch
directory. Every handled exit path has already restored or verified
default.nix by the time it runs, so this is a no-op there -- it only
fires on an interrupted run.

Found by review on PR #7504 (beads#7529's own follow-up): reproduced
and confirmed with a stub nix that signals its own parent process
group, matching a real Ctrl-C mid-build. Confirmed the bug first
(plain trap: placeholder left in place, backup gone, matching the
report exactly), then confirmed the fix (original hash and mode
restored, operator backup untouched, no leftover scratch dir). Also
re-ran the full existing scenario matrix (non-hash failure, hash
mismatch with step-5 verify failure, genuine happy path) to confirm no
regression.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VN2SgXAHW1eTB1izwWwmyr

* fix(scripts): restore default.nix if vendorhash verification is interrupted

The EXIT trap restored default.nix only while it still held the placeholder
hash. A SIGINT/SIGTERM during the step-5 verification build therefore left
the unverified candidate hash in place and deleted the only backup with the
scratch dir. Track VERIFIED instead: until a build has verified default.nix
as it stands, any exit restores the original. On handled paths this is a
no-op, since they have already restored or verified the file.

Write the build logs under ${TMPDIR:-/tmp} with per-run names. The
verification log had a fixed /tmp name shared by every run and every user.

Add the regression tests the #7498/#7499/#7529 fixes lacked:
- TestUpdateNixVendorHashReportsTheRealBuildResult runs the script with a
  stub nix over a non-hash build failure, an unparseable mismatch, a failed
  verification, a half-parsed got: hash, a real update, and a probe that
  accepts the placeholder (restored file builds, or not). Every failure
  exits 1 with default.nix restored byte for byte and mode for mode, the
  operator's own default.nix.backup untouched and the named logs kept.
  Against main's script it reproduces each false success.
- TestUpdateNixVendorHashInterruptRestoresOriginal signals the script's
  process group during the probe build and during verification.
- TestUpdateVendorHashWorkflowBuildsIndependently pins the workflow's
  unconditional `nix build .#default` between the script and the
  match-report and push steps.

The scenario matrix, operator-backup and file-mode fixture, and workflow
ordering check are adapted from BlueboxC's correction of this PR in their
fork (62e7718e), as described on #7498 and #7529.

Refs #7504, #7496, #7498, #7499, #7500, #7529

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-authored-by: BlueboxC <david@blueboxcomputers.es>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: BlueboxC <david@blueboxcomputers.es>
2026-10-14 22:18:15 +00:00

448 lines
16 KiB
Python

load("@rules_go//go:def.bzl", "go_test")
# CI and repo-policy tests: they parse the workflows, run the CI shell scripts
# against fixtures, and check repository-wide rules (BUILD tag taxonomy, skip
# allowlist, managed BUILD blocks, shard manifests, tracked-file scans).
#
# They are one Go package but several go_test targets, each compiling only
# the files whose tests it runs and declaring only what those tests read, so
# that a change re-runs only the targets that read it: a Go-only change runs
# no workflow policy test, a docs-only or workflow-only change no Go-source
# scan. The data are the partitions of the checkout that tools/bazel/go_srcs.py
# generates (//:repo_go_srcs, //:repo_go_test_srcs, //:repo_doc_files,
# //:repo_other_files, and per package) or single files; only
# :tracked_files_test declares //:repo_files, every tracked file.
#
# Every *_test.go here is in exactly one of these targets, except the
# helpers-only files (no Test functions) that several compile; srcs are kept
# by hand (`# keep` stops gazelle from putting every file back into
# scripts_test), and TestScriptsTestTargetsPartitionTheTests
# (scripts_targets_test.go) fails on a file in no target or a test in two.
#
# No tags: they run remotely like any other target. The scripts and fixtures
# run bash, git, python3, jq, yq, curl, openssl, unzip and coreutils from the
# executor (the rbe-west worker image, or the runner in fork-cache mode); a
# missing tool fails the test under Bazel instead of skipping it
# (host_tools_test.go), so a cached pass never stands for an executor that
# could not run it. Go and the release formula come in hermetically.
# The CI and Bazel policy tests: workflows, the setup-bazel action, .bazelrc,
# MODULE.bazel, every BUILD and .bzl file, tools/bazel and the CI scripts,
# all in //:repo_other_files (every tracked file but Go and Markdown source),
# plus the one Go file whose dolt image pin they compare.
go_test(
name = "scripts_test",
srcs = [
"bazel_autofix_push_test.go",
"bazel_cache_mode_test.go",
"bazel_dolt_int_test.go",
"bazel_dolt_race_test.go",
"bazel_farm_workflow_test.go",
"bazel_fork_mode_test.go",
"bazel_go_repository_tools_test.go",
"bazel_hermetic_cc_test.go",
"bazel_invocations_test.go",
"bazel_policy_test.go",
"bazel_rbe_prewarm_test.go",
"bazel_rrc_test.go",
"ci_actions_policy_test.go",
"ci_analytics_workflow_test.go",
"ci_blacksmith_runner_test.go",
"ci_f7b_review_test.go",
"ci_f7c_advisory_test.go",
"ci_merge_queue_test.go",
"ci_workflow_apt_get_timeout_test.go",
"ci_workflow_core_test.go",
"ci_workflow_test.go",
"embedded_shard_count_test.go",
"host_tools_test.go",
"makefile_bazel_primary_test.go",
"migration_corpus_bazel_test.go",
"nogo_integration_step_script_test.go",
"nogo_lint_policy_test.go",
"pinned_shards_helpers_test.go",
"pinned_shards_test.go",
"pr_lanes_bazel_coverage_test.go",
"pr_risk_bazel_coverage_test.go",
"rbe_worker_env_test.go",
"repo_helpers_test.go",
"worker_env_autosync_test.go",
], # keep
data = [
"//:repo_other_files",
"//internal/testutil:testdoltcommon.go",
"@go_sdk//:bin/go",
"@go_sdk//:files",
],
# The Go SDK is go.mod's toolchain, whatever go the executor has on PATH.
# No //:repo_go_srcs here (F5 S2, cost fix, independent review of #7486):
# T4 (TestNogoIntegrationStepCoversItsConfiguration) and the
# unvalidated_integration_tests.txt exactness check moved to their own
# :nogo_integration_scan_test target below, which alone depends on
# //:repo_go_srcs and //:repo_go_test_srcs, so a Go-only change no longer
# re-runs the rest of this suite too.
env = {"BEADS_TEST_GO": "$(rlocationpath @go_sdk//:bin/go)"},
deps = [
"//internal/doltversion",
"//internal/formula",
"//internal/testutil/bazeltest",
"//scripts/internal/testbash",
"@in_gopkg_yaml_v3//:yaml_v3",
],
)
# T4 (TestNogoIntegrationStepCoversItsConfiguration) and T6
# (TestUnvalidatedIntegrationTestsFileIsExact, independent review of #7486
# must-fix 6): both source-scan every tracked .go file (test and non-test)
# for the "integration" build tag, so they alone depend on //:repo_go_srcs
# and //:repo_go_test_srcs. Carved out of scripts_test (cost fix, same
# review): with this data in scripts_test, any Go-only change re-ran the
# whole nogo-policy suite, raising scripts_test's rerun rate from 73% to 91%
# of main commits. bazel_invocations_test.go and ci_workflow_core_test.go are
# helper-only files (no Test functions) shared with scripts_test, so neither
# copy duplicates the other's parsing logic.
go_test(
name = "nogo_integration_scan_test",
srcs = [
"bazel_invocations_test.go",
"ci_workflow_core_test.go",
"nogo_integration_scan_test.go",
"repo_helpers_test.go",
], # keep
data = [
"//:repo_go_srcs",
"//:repo_go_test_srcs",
"//:repo_other_files",
],
deps = [
"//internal/testutil/bazeltest",
"@in_gopkg_yaml_v3//:yaml_v3",
],
)
# The shell scripts' own behavior, run against fixtures: this package's
# scripts and their testdata, the few root files they read, and the release
# formula.
go_test(
name = "shell_scripts_test",
srcs = [
"check_build_tags_test.go",
"check_doc_freshness_test.go",
"check_release_cross_compile_test.go",
"check_testing_short_test.go",
"check_winget_portable_alias_test.go",
"ci_analytics_extract_test.go",
"complexity_script_test.go",
"conformance_script_test.go",
"host_tools_test.go",
"install_dolt_test.go",
"pr_preflight_test.go",
"pre_push_suite_test.go",
"pull_dolt_image_test.go",
"release_script_test.go",
"repo_helpers_test.go",
"required_suite_test.go",
"test_script_test.go",
"update_nix_vendorhash_test.go",
"update_nix_vendorhash_unix_test.go",
"upgrade_smoke_loop_test.go",
],
data = [
":repo_other_files",
"//:.buildflags",
"//:.githooks/pre-push",
"//:.test-skip",
"//internal/httpclient:repo_other_files",
"//test/conformance:repo_go_test_srcs",
"//test/conformance:repo_other_files",
"//tools/bazel:repo_other_files",
"@beads_formulas//:beads-release.formula.toml",
],
# The release formula lives in .beads/ (in .bazelignore: it also holds
# live bd data), so it comes in through its own local repository.
env = {"BEADS_TEST_RELEASE_FORMULA": "$(rlocationpath @beads_formulas//:beads-release.formula.toml)"},
deps = [
"//internal/formula",
"//internal/testutil/bazeltest",
"//scripts/internal/testbash",
],
)
# Scans of the Go test files: the Bazel-skip allowlist, the managed local
# lane, and the shard manifests and scripts against the tests they shard.
# Every _test.go file (//:repo_go_test_srcs) but no non-test Go source, so a
# non-test Go change leaves it cached.
go_test(
name = "go_test_sources_test",
srcs = [
"bazel_skip_allowlist_test.go",
"embedded_shard_count_test.go",
"host_tools_test.go",
"managed_local_lane_test.go",
"pinned_shards_helpers_test.go",
"pinned_shards_sources_test.go",
"proxied_lane_coverage_test.go",
"repo_helpers_test.go",
"scripts_targets_test.go",
"shard_manifest_test.go",
],
data = [
"BUILD.bazel",
"ci/_embedded_shard_manifest_lib.py",
"ci/embedded_cmd_test_durations.json",
"ci/embedded_storage_test_durations.json",
"ci/gen_embedded_cmd_shard_manifest.py",
"ci/gen_embedded_storage_shard_manifest.py",
"ci/gen_proxied_shard_manifest.py",
"ci/proxied_test_durations.json",
"//:.github/scripts/embedded-cmd-test-shards.txt",
"//:.github/scripts/embedded-storage-test-shard.sh",
"//:.github/scripts/embedded-storage-test-shards.txt",
"//:.github/scripts/embedded-test-shard.sh",
"//:.github/scripts/proxied-cmd-test-shards.txt",
"//:.github/scripts/proxied-test-shard.sh",
"//:.github/scripts/server-storage-test-shard.sh",
"//:.github/scripts/server-storage-test-shards.txt",
"//:nested_go_modules",
"//:repo_go_test_srcs",
"//backend/http:repo_other_files",
"//backend/http/corpus:repo_other_files",
"//cmd/bd:repo_other_files",
"//internal/httpclient:repo_other_files",
"//internal/storage/dolt:repo_other_files",
"//internal/storage/embeddeddolt:repo_other_files",
"//tests/regression:repo_other_files",
"//tools/bazel:check_shard_coverage.py",
"//tools/bazel:equivalence_allowlist.txt",
],
deps = ["//internal/testutil/bazeltest"],
)
# check-testing-short.sh over every Go file, test and non-test, plus the
# Go-exec half of the host-go-under-Bazel policy (host_tool_exec_policy_test.go
# scans //:repo_go_srcs and //:repo_go_test_srcs for a literal go/gofmt exec).
# The shell-script half (host_tool_shell_policy_test.go) is its own
# :shell_host_tool_policy_test below: it reads a fixed, narrow list of shell
# scripts, not every Go source file, so it would otherwise pay this target's
# full re-run cost for files it never looks at.
go_test(
name = "go_sources_test",
srcs = [
"host_tool_exec_policy_test.go",
"repo_helpers_test.go",
"testing_short_tree_test.go",
],
data = [
"check-testing-short.sh",
"//:repo_go_srcs",
"//:repo_go_test_srcs",
],
deps = ["//internal/testutil/bazeltest"],
)
# The shell-script half of the host-go-under-Bazel policy
# (host_tool_shell_policy_test.go: see its package comment for shellPolicyScope
# and the detection tradeoffs). Split out of go_sources_test (which otherwise
# re-ran on every Go-source change to serve only this file) with data limited
# to exactly the shell scripts shellPolicyScope names -- no BUILD.bazel/.bzl
# walk, and so no //:repo_other_files: TestShellPolicyScopeCoversBazelShellData
# (dropped; see that test's old doc comment in version control) could not
# parse a `//pkg:file.sh` label or a glob() either, so paying 758 extra input
# files and ~4 points of cache-rerun rate across the whole repo for a walk
# that demonstrably still missed real in-scope scripts was not worth it.
go_test(
name = "shell_host_tool_policy_test",
srcs = [
"host_tool_shell_policy_test.go",
"repo_helpers_test.go",
],
data = [
"check-build-tags.sh",
"check-doc-freshness.sh",
"check-go-install-guidance.sh",
"check-migration-hygiene.sh",
"check-testing-short.sh",
"check-versions.sh",
"check-winget-portable-alias.sh",
"check-workapi-frontend-boundary.sh",
"ci/fmt-check.sh",
"ci/gofmt-bin.sh",
"ci/pr-lint.sh",
"conformance.sh",
"migrate-legacy-to-current.sh",
"migration-test/historical-dolt-upgrade-test.sh",
"migration-test/legacy-bridge-test.sh",
"migration-test/lib/binary.sh",
"release.sh",
"test.sh",
"upgrade-smoke-test.sh",
"//:.github/scripts/embedded-storage-test-shard.sh",
"//:.github/scripts/embedded-test-shard.sh",
"//:.github/scripts/proxied-test-shard.sh",
"//:.github/scripts/server-storage-test-shard.sh",
"//scripts/repochecks:run_check.sh",
"//scripts/repochecks:types_gen_drift_test.sh",
],
deps = ["//internal/testutil/bazeltest"],
)
# Scans of every tracked file (//:repo_files): any change re-runs them, so
# keep them few and fast.
go_test(
name = "tracked_files_test",
srcs = [
"repo_helpers_test.go",
"tracked_files_test.go",
],
data = ["//:repo_files"],
deps = ["//internal/testutil/bazeltest"],
)
# 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. shell_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__"],
)
# The historical-upgrade harness (scripts/migration-test) and the public
# SQLite bridge it drives, for //tests/migration's per-release targets.
filegroup(
name = "migration_harness",
srcs = [
"migrate-legacy-to-current.sh",
"migration-test/historical-dolt-upgrade-test.sh",
"migration-test/legacy-bridge-test.sh",
"migration-test/lib/binary.sh",
"migration-test/lib/versions.sh",
],
visibility = ["//tests/migration:__pkg__"],
)
exports_files(
["upgrade-smoke-test.sh"],
visibility = ["//tests/upgrade_smoke:__pkg__"],
)
exports_files(
[
"ci/fmt-check.sh",
"ci/lib/timing.sh",
"ci/pr-lint.sh",
],
visibility = ["//scripts/prlintmake:__pkg__"],
)
exports_files(
["ci/gofmt-bin.sh"],
visibility = ["//scripts/prlintmake:__pkg__"],
)
# The compatibility entrypoint whose exit-status contract
# //scripts/check-versions:check-versions_test drives.
exports_files(
["check-versions.sh"],
visibility = ["//scripts/check-versions:__pkg__"],
)
# Scripts the docs and agent instructions link to by path, which
# //test/docsync checks resolve.
exports_files(
[
"bump-version.sh",
"check-cli-docs-drift.sh",
"generate-cli-docs.sh",
"test.sh",
],
visibility = ["//test/docsync:__pkg__"],
)
# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) ---
filegroup(
name = "repo_doc_files",
srcs = glob(
[
"**/*.md",
"**/*.mdx",
],
allow_empty = True,
exclude = ["**/node_modules/**"],
),
visibility = ["//:__subpackages__"],
)
filegroup(
name = "repo_go_srcs",
srcs = glob(
["**/*.go"],
allow_empty = True,
exclude = [
"**/*_test.go",
"**/node_modules/**",
],
),
visibility = ["//:__subpackages__"],
)
filegroup(
name = "repo_go_test_srcs",
srcs = glob(
["**/*_test.go"],
allow_empty = True,
exclude = ["**/node_modules/**"],
),
visibility = ["//:__subpackages__"],
)
filegroup(
name = "repo_other_files",
srcs = glob(
["**"],
exclude = [
"**/*.db",
"**/*.exe",
"**/*.go",
"**/*.md",
"**/*.mdx",
"**/*.out",
"**/*.prof",
"**/*.pyc",
"**/*.test",
"**/__pycache__/**",
"**/node_modules/**",
],
),
visibility = ["//:__subpackages__"],
)
# --- end repo_files ---