fix(preflight): share checkout lint authority (#5780)
* fix(preflight): share checkout lint authority
Move the Beads repository's native and Windows non-CGO lint policy into a checkout-owned Go driver. The supported Make/Bash wrapper and bd preflight now execute that same authority, while generic Go repositories retain their direct lint contract and formatting is rooted at the detected project.
Fixes #5300.
Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell
* docs(lint): clarify shared driver scope and timeout limits
Address PR #5780 review 5191123668. Document existing inherited-tag semantics and distinguish the wrapper/preflight driver from standalone workflow and pre-commit callers.
Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell
* fix(lint): prefer the selected Windows Go toolchain (#6865)
Keep per-pass toolchain requests and deadlines while avoiding the extra auto-selection launcher under the linter. Nonstandard SDKs retain their original PATH.
Agent-Signature: codex-tui-gpt-6-astra-max on behalf of Ewen Cuthiell
* fix(lint): retain PATH for SDK directories containing separators (#6865)
Agent-Signature: codex-tui-gpt-6-astra-max on behalf of Ewen Cuthiell
* test(preflight): declare lint fixture executable for Bazel (#5300)
Agent-Signature: codex-tui-gpt-6-astra-max on behalf of Ewen Cuthiell
* fix(bazel): keep cmd/bd/testdata out of gazelle's package scan
PR #5780 adds the compilable fixture cmd/bd/testdata/preflight-go.go. Left to
itself gazelle generates a BUILD.bazel for cmd/bd/testdata, which makes it its
own package and then (a) empties cmd/bd's data = glob(["testdata/**"]) under the
default allow_empty = False and (b) puts testdata/preflight-go.go across a
package boundary from the preflight_go_fixture target that lists it.
bazel.yml's "BUILD files in sync" step runs `make bazel-sync` and fails on any
resulting drift, and fork PRs always run that job, so this was a live red whose
prescribed remedy ("commit the result") would have broken the build further.
Exclude the directory instead, and take gazelle's ordering for the data list.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* fix(preflight): bound the lint deadline and report each check's scope
Review iteration 1 for PR #5780. Synthesis and scorecard both approve (975/1000,
no blocker or major), so this addresses non-gating findings only.
Finding 4 (Resource Lifecycle) - the 13-minute deadline could not bound the call
it guards. CombinedOutput collects through a pipe every descendant inherits, so
Wait kept blocking on a surviving golangci-lint after the context killed the
direct `go run` child. Measured on the same process shape (direct child killed at
1s, descendant holding the pipe for 8s): without WaitDelay the call returned in
8.00s, with WaitDelay in 1.50s. Sets cmd.WaitDelay so the deadline is a real
bound and the "lint check exceeded" line can actually print.
Also gates that line on err != nil, which retires the claude lane's nit (a): a
child that exits cleanly as the deadline fires is a pass, not a timeout. Measured
at the crossover, 68 of 300 passing runs would have carried a false "exceeded"
line; 0 after the gate.
Finding 1 (Change Impact) and nit 7 (Debuggability), one remedy - the PR re-roots
lint and fmt at preflightProjectRoot() but leaves runTestCheck in the caller's
cwd, so a --check run from a subdirectory mixes a repo-wide verdict with a
subtree-only one "with nothing in the output distinguishing them", and the
reported command is only reproducible from the root. CheckResult now carries the
directory each check resolved to, and the report prints it.
Took the synthesis's second arm (state the scope split) rather than re-rooting
runTestCheck, because re-rooting is a behaviour change neither lane priced: it
widens a subdirectory user's test run from 5 packages to 128, including every
Dolt-dependent package, on a repo whose full test gate the PR body already
discloses as red. The stated hazard is that the scopes are indistinguishable,
which reporting the directory discharges directly.
Finding 2 (Architectural Consistency), second clause - adds
TestBuildFlagsContractMatchesShellSource, which parses .buildflags and pins the
driver constants to it. The existing mirror test asserts today's values from the
Go side only, so the files could drift silently; buildEnvironment overrides
rather than merges -tags, so a tag added to .buildflags alone would be dropped
with every test still green. Each of the three legs was mutation-proved to red.
Declined, with the measurements that decided it:
- Finding 2's first clause (route the key comparisons through
internal/execenv.KeyEqualForOS). The two are provably equivalent today -
execenv's keyIdentityForWindows is strings.ToLower for the same documented
reason - so this is de-duplication, not a defect, and the hazard it closes
(os/exec key-identity semantics changing) is far less live than the
.buildflags drift the second clause pins. The faithful swap means threading
goos through five driver functions and ~27 test call sites in this PR's
centerpiece file, and it would put a non-stdlib dependency into pr-lint_lib,
which the binary currently does not have. Worth doing on its own footing, not
inside a non-gating cleanup.
- Nit 6 (parse GOFLAGS into fields instead of a substring test). .buildflags:22
guards its own append with `!= *gms_pure_go*`, a substring test, and the Go
code is a deliberate mirror of it. Making only the Go side stricter would
manufacture exactly the drift finding 2 warns about: on
GOFLAGS=-tags=gms_pure_go_x the shell would skip its append while the driver
performed one. The fix belongs to .buildflags and the driver together, and
.buildflags is outside this PR. The new mirror test now pins that agreement.
- Nit 5 (probe golangci-lint as well as `go`). The claude lane examined the
LookPath target change and recorded it as deliberate, with the driver's own
missing-linter error as the mitigation. The suggested alternative - have the
driver emit preflight's `--skip-lint` hint - puts a bd-specific flag into a
driver that CI also invokes, where that advice is wrong.
- Finding 3 (two false PR-body claims). Correcting the body is outward-facing
and belongs to finalize; stamped for it. Swept the in-tree copies: the PR's own
LINTING.md sentence "Toolchain requests, formatting, and lint deadlines are
unchanged" is scoped to the Windows SDK-preference paragraph it sits in and is
true there, so it is graded rather than repaired. CHANGELOG.md contains no
pr-lint entry, so the remedy's CHANGELOG half has no target in this PR.
Validation: go build, go vet and gofmt clean on the changed packages;
./scripts/pr-lint/ 12 pass / 2 Windows-only skip; ./scripts/prlintmake/ pass;
cmd/bd preflight selector pass; scripts/check-doc-freshness.sh PASSED;
make bazel-sync idempotent and bazel test //scripts/pr-lint:pr-lint_test PASSED.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* test(prlintmake): inject the pr-lint wrapper's gofmt shim through GOFMT
The merge with #6294 (be-gx8) left runPRLintWrapper relying on a
code path #6294 made deliberately loud. The helper put its gofmt shim
on PATH. Since #6294, fmt-check.sh resolves gofmt through
scripts/ci/gofmt-bin.sh, which ignores a PATH gofmt by design.
Both TestPRLintWrapper* tests still passed, but only via the
resolver's fallback. The go shim answers `go env GOVERSION` and
`go env GOROOT` with nothing, so gofmt-bin.sh warns "could not resolve
a gofmt from go.mod's go1.26.7 toolchain" and falls back to whatever
gofmt is first on PATH, which happened to be the shim. With GOFMT set
in the environment, which is #6294's documented override, the
resolver uses that value instead. Both tests then fail: exit status
127 from fmt-check.sh, and a missing aggregate failure diagnostic
because pr-lint.sh stops before `go run`. At the pre-merge head
(7f37ba5165) the same ambient GOFMT=/nonexistent run passed all 5
tests.
Pass the shim through GOFMT, as main's runFmtCheck does, and keep it
off PATH so only the go shim is found there. gofmt-bin.sh now returns
the override immediately, and the go shim sees only `version <shim>`
and `run ... ./scripts/pr-lint`. Both wrapper assertions, on the
driver arguments and on the failure diagnostic, are unchanged.
go test ./scripts/prlintmake/: 9/9 pass by default and 9/9 pass with
GOFMT=/nonexistent in the environment. Before this change the second
run was 7/9.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
2704d8a954
commit
6899813df6
18 changed files with 1466 additions and 107 deletions
|
|
@ -12,6 +12,14 @@ load("@rules_go//go:def.bzl", "go_library", "go_test")
|
|||
# gazelle:exclude integrations
|
||||
gazelle(name = "gazelle")
|
||||
|
||||
exports_files(
|
||||
[".buildflags"],
|
||||
visibility = [
|
||||
"//scripts/pr-lint:__pkg__",
|
||||
"//scripts/prlintmake:__pkg__",
|
||||
],
|
||||
)
|
||||
|
||||
exports_files([
|
||||
"go.mod",
|
||||
"go.sum",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
load("@rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
|
||||
load("@rules_shell//shell:sh_test.bzl", "sh_test")
|
||||
|
||||
# testdata/ holds the compilable preflight fixture below. Left to itself gazelle
|
||||
# would make testdata/ its own package, which empties this package's
|
||||
# glob(["testdata/**"]) and puts the fixture source across a package boundary.
|
||||
# gazelle:exclude testdata
|
||||
|
||||
go_binary(
|
||||
name = "preflight_go_fixture",
|
||||
testonly = True,
|
||||
srcs = ["testdata/preflight-go.go"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "bd_lib",
|
||||
srcs = [
|
||||
|
|
@ -1185,6 +1196,7 @@ go_test(
|
|||
data = glob(["testdata/**"]) + [
|
||||
":bd_for_tests",
|
||||
":go_srcs",
|
||||
":preflight_go_fixture",
|
||||
"//:bd_test_files",
|
||||
"//cmd/bd/doctor:go_srcs",
|
||||
"//cmd/bd/doctor/fix:go_srcs",
|
||||
|
|
@ -1195,6 +1207,7 @@ go_test(
|
|||
env = {
|
||||
"BEADS_TEST_BD_BINARY": "$(rlocationpath :bd_for_tests)",
|
||||
"BEADS_TEST_GOFMT": "$(rlocationpath @go_sdk//:bin/gofmt)",
|
||||
"BEADS_TEST_PREFLIGHT_GO": "$(rlocationpath :preflight_go_fixture)",
|
||||
},
|
||||
shard_count = 16,
|
||||
deps = [
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
|
@ -26,6 +27,13 @@ type CheckResult struct {
|
|||
Warning bool `json:"warning,omitempty"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Command string `json:"command"`
|
||||
// Dir is the directory Command was resolved and run in. The --check probes
|
||||
// do not all share one scope: lint and format run at the repository root
|
||||
// while tests run in the caller's working directory, so a run started from
|
||||
// a subdirectory mixes a repo-wide verdict with a subtree-only one.
|
||||
// Reporting the directory keeps the two distinguishable and makes the
|
||||
// reported command reproducible from somewhere other than the root.
|
||||
Dir string `json:"dir,omitempty"`
|
||||
}
|
||||
|
||||
// PreflightResult represents the overall preflight check results.
|
||||
|
|
@ -60,6 +68,14 @@ Examples:
|
|||
RunE: runPreflight,
|
||||
}
|
||||
|
||||
const beadsPRLintDriverCommand = "go run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint"
|
||||
|
||||
// lintCancellationGrace bounds how long Wait keeps reading the output pipe
|
||||
// after the lint deadline fires. Descendants of the driver can hold the write
|
||||
// end open indefinitely, so this is what turns the deadline into a real bound
|
||||
// on the call rather than only on the direct child.
|
||||
const lintCancellationGrace = 10 * time.Second
|
||||
|
||||
func init() {
|
||||
preflightCmd.Flags().Bool("check", false, "Run checks automatically")
|
||||
preflightCmd.Flags().Bool("fix", false, "Auto-fix issues where possible (vendorHash, version sync)")
|
||||
|
|
@ -97,12 +113,7 @@ func runPreflight(cmd *cobra.Command, args []string) error {
|
|||
|
||||
// Static checklist mode — tailor the checklist to the detected project
|
||||
// stack so non-Go projects don't get a misleading Go/Nix checklist (GH#4364).
|
||||
root := git.GetRepoRoot()
|
||||
if root == "" {
|
||||
if wd, err := os.Getwd(); err == nil {
|
||||
root = wd
|
||||
}
|
||||
}
|
||||
root := preflightProjectRoot()
|
||||
|
||||
fmt.Println("PR Readiness Checklist:")
|
||||
fmt.Println()
|
||||
|
|
@ -136,7 +147,7 @@ func buildPreflightChecklist(dir string) []string {
|
|||
if isBeadsRepo(dir) {
|
||||
return []string{
|
||||
"Tests pass: go test -tags gms_pure_go -short ./...",
|
||||
"Lint passes: golangci-lint run --build-tags=gms_pure_go ./...",
|
||||
"Lint passes: make ci-pr-lint",
|
||||
"Formatting: gofmt -l .",
|
||||
"No beads pollution: check .beads/issues.jsonl diff",
|
||||
"Nix hash current: go.sum unchanged or vendorHash updated",
|
||||
|
|
@ -281,6 +292,9 @@ func runChecks(jsonOutput, skipLint bool) error {
|
|||
fmt.Printf("✗ %s\n", r.Name)
|
||||
}
|
||||
fmt.Printf(" Command: %s\n", r.Command)
|
||||
if r.Dir != "" {
|
||||
fmt.Printf(" Directory: %s\n", r.Dir)
|
||||
}
|
||||
if r.Skipped && r.Output != "" {
|
||||
fmt.Printf(" Reason: %s\n", r.Output)
|
||||
} else if r.Warning && r.Output != "" {
|
||||
|
|
@ -302,23 +316,45 @@ func runChecks(jsonOutput, skipLint bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// runTestCheck runs go test -short ./... and returns the result.
|
||||
// runTestCheck runs go test -short ./... and returns the result. Unlike the
|
||||
// lint and format checks it is deliberately left in the caller's working
|
||||
// directory, so ./... stays the subtree the caller asked about; the reported
|
||||
// Dir is what keeps that narrower scope visible beside the rooted checks.
|
||||
func runTestCheck() CheckResult {
|
||||
command := "go test -tags gms_pure_go -short ./..."
|
||||
cmd := exec.Command("go", "test", "-tags", "gms_pure_go", "-short", "./...")
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
dir, err2 := os.Getwd()
|
||||
if err2 != nil {
|
||||
dir = "."
|
||||
}
|
||||
|
||||
return CheckResult{
|
||||
Name: "Tests pass",
|
||||
Passed: err == nil,
|
||||
Output: string(output),
|
||||
Command: command,
|
||||
Dir: dir,
|
||||
}
|
||||
}
|
||||
|
||||
// runLintCheck runs golangci-lint and returns the result.
|
||||
type lintInvocation struct {
|
||||
display string
|
||||
executable string
|
||||
args []string
|
||||
dir string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
// runLintCheck runs the checkout-owned Beads lint driver in the Beads source
|
||||
// tree and retains a direct, generic golangci-lint check elsewhere.
|
||||
func runLintCheck(skipLint bool) CheckResult {
|
||||
command := "golangci-lint run --build-tags=gms_pure_go ./..."
|
||||
return runLintCheckAt(preflightProjectRoot(), skipLint)
|
||||
}
|
||||
|
||||
func runLintCheckAt(root string, skipLint bool) CheckResult {
|
||||
invocation := lintInvocationForRoot(root)
|
||||
if skipLint {
|
||||
return CheckResult{
|
||||
Name: "Lint passes",
|
||||
|
|
@ -326,33 +362,86 @@ func runLintCheck(skipLint bool) CheckResult {
|
|||
Skipped: true,
|
||||
Warning: true,
|
||||
Output: "lint check explicitly skipped by --skip-lint",
|
||||
Command: command,
|
||||
Command: invocation.display,
|
||||
Dir: invocation.dir,
|
||||
}
|
||||
}
|
||||
|
||||
// Check if golangci-lint is available
|
||||
if _, err := exec.LookPath("golangci-lint"); err != nil {
|
||||
if _, err := exec.LookPath(invocation.executable); err != nil {
|
||||
return CheckResult{
|
||||
Name: "Lint passes",
|
||||
Passed: false,
|
||||
Output: "golangci-lint not found in PATH (install it or rerun with --skip-lint)",
|
||||
Command: command,
|
||||
Output: fmt.Sprintf("%s not found in PATH (install it or rerun with --skip-lint)", invocation.executable),
|
||||
Command: invocation.display,
|
||||
Dir: invocation.dir,
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command("golangci-lint", "run", "--build-tags=gms_pure_go", "./...")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), invocation.timeout)
|
||||
defer cancel()
|
||||
// Cancellation targets the direct command. For the Beads go run invocation,
|
||||
// it does not provide process-tree ownership: descendants may outlive it.
|
||||
cmd := exec.CommandContext(ctx, invocation.executable, invocation.args...)
|
||||
cmd.Dir = invocation.dir
|
||||
// CombinedOutput collects through a pipe that every descendant inherits, so
|
||||
// killing the direct child on deadline does not close the write end while a
|
||||
// golangci-lint grandchild still holds it. Without a WaitDelay the deadline
|
||||
// therefore cannot bound this call at all, and the message appended below
|
||||
// never prints in the one case it exists to report.
|
||||
cmd.WaitDelay = lintCancellationGrace
|
||||
output, err := cmd.CombinedOutput()
|
||||
// Report the deadline only when it actually decided the result: a child that
|
||||
// wins the race exits cleanly just as the context expires, and that is a
|
||||
// pass, not a timeout.
|
||||
if err != nil && errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
output = append(output, []byte(fmt.Sprintf("\nlint check exceeded %s", invocation.timeout))...)
|
||||
}
|
||||
|
||||
return CheckResult{
|
||||
Name: "Lint passes",
|
||||
Passed: err == nil,
|
||||
Output: string(output),
|
||||
Command: command,
|
||||
Command: invocation.display,
|
||||
Dir: invocation.dir,
|
||||
}
|
||||
}
|
||||
|
||||
func lintInvocationForRoot(root string) lintInvocation {
|
||||
if isBeadsRepo(root) {
|
||||
return lintInvocation{
|
||||
display: beadsPRLintDriverCommand,
|
||||
executable: "go",
|
||||
args: []string{"run", "-mod=readonly", "-tags=gms_pure_go", "./scripts/pr-lint"},
|
||||
dir: root,
|
||||
timeout: 13 * time.Minute,
|
||||
}
|
||||
}
|
||||
return lintInvocation{
|
||||
display: "golangci-lint run ./...",
|
||||
executable: "golangci-lint",
|
||||
args: []string{"run", "./..."},
|
||||
dir: root,
|
||||
timeout: 6 * time.Minute,
|
||||
}
|
||||
}
|
||||
|
||||
func preflightProjectRoot() string {
|
||||
if root := git.GetRepoRoot(); root != "" {
|
||||
return root
|
||||
}
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "."
|
||||
}
|
||||
return wd
|
||||
}
|
||||
|
||||
// runFmtCheck runs gofmt -l and fails if any files need formatting.
|
||||
func runFmtCheck() CheckResult {
|
||||
return runFmtCheckAt(preflightProjectRoot())
|
||||
}
|
||||
|
||||
func runFmtCheckAt(root string) CheckResult {
|
||||
command := "gofmt -l ."
|
||||
|
||||
// Check if gofmt is available
|
||||
|
|
@ -362,10 +451,12 @@ func runFmtCheck() CheckResult {
|
|||
Passed: false,
|
||||
Output: "gofmt not found in PATH (install Go toolchain)",
|
||||
Command: command,
|
||||
Dir: root,
|
||||
}
|
||||
}
|
||||
|
||||
cmd := exec.Command("gofmt", "-l", ".")
|
||||
cmd.Dir = root
|
||||
output, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -374,6 +465,7 @@ func runFmtCheck() CheckResult {
|
|||
Passed: false,
|
||||
Output: string(output),
|
||||
Command: command,
|
||||
Dir: root,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -384,6 +476,7 @@ func runFmtCheck() CheckResult {
|
|||
Passed: false,
|
||||
Output: fmt.Sprintf("Unformatted files:\n%s\nRun: gofmt -w .", unformatted),
|
||||
Command: command,
|
||||
Dir: root,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -391,6 +484,7 @@ func runFmtCheck() CheckResult {
|
|||
Name: "Formatting",
|
||||
Passed: true,
|
||||
Command: command,
|
||||
Dir: root,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
|
|
@ -224,58 +227,179 @@ func TestRunLintCheck_SkipLintFlag(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// gofmtForTest returns the gofmt to run: the one on PATH under go test, and
|
||||
// under Bazel (no host toolchain) the Go SDK's, declared as data and named by
|
||||
// BEADS_TEST_GOFMT.
|
||||
func gofmtForTest(t *testing.T) string {
|
||||
func TestLintInvocationForRootMatchesChecklistAndProjectType(t *testing.T) {
|
||||
beads := writeMarkerDir(t, map[string]string{"go.mod": "module github.com/steveyegge/beads\n"})
|
||||
beadsInvocation := lintInvocationForRoot(beads)
|
||||
if beadsInvocation.display != beadsPRLintDriverCommand {
|
||||
t.Fatalf("Beads command = %q, want %q", beadsInvocation.display, beadsPRLintDriverCommand)
|
||||
}
|
||||
if beadsInvocation.executable != "go" {
|
||||
t.Fatalf("Beads executable = %q, want go", beadsInvocation.executable)
|
||||
}
|
||||
wantBeadsArgs := []string{"run", "-mod=readonly", "-tags=gms_pure_go", "./scripts/pr-lint"}
|
||||
if !reflect.DeepEqual(beadsInvocation.args, wantBeadsArgs) {
|
||||
t.Fatalf("Beads args = %#v, want %#v", beadsInvocation.args, wantBeadsArgs)
|
||||
}
|
||||
if beadsInvocation.dir != beads {
|
||||
t.Fatalf("Beads command dir = %q, want %q", beadsInvocation.dir, beads)
|
||||
}
|
||||
if checklist := strings.Join(buildPreflightChecklist(beads), "\n"); !strings.Contains(checklist, "make ci-pr-lint") {
|
||||
t.Fatalf("Beads checklist does not report supported lint entrypoint:\n%s", checklist)
|
||||
}
|
||||
|
||||
generic := writeMarkerDir(t, map[string]string{"go.mod": "module example.com/generic\n"})
|
||||
genericInvocation := lintInvocationForRoot(generic)
|
||||
if genericInvocation.display != "golangci-lint run ./..." {
|
||||
t.Fatalf("generic command = %q, want direct lint contract", genericInvocation.display)
|
||||
}
|
||||
if genericInvocation.executable != "golangci-lint" {
|
||||
t.Fatalf("generic executable = %q, want golangci-lint", genericInvocation.executable)
|
||||
}
|
||||
if want := []string{"run", "./..."}; !reflect.DeepEqual(genericInvocation.args, want) {
|
||||
t.Fatalf("generic args = %#v, want %#v", genericInvocation.args, want)
|
||||
}
|
||||
if checklist := strings.Join(buildPreflightChecklist(generic), "\n"); !strings.Contains(checklist, genericInvocation.display) {
|
||||
t.Fatalf("generic checklist does not report executable lint command %q:\n%s", genericInvocation.display, checklist)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunLintCheckAtBeadsExecutesCheckoutDriverAndReportsJSONCommand(t *testing.T) {
|
||||
helperDir := t.TempDir()
|
||||
helperName := "go"
|
||||
if runtime.GOOS == "windows" {
|
||||
helperName += ".exe"
|
||||
}
|
||||
helperPath := filepath.Join(helperDir, helperName)
|
||||
if bazeltest.IsBazel() {
|
||||
fixture, err := bazeltest.RunfileEnv("BEADS_TEST_PREFLIGHT_GO")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := os.ReadFile(fixture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(helperPath, data, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
realGo, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
t.Fatalf("locate Go toolchain for subprocess fixture: %v", err)
|
||||
}
|
||||
build := exec.Command(realGo, "build", "-o", helperPath, "testdata/preflight-go.go")
|
||||
if output, err := build.CombinedOutput(); err != nil {
|
||||
t.Fatalf("build fake Go executable: %v\n%s", err, output)
|
||||
}
|
||||
}
|
||||
|
||||
beads := writeMarkerDir(t, map[string]string{"go.mod": "module github.com/steveyegge/beads\n"})
|
||||
marker := filepath.Join(t.TempDir(), "invocation.json")
|
||||
t.Setenv("PATH", helperDir)
|
||||
t.Setenv("PREFLIGHT_LINT_MARKER", marker)
|
||||
result := runLintCheckAt(beads, false)
|
||||
if !result.Passed {
|
||||
t.Fatalf("checkout lint invocation failed: %s", result.Output)
|
||||
}
|
||||
if result.Command != beadsPRLintDriverCommand {
|
||||
t.Fatalf("reported command = %q, want %q", result.Command, beadsPRLintDriverCommand)
|
||||
}
|
||||
if !strings.Contains(result.Output, "synthetic checkout lint success") {
|
||||
t.Fatalf("missing subprocess output: %q", result.Output)
|
||||
}
|
||||
|
||||
var invocation struct {
|
||||
Args []string `json:"args"`
|
||||
Dir string `json:"dir"`
|
||||
}
|
||||
markerData, err := os.ReadFile(marker)
|
||||
if err != nil {
|
||||
t.Fatalf("read invocation marker: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(markerData, &invocation); err != nil {
|
||||
t.Fatalf("decode invocation marker: %v", err)
|
||||
}
|
||||
wantArgs := []string{"run", "-mod=readonly", "-tags=gms_pure_go", "./scripts/pr-lint"}
|
||||
if !reflect.DeepEqual(invocation.Args, wantArgs) {
|
||||
t.Fatalf("subprocess args = %#v, want %#v", invocation.Args, wantArgs)
|
||||
}
|
||||
if invocation.Dir != beads {
|
||||
t.Fatalf("subprocess dir = %q, want checkout root %q", invocation.Dir, beads)
|
||||
}
|
||||
|
||||
encoded, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal CheckResult: %v", err)
|
||||
}
|
||||
var reported CheckResult
|
||||
if err := json.Unmarshal(encoded, &reported); err != nil {
|
||||
t.Fatalf("unmarshal CheckResult: %v", err)
|
||||
}
|
||||
if reported.Command != beadsPRLintDriverCommand || !reported.Passed {
|
||||
t.Fatalf("JSON evidence = %#v, want passing checkout driver command", reported)
|
||||
}
|
||||
}
|
||||
|
||||
// usePreflightGofmt preserves the SDK executable declared by the Bazel target
|
||||
// while exercising the production PATH lookup in runFmtCheckAt.
|
||||
func usePreflightGofmt(t *testing.T) {
|
||||
t.Helper()
|
||||
if !bazeltest.IsBazel() {
|
||||
return "gofmt"
|
||||
return
|
||||
}
|
||||
path, err := bazeltest.RunfileEnv("BEADS_TEST_GOFMT")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
t.Setenv("PATH", filepath.Dir(path)+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
}
|
||||
|
||||
func TestRunFmtCheck_Formatted(t *testing.T) {
|
||||
func TestRunFmtCheckAtFormattedRoot(t *testing.T) {
|
||||
usePreflightGofmt(t)
|
||||
dir := t.TempDir()
|
||||
// Write a properly formatted Go file
|
||||
err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nfunc main() {}\n"), 0644)
|
||||
if err != nil {
|
||||
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nfunc main() {}\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Run gofmt -l in the temp dir
|
||||
cmd := exec.Command(gofmtForTest(t), "-l", ".")
|
||||
cmd.Dir = dir
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("gofmt failed: %v: %s", err, output)
|
||||
}
|
||||
if strings.TrimSpace(string(output)) != "" {
|
||||
t.Fatalf("expected no unformatted files, got: %s", output)
|
||||
result := runFmtCheckAt(dir)
|
||||
if !result.Passed {
|
||||
t.Fatalf("formatted root failed: %s", result.Output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunFmtCheck_Unformatted(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// Write a poorly formatted Go file (extra spaces, no newline)
|
||||
err := os.WriteFile(filepath.Join(dir, "bad.go"), []byte("package main\nfunc main( ) { }\n"), 0644)
|
||||
if err != nil {
|
||||
func TestRunFmtCheckAtFindsUnformattedFileOutsideCallerSubtree(t *testing.T) {
|
||||
usePreflightGofmt(t)
|
||||
root := t.TempDir()
|
||||
callerDir := filepath.Join(root, "nested", "caller")
|
||||
outsideCaller := filepath.Join(root, "sibling", "bad.go")
|
||||
if err := os.MkdirAll(callerDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(outsideCaller), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(outsideCaller, []byte("package sibling\nfunc bad( ) { }\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(gofmtForTest(t), "-l", ".")
|
||||
cmd.Dir = dir
|
||||
output, _ := cmd.CombinedOutput()
|
||||
unformatted := strings.TrimSpace(string(output))
|
||||
if unformatted == "" {
|
||||
t.Fatal("expected unformatted files to be listed")
|
||||
originalDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(unformatted, "bad.go") {
|
||||
t.Fatalf("expected bad.go in output, got: %s", unformatted)
|
||||
if err := os.Chdir(callerDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if err := os.Chdir(originalDir); err != nil {
|
||||
t.Errorf("restore working directory: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
result := runFmtCheckAt(root)
|
||||
if result.Passed {
|
||||
t.Fatal("root formatting check passed despite unformatted sibling file")
|
||||
}
|
||||
if !strings.Contains(result.Output, "bad.go") {
|
||||
t.Fatalf("root formatting check missed sibling file: %s", result.Output)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
23
cmd/bd/testdata/preflight-go.go
vendored
Normal file
23
cmd/bd/testdata/preflight-go.go
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
marker, err := os.Create(os.Getenv("PREFLIGHT_LINT_MARKER"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer marker.Close()
|
||||
if err := json.NewEncoder(marker).Encode(map[string]any{"args": os.Args[1:], "dir": dir}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("synthetic checkout lint success")
|
||||
}
|
||||
|
|
@ -3,8 +3,8 @@
|
|||
Last reviewed: 2026-09-03
|
||||
|
||||
Freshness source: `engdocs/CI_TEST_SURFACE_AUDIT.md`, `.github/workflows/*.yml`,
|
||||
`.buildflags`, `.golangci.yml`, `scripts/ci/pr-lint.sh`, `Makefile`, package test
|
||||
manifests, and maintainer decision review.
|
||||
`.buildflags`, `.golangci.yml`, `scripts/ci/pr-lint.sh`, `scripts/pr-lint/`,
|
||||
`Makefile`, package test manifests, and maintainer decision review.
|
||||
|
||||
This document records the agreed target shape for CI cleanup. It is the policy
|
||||
and roadmap layer; the current inventory remains in
|
||||
|
|
@ -51,8 +51,11 @@ Every PR, including docs-only PRs, should run the required Linux baseline:
|
|||
|
||||
## Wrapper Conventions
|
||||
|
||||
Shell scripts under `scripts/ci/` are the source of truth. Make targets should
|
||||
be aliases for discoverability, not a second implementation of command policy.
|
||||
Repository-owned wrappers under `scripts/ci/` are the supported tier
|
||||
entrypoints. Make targets should be aliases for discoverability, not a second
|
||||
implementation of command policy. A wrapper may delegate shared policy to one
|
||||
repository-owned Go driver when production tooling also needs the same
|
||||
shell-free contract.
|
||||
|
||||
Wrapper rules:
|
||||
|
||||
|
|
@ -171,6 +174,10 @@ easy to identify and rerun. It includes:
|
|||
cross-lints are compile-only passes on the Linux runner; they add no macOS or
|
||||
Windows runner and do not change the platform column above.
|
||||
|
||||
All three lint passes and their target selection are owned by the
|
||||
checkout-local `scripts/pr-lint` Go driver. The Bash wrapper retains formatting,
|
||||
direct invocation, and aggregate timing without duplicating that policy.
|
||||
|
||||
All three passes are SCOPED BY LANE. On a PR the wrapper takes
|
||||
`BD_LINT_NEW_FROM_MERGE_BASE` and reports only the findings that PR introduces,
|
||||
matching the `only-new-issues` setting on the sibling `lint` job; on a push to
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ freshness source.
|
|||
| `reference/json-schema.md` | `Last reviewed:` marker tied to `cmd/bd/output.go`, `cmd/bd/errors.go`, and protocol tests. (Mintlify port: was `JSON_SCHEMA.md`.) |
|
||||
| `recovery/init-safety.md` | `Last reviewed:` marker tied to `cmd/bd/init*.go` safety code and tests. (Mintlify port: was `RECOVERY.md`.) |
|
||||
| `ERROR_HANDLING.md` | `Last reviewed:` marker tied to current command error exits and JSON error helpers. |
|
||||
| `LINTING.md` | `Last reviewed:` marker tied to `.golangci.yml`, `scripts/ci/pr-lint.sh`, the `Makefile` wrapper, and CI workflow wiring. |
|
||||
| `LINTING.md` | `Last reviewed:` marker tied to `.golangci.yml`, `scripts/ci/pr-lint.sh`, `scripts/pr-lint/`, the `Makefile` wrapper, and CI workflow wiring. |
|
||||
| `SERVE_RUNBOOK.md` | `Last reviewed:` marker tied to the operating-envelope constants in `internal/httpapi/server.go` and the log fields in its `event`/`request` emitters. |
|
||||
| `CI_CLEANUP_PLAN.md` | `Last reviewed:` marker tied to the CI audit, workflow files, wrapper scripts, `Makefile`, package manifests, and maintainer decision review. |
|
||||
| `design/otel/otel-data-model.md` | `Last reviewed:` marker tied to telemetry, Dolt storage, hooks, and AI call sites. |
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
# Linting Policy
|
||||
|
||||
Last reviewed: 2026-09-03
|
||||
Last reviewed: 2026-09-13
|
||||
|
||||
Freshness source: `.golangci.yml`, `scripts/ci/pr-lint.sh`, `Makefile`,
|
||||
`.github/workflows/pr.yml`, and `.github/workflows/main.yml`.
|
||||
Freshness source: `.golangci.yml`, `scripts/ci/pr-lint.sh`, `scripts/pr-lint/`,
|
||||
`Makefile`, `.github/workflows/pr.yml`, and `.github/workflows/main.yml`.
|
||||
|
||||
This document explains the required Go lint gate for this codebase.
|
||||
|
||||
|
|
@ -37,14 +37,37 @@ answered.
|
|||
|
||||
The wrapper runs:
|
||||
|
||||
- `make fmt-check`;
|
||||
- golangci-lint with `.golangci.yml`, readonly module downloads, a five-minute
|
||||
timeout, the `gms_pure_go` build tag, and `--new-from-merge-base` when
|
||||
`BD_LINT_NEW_FROM_MERGE_BASE` names a ref; and
|
||||
- a second non-CGO Windows cross-lint pass, on the same scope, when the native
|
||||
host does not already cover that target; and
|
||||
- a third non-CGO darwin (arm64) cross-lint pass, on the same scope, under the
|
||||
same rule, so `//go:build darwin` files are linted from the Linux runner.
|
||||
- the shared `scripts/ci/fmt-check.sh` formatting check; and
|
||||
- the checkout-owned `scripts/pr-lint` Go driver, which runs golangci-lint with
|
||||
`.golangci.yml`, readonly module downloads, a five-minute timeout, the
|
||||
`gms_pure_go` build tag, and `--new-from-merge-base` when
|
||||
`BD_LINT_NEW_FROM_MERGE_BASE` names a ref, then runs the same scope for the
|
||||
non-CGO Windows target and the non-CGO darwin (arm64) target when the native
|
||||
host does not already cover them, so `//go:build windows && !cgo` and
|
||||
`//go:build darwin` files are linted from the Linux runner.
|
||||
|
||||
The shell wrapper records one aggregate timing for the Go lint driver. The
|
||||
driver prints a heading and result for each native or cross-target pass so
|
||||
failures remain attributable without duplicating target-selection policy in
|
||||
Bash.
|
||||
|
||||
The shared driver owns the lint passes used by `scripts/ci/pr-lint.sh` and
|
||||
Beads-source `bd preflight`. The standalone workflow lint action and pre-commit
|
||||
hook do not call it. The hook uses changed-lines scope, adds `--fix`, and omits
|
||||
the Windows cross-lint pass. Workflow consolidation remains tracked in #5629.
|
||||
|
||||
The driver matches `.buildflags` when preparing `GOFLAGS`: appending
|
||||
`-tags=gms_pure_go` can override an inherited bare-Go tags value; it does not
|
||||
merge tag lists. Each lint process has a six-minute context and a five-minute
|
||||
golangci-lint timeout. Cancelling preflight's outer `go run` command does not
|
||||
guarantee immediate descendant cleanup.
|
||||
|
||||
On Windows, the driver checks each pass's selected Go SDK and prefers its
|
||||
`bin` directory only in that linter child's PATH. This avoids an extra Go
|
||||
auto-selection process surviving cancellation. Discovery shares the existing
|
||||
30-second probe budget; custom SDKs that cannot be verified retain the original
|
||||
PATH. Toolchain requests, formatting, and lint deadlines are unchanged. This
|
||||
does not guarantee cleanup of compiler descendants or fix slow package loading.
|
||||
|
||||
## Policy
|
||||
|
||||
|
|
@ -66,7 +89,8 @@ test-fixture file reads, and documented security false positives.
|
|||
|
||||
`pr-lint` stays separate from `pr-policy` and `pr-core` so failures are easy to
|
||||
identify and rerun. Its repository-owned wrapper is
|
||||
`scripts/ci/pr-lint.sh`, exposed as `make ci-pr-lint`.
|
||||
`scripts/ci/pr-lint.sh`, exposed as `make ci-pr-lint`; its lint policy is owned
|
||||
by the shell-free `scripts/pr-lint` Go driver.
|
||||
|
||||
See [`CI_CLEANUP_PLAN.md`](CI_CLEANUP_PLAN.md) for the full CI tier policy.
|
||||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,11 @@ exports_files(
|
|||
)
|
||||
|
||||
exports_files(
|
||||
["ci/fmt-check.sh"],
|
||||
[
|
||||
"ci/fmt-check.sh",
|
||||
"ci/lib/timing.sh",
|
||||
"ci/pr-lint.sh",
|
||||
],
|
||||
visibility = ["//scripts/prlintmake:__pkg__"],
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,11 @@ Each wrapper auto-detects the repository root, sources `.buildflags` when it
|
|||
invokes Go in the default build mode, and records per-command timing through
|
||||
`scripts/ci/lib/timing.sh`.
|
||||
|
||||
`ci-pr-lint` keeps its supported Bash/Make entrypoint but delegates native and
|
||||
Windows/non-CGO target selection to the checkout-owned `scripts/pr-lint` Go
|
||||
driver. This is the same shell-free lint authority used by `bd preflight` in a
|
||||
Beads source checkout.
|
||||
|
||||
Broad Go test wrappers also source `scripts/ci/lib/test-env.sh`, which creates a
|
||||
temporary HOME/XDG/Dolt root, isolates Git global/system config, clears runtime
|
||||
Beads/Dolt environment variables, and sets `BEADS_TEST_SKIP=dolt` before tests
|
||||
|
|
|
|||
|
|
@ -26,8 +26,8 @@ DOCS=(
|
|||
"docs/recovery/init-safety.md|cmd/bd/init.go;cmd/bd/init_safety.go;cmd/bd/init_safety_test.go;cmd/bd/init_safety_help.go;cmd/bd/dolt.go"
|
||||
"engdocs/ERROR_HANDLING.md|cmd/bd/*.go;cmd/bd/errors.go"
|
||||
"engdocs/SERVE_RUNBOOK.md|internal/httpapi/server.go;internal/httpapi/events_watch.go;cmd/bd/serve.go;internal/httpapi/auth.go"
|
||||
"engdocs/LINTING.md|.golangci.yml;scripts/ci/pr-lint.sh;Makefile;.github/workflows/pr.yml;.github/workflows/main.yml"
|
||||
"engdocs/CI_CLEANUP_PLAN.md|engdocs/CI_TEST_SURFACE_AUDIT.md;.github/workflows/*.yml;.buildflags;.golangci.yml;scripts/ci/pr-lint.sh;Makefile"
|
||||
"engdocs/LINTING.md|.golangci.yml;scripts/ci/pr-lint.sh;scripts/pr-lint/;Makefile;.github/workflows/pr.yml;.github/workflows/main.yml"
|
||||
"engdocs/CI_CLEANUP_PLAN.md|engdocs/CI_TEST_SURFACE_AUDIT.md;.github/workflows/*.yml;.buildflags;.golangci.yml;scripts/ci/pr-lint.sh;scripts/pr-lint/;Makefile"
|
||||
"engdocs/design/otel/otel-data-model.md|internal/telemetry/;internal/storage/dolt/store.go;internal/compact/haiku.go;cmd/bd/find_duplicates.go;internal/hooks/"
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ var freshnessDocuments = []struct {
|
|||
{"docs/recovery/init-safety.md", []string{"cmd/bd/init.go", "cmd/bd/init_safety.go", "cmd/bd/init_safety_test.go", "cmd/bd/init_safety_help.go", "cmd/bd/dolt.go"}},
|
||||
{"engdocs/ERROR_HANDLING.md", []string{"cmd/bd/*.go", "cmd/bd/errors.go"}},
|
||||
{"engdocs/SERVE_RUNBOOK.md", []string{"internal/httpapi/server.go", "internal/httpapi/events_watch.go", "cmd/bd/serve.go", "internal/httpapi/auth.go"}},
|
||||
{"engdocs/LINTING.md", []string{".golangci.yml", "scripts/ci/pr-lint.sh", "Makefile", ".github/workflows/pr.yml", ".github/workflows/main.yml"}},
|
||||
{"engdocs/CI_CLEANUP_PLAN.md", []string{"engdocs/CI_TEST_SURFACE_AUDIT.md", ".github/workflows/*.yml", ".buildflags", ".golangci.yml", "scripts/ci/pr-lint.sh", "Makefile"}},
|
||||
{"engdocs/LINTING.md", []string{".golangci.yml", "scripts/ci/pr-lint.sh", "scripts/pr-lint/", "Makefile", ".github/workflows/pr.yml", ".github/workflows/main.yml"}},
|
||||
{"engdocs/CI_CLEANUP_PLAN.md", []string{"engdocs/CI_TEST_SURFACE_AUDIT.md", ".github/workflows/*.yml", ".buildflags", ".golangci.yml", "scripts/ci/pr-lint.sh", "scripts/pr-lint/", "Makefile"}},
|
||||
{"engdocs/design/otel/otel-data-model.md", []string{"internal/telemetry/", "internal/storage/dolt/store.go", "internal/compact/haiku.go", "cmd/bd/find_duplicates.go", "internal/hooks/"}},
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,41 +13,12 @@ source "$REPO_ROOT/scripts/ci/lib/timing.sh"
|
|||
|
||||
cd "$REPO_ROOT"
|
||||
|
||||
# The PR lane sets this to the branch it merges into so only the issues the PR
|
||||
# introduces are reported; main's push lane leaves it unset and sweeps the
|
||||
# whole tree. See the lint comment in .github/workflows/pr.yml.
|
||||
lint_scope=()
|
||||
if [[ -n "${BD_LINT_NEW_FROM_MERGE_BASE:-}" ]]; then
|
||||
lint_scope=(--new-from-merge-base="$BD_LINT_NEW_FROM_MERGE_BASE")
|
||||
fi
|
||||
|
||||
ci_time "gofmt check" -- ./scripts/ci/fmt-check.sh
|
||||
ci_time "golangci-lint" -- \
|
||||
golangci-lint run --config=.golangci.yml --modules-download-mode=readonly \
|
||||
--timeout=5m --build-tags=gms_pure_go \
|
||||
${lint_scope[@]+"${lint_scope[@]}"} ./...
|
||||
|
||||
# Other target tuples may not load files guarded by //go:build windows && !cgo.
|
||||
# Cross-lint that non-CGO Windows build too, unless the native target above
|
||||
# already matches it exactly.
|
||||
native_goos="$(go env GOOS)"
|
||||
native_cgo_enabled="$(go env CGO_ENABLED)"
|
||||
if [[ "$native_goos" != "windows" || "$native_cgo_enabled" != "0" ]]; then
|
||||
ci_time "golangci-lint (windows)" -- \
|
||||
env GOOS=windows GOARCH=amd64 CGO_ENABLED=0 GOWORK=off \
|
||||
golangci-lint run --config=.golangci.yml --modules-download-mode=readonly \
|
||||
--timeout=5m --build-tags=gms_pure_go \
|
||||
${lint_scope[@]+"${lint_scope[@]}"} ./...
|
||||
fi
|
||||
|
||||
# Files guarded by //go:build darwin (and the !windows && !linux fallbacks)
|
||||
# are invisible to the Linux runner too. Cross-lint the non-CGO darwin build
|
||||
# from the same runner so a darwin-only finding fails the PR instead of the
|
||||
# next maintainer's laptop, unless the native target above already matches it.
|
||||
if [[ "$native_goos" != "darwin" || "$native_cgo_enabled" != "0" ]]; then
|
||||
ci_time "golangci-lint (darwin)" -- \
|
||||
env GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 GOWORK=off \
|
||||
golangci-lint run --config=.golangci.yml --modules-download-mode=readonly \
|
||||
--timeout=5m --build-tags=gms_pure_go \
|
||||
${lint_scope[@]+"${lint_scope[@]}"} ./...
|
||||
fi
|
||||
# The checkout-owned Go driver is the single authority for the native and
|
||||
# cross-target lint arguments. Files guarded by //go:build windows && !cgo or
|
||||
# //go:build darwin are invisible to the Linux runner, so the driver cross-lints
|
||||
# those non-CGO targets from the same runner. Keep this wrapper as the supported
|
||||
# direct Bash/Make entrypoint and aggregate timing boundary.
|
||||
ci_time "golangci-lint (native + windows/darwin non-CGO)" -- \
|
||||
go run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint
|
||||
|
|
|
|||
22
scripts/pr-lint/BUILD.bazel
Normal file
22
scripts/pr-lint/BUILD.bazel
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
load("@rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
|
||||
|
||||
go_library(
|
||||
name = "pr-lint_lib",
|
||||
srcs = ["main.go"],
|
||||
importpath = "github.com/steveyegge/beads/scripts/pr-lint",
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
go_binary(
|
||||
name = "pr-lint",
|
||||
embed = [":pr-lint_lib"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "pr-lint_test",
|
||||
srcs = ["main_test.go"],
|
||||
data = ["//:.buildflags"],
|
||||
embed = [":pr-lint_lib"],
|
||||
deps = ["//internal/testutil/bazeltest"],
|
||||
)
|
||||
374
scripts/pr-lint/main.go
Normal file
374
scripts/pr-lint/main.go
Normal file
|
|
@ -0,0 +1,374 @@
|
|||
// pr-lint runs the repository's canonical Go lint passes without requiring
|
||||
// Make or Bash. The supported scripts/ci/pr-lint.sh entrypoint delegates here,
|
||||
// and bd preflight runs this checkout-owned command for the Beads source tree.
|
||||
// The standalone workflow lint action and pre-commit hook do not call this
|
||||
// driver; their scope and behavior still differ.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
beadsBuildTags = "gms_pure_go"
|
||||
goEnvTimeout = 30 * time.Second
|
||||
lintProcessTimeout = 6 * time.Minute
|
||||
lintReportedTimeout = "5m"
|
||||
)
|
||||
|
||||
type commandSpec struct {
|
||||
name string
|
||||
args []string
|
||||
dir string
|
||||
env []string
|
||||
}
|
||||
|
||||
type processRunner interface {
|
||||
lookPath(string) (string, error)
|
||||
output(context.Context, commandSpec) ([]byte, []byte, error)
|
||||
run(context.Context, commandSpec, io.Writer, io.Writer) error
|
||||
}
|
||||
|
||||
type osProcessRunner struct{}
|
||||
|
||||
func (osProcessRunner) lookPath(name string) (string, error) {
|
||||
return exec.LookPath(name)
|
||||
}
|
||||
|
||||
func (osProcessRunner) output(ctx context.Context, spec commandSpec) ([]byte, []byte, error) {
|
||||
cmd := exec.CommandContext(ctx, spec.name, spec.args...)
|
||||
cmd.Dir = spec.dir
|
||||
cmd.Env = spec.env
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
return stdout.Bytes(), stderr.Bytes(), err
|
||||
}
|
||||
|
||||
func (osProcessRunner) run(ctx context.Context, spec commandSpec, stdout, stderr io.Writer) error {
|
||||
cmd := exec.CommandContext(ctx, spec.name, spec.args...) //nolint:gosec // G702: executable is PATH-resolved; argv positions are assembled internally, and the env merge-base remains one unshelled argv value.
|
||||
cmd.Dir = spec.dir
|
||||
cmd.Env = spec.env
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
type nativeGoEnvironment struct {
|
||||
GOOS string `json:"GOOS"`
|
||||
CGOEnabled string `json:"CGO_ENABLED"`
|
||||
GOROOT string `json:"GOROOT"`
|
||||
GOVERSION string `json:"GOVERSION"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "determine repository root: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(run(os.Args[1:], dir, os.Environ(), os.Stdout, os.Stderr, osProcessRunner{}))
|
||||
}
|
||||
|
||||
func run(args []string, dir string, environ []string, stdout, stderr io.Writer, runner processRunner) int {
|
||||
if len(args) != 0 {
|
||||
fmt.Fprintln(stderr, "usage: go run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint")
|
||||
return 2
|
||||
}
|
||||
|
||||
goPath, err := runner.lookPath("go")
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "Go toolchain not found in PATH: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
lintPath, err := runner.lookPath("golangci-lint")
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "golangci-lint not found in PATH: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
effectiveEnv := buildEnvironment(environ, runtime.GOOS == "windows")
|
||||
probeCtx, cancel := context.WithTimeout(context.Background(), goEnvTimeout)
|
||||
defer cancel()
|
||||
native, code := readNativeGoEnvironment(probeCtx, dir, effectiveEnv, goPath, stderr, runner)
|
||||
if code != 0 {
|
||||
return code
|
||||
}
|
||||
|
||||
mergeBase, _ := environmentValue(effectiveEnv, "BD_LINT_NEW_FROM_MERGE_BASE", runtime.GOOS == "windows")
|
||||
argsForLint := lintArgs(mergeBase)
|
||||
skipWindows := native.GOOS == "windows" && native.CGOEnabled == "0"
|
||||
windowsEnv := setEnvironment(effectiveEnv, map[string]string{
|
||||
"CGO_ENABLED": "0",
|
||||
"GOARCH": "amd64",
|
||||
"GOOS": "windows",
|
||||
"GOWORK": "off",
|
||||
}, runtime.GOOS == "windows")
|
||||
// Files guarded by //go:build darwin (and the !windows && !linux fallbacks)
|
||||
// are invisible to the Linux runner too, so a darwin-only finding must fail
|
||||
// the PR here instead of on the next maintainer's laptop.
|
||||
skipDarwin := native.GOOS == "darwin" && native.CGOEnabled == "0"
|
||||
darwinEnv := setEnvironment(effectiveEnv, map[string]string{
|
||||
"CGO_ENABLED": "0",
|
||||
"GOARCH": "arm64",
|
||||
"GOOS": "darwin",
|
||||
"GOWORK": "off",
|
||||
}, runtime.GOOS == "windows")
|
||||
nativeEnv := effectiveEnv
|
||||
if runtime.GOOS == "windows" {
|
||||
nativeEnv = preferSelectedGo(probeCtx, dir, effectiveEnv, native, stderr, runner)
|
||||
if !skipWindows {
|
||||
// GOWORK=off can select a different toolchain. Probe the original
|
||||
// Go executable and PATH again, before either lint pass starts.
|
||||
selected, code := readNativeGoEnvironment(probeCtx, dir, windowsEnv, goPath, stderr, runner)
|
||||
if code == 0 {
|
||||
windowsEnv = preferSelectedGo(probeCtx, dir, windowsEnv, selected, stderr, runner)
|
||||
}
|
||||
}
|
||||
if !skipDarwin {
|
||||
selected, code := readNativeGoEnvironment(probeCtx, dir, darwinEnv, goPath, stderr, runner)
|
||||
if code == 0 {
|
||||
darwinEnv = preferSelectedGo(probeCtx, dir, darwinEnv, selected, stderr, runner)
|
||||
}
|
||||
}
|
||||
}
|
||||
cancel() // All discovery shares the existing 30-second probe budget.
|
||||
if code := runLintPass(
|
||||
"golangci-lint (native)",
|
||||
commandSpec{name: lintPath, args: argsForLint, dir: dir, env: nativeEnv},
|
||||
stdout,
|
||||
stderr,
|
||||
runner,
|
||||
); code != 0 {
|
||||
return code
|
||||
}
|
||||
|
||||
if skipWindows {
|
||||
fmt.Fprintln(stdout, "==> golangci-lint (windows/amd64, non-CGO) already covered by native pass")
|
||||
} else if code := runLintPass(
|
||||
"golangci-lint (windows/amd64, non-CGO)",
|
||||
commandSpec{name: lintPath, args: argsForLint, dir: dir, env: windowsEnv},
|
||||
stdout,
|
||||
stderr,
|
||||
runner,
|
||||
); code != 0 {
|
||||
return code
|
||||
}
|
||||
|
||||
if skipDarwin {
|
||||
fmt.Fprintln(stdout, "==> golangci-lint (darwin/arm64, non-CGO) already covered by native pass")
|
||||
return 0
|
||||
}
|
||||
|
||||
return runLintPass(
|
||||
"golangci-lint (darwin/arm64, non-CGO)",
|
||||
commandSpec{name: lintPath, args: argsForLint, dir: dir, env: darwinEnv},
|
||||
stdout,
|
||||
stderr,
|
||||
runner,
|
||||
)
|
||||
}
|
||||
|
||||
func readNativeGoEnvironment(
|
||||
ctx context.Context,
|
||||
dir string,
|
||||
environ []string,
|
||||
goPath string,
|
||||
stderr io.Writer,
|
||||
runner processRunner,
|
||||
) (nativeGoEnvironment, int) {
|
||||
spec := commandSpec{
|
||||
name: goPath,
|
||||
args: []string{"env", "-json", "GOOS", "CGO_ENABLED", "GOROOT", "GOVERSION"},
|
||||
dir: dir,
|
||||
env: environ,
|
||||
}
|
||||
output, commandStderr, err := runner.output(ctx, spec)
|
||||
writeDiagnostic(stderr, commandStderr)
|
||||
if err != nil {
|
||||
writeDiagnostic(stderr, output)
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
fmt.Fprintf(stderr, "go env exceeded %s\n", goEnvTimeout)
|
||||
return nativeGoEnvironment{}, 1
|
||||
}
|
||||
fmt.Fprintf(stderr, "inspect native Go target: %v\n", err)
|
||||
return nativeGoEnvironment{}, processExitCode(err)
|
||||
}
|
||||
|
||||
var native nativeGoEnvironment
|
||||
if err := json.Unmarshal(output, &native); err != nil {
|
||||
fmt.Fprintf(stderr, "parse native Go target: %v\n", err)
|
||||
return nativeGoEnvironment{}, 1
|
||||
}
|
||||
if native.GOOS == "" || native.CGOEnabled == "" {
|
||||
fmt.Fprintf(stderr, "go env returned an incomplete target: GOOS=%q CGO_ENABLED=%q\n", native.GOOS, native.CGOEnabled)
|
||||
return nativeGoEnvironment{}, 1
|
||||
}
|
||||
return native, 0
|
||||
}
|
||||
|
||||
// On Windows, Go auto-selection starts another process instead of replacing
|
||||
// itself. Prefer the selected SDK for lint so cancellation reaches that Go
|
||||
// process directly. This is not general descendant-process containment.
|
||||
func preferSelectedGo(ctx context.Context, dir string, environ []string, selected nativeGoEnvironment, stderr io.Writer, runner processRunner) []string {
|
||||
if !filepath.IsAbs(selected.GOROOT) || selected.GOVERSION == "" {
|
||||
return environ
|
||||
}
|
||||
bin := filepath.Join(selected.GOROOT, "bin")
|
||||
if strings.ContainsRune(bin, os.PathListSeparator) {
|
||||
fmt.Fprintln(stderr, "Windows lint: retaining original Go PATH; selected SDK directory contains a PATH separator")
|
||||
return environ
|
||||
}
|
||||
// GOROOT can describe a source-only or custom installation. Verify the
|
||||
// candidate without allowing it to switch toolchains during this probe.
|
||||
output, diagnostic, err := runner.output(ctx, commandSpec{
|
||||
name: filepath.Join(bin, "go.exe"),
|
||||
args: []string{"env", "-json", "GOROOT", "GOVERSION"},
|
||||
dir: dir,
|
||||
env: setEnvironment(environ, map[string]string{"GOTOOLCHAIN": "local"}, true),
|
||||
})
|
||||
writeDiagnostic(stderr, diagnostic)
|
||||
var candidate nativeGoEnvironment
|
||||
if err != nil || json.Unmarshal(output, &candidate) != nil ||
|
||||
candidate.GOVERSION != selected.GOVERSION ||
|
||||
!strings.EqualFold(filepath.Clean(candidate.GOROOT), filepath.Clean(selected.GOROOT)) {
|
||||
fmt.Fprintln(stderr, "Windows lint: retaining original Go PATH; selected SDK could not be verified")
|
||||
return environ
|
||||
}
|
||||
path, _ := environmentValue(environ, "PATH", true)
|
||||
return setEnvironment(environ, map[string]string{"PATH": bin + string(os.PathListSeparator) + path}, true)
|
||||
}
|
||||
|
||||
func writeDiagnostic(destination io.Writer, output []byte) {
|
||||
if len(output) == 0 {
|
||||
return
|
||||
}
|
||||
_, _ = destination.Write(output)
|
||||
if output[len(output)-1] != '\n' {
|
||||
fmt.Fprintln(destination)
|
||||
}
|
||||
}
|
||||
|
||||
func runLintPass(label string, spec commandSpec, stdout, stderr io.Writer, runner processRunner) int {
|
||||
fmt.Fprintf(stdout, "==> %s\n", label)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), lintProcessTimeout)
|
||||
defer cancel()
|
||||
|
||||
err := runner.run(ctx, spec, stdout, stderr)
|
||||
if err == nil {
|
||||
fmt.Fprintf(stdout, "<== %s succeeded\n", label)
|
||||
return 0
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
fmt.Fprintf(stderr, "<== %s exceeded %s\n", label, lintProcessTimeout)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintf(stderr, "<== %s failed: %v\n", label, err)
|
||||
return processExitCode(err)
|
||||
}
|
||||
|
||||
func lintArgs(mergeBase string) []string {
|
||||
args := []string{
|
||||
"run",
|
||||
"--config=.golangci.yml",
|
||||
"--modules-download-mode=readonly",
|
||||
"--timeout=" + lintReportedTimeout,
|
||||
"--build-tags=" + beadsBuildTags,
|
||||
}
|
||||
if mergeBase != "" {
|
||||
args = append(args, "--new-from-merge-base="+mergeBase)
|
||||
}
|
||||
return append(args, "./...")
|
||||
}
|
||||
|
||||
func buildEnvironment(environ []string, caseInsensitive bool) []string {
|
||||
cgoEnabled, found := environmentValue(environ, "CGO_ENABLED", caseInsensitive)
|
||||
if !found || cgoEnabled == "" {
|
||||
cgoEnabled = "1"
|
||||
}
|
||||
|
||||
goFlags, _ := environmentValue(environ, "GOFLAGS", caseInsensitive)
|
||||
// Match .buildflags: an appended -tags value overrides an inherited bare-Go
|
||||
// -tags value rather than merging tag lists.
|
||||
if !strings.Contains(goFlags, beadsBuildTags) {
|
||||
if goFlags != "" {
|
||||
goFlags += " "
|
||||
}
|
||||
goFlags += "-tags=" + beadsBuildTags
|
||||
}
|
||||
|
||||
return setEnvironment(environ, map[string]string{
|
||||
"BEADS_BUILD_TAGS": beadsBuildTags,
|
||||
"CGO_ENABLED": cgoEnabled,
|
||||
"GOFLAGS": goFlags,
|
||||
}, caseInsensitive)
|
||||
}
|
||||
|
||||
func setEnvironment(environ []string, overrides map[string]string, caseInsensitive bool) []string {
|
||||
result := make([]string, 0, len(environ)+len(overrides))
|
||||
for _, entry := range environ {
|
||||
key, _, ok := strings.Cut(entry, "=")
|
||||
if !ok {
|
||||
result = append(result, entry)
|
||||
continue
|
||||
}
|
||||
if containsEnvironmentKey(overrides, key, caseInsensitive) {
|
||||
continue
|
||||
}
|
||||
result = append(result, entry)
|
||||
}
|
||||
for key, value := range overrides {
|
||||
result = append(result, key+"="+value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func environmentValue(environ []string, wanted string, caseInsensitive bool) (string, bool) {
|
||||
for index := len(environ) - 1; index >= 0; index-- {
|
||||
key, value, ok := strings.Cut(environ[index], "=")
|
||||
if ok && environmentKeysEqual(key, wanted, caseInsensitive) {
|
||||
return value, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func containsEnvironmentKey(values map[string]string, wanted string, caseInsensitive bool) bool {
|
||||
for key := range values {
|
||||
if environmentKeysEqual(key, wanted, caseInsensitive) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func environmentKeysEqual(left, right string, caseInsensitive bool) bool {
|
||||
if caseInsensitive {
|
||||
return strings.ToLower(left) == strings.ToLower(right)
|
||||
}
|
||||
return left == right
|
||||
}
|
||||
|
||||
func processExitCode(err error) int {
|
||||
if err == nil {
|
||||
return 0
|
||||
}
|
||||
var exitErr interface{ ExitCode() int }
|
||||
if errors.As(err, &exitErr) && exitErr.ExitCode() >= 0 {
|
||||
return exitErr.ExitCode()
|
||||
}
|
||||
return 1
|
||||
}
|
||||
578
scripts/pr-lint/main_test.go
Normal file
578
scripts/pr-lint/main_test.go
Normal file
|
|
@ -0,0 +1,578 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
||||
)
|
||||
|
||||
type recordingRunner struct {
|
||||
paths map[string]string
|
||||
goEnvOutput string
|
||||
goEnvStderr string
|
||||
goEnvErr error
|
||||
runErrors []error
|
||||
outputCommands []commandSpec
|
||||
runCommands []commandSpec
|
||||
outputFunc func(context.Context, commandSpec) ([]byte, []byte, error)
|
||||
}
|
||||
|
||||
func (runner *recordingRunner) lookPath(name string) (string, error) {
|
||||
if path, ok := runner.paths[name]; ok {
|
||||
return path, nil
|
||||
}
|
||||
return "", errors.New("synthetic missing command")
|
||||
}
|
||||
|
||||
func (runner *recordingRunner) output(ctx context.Context, spec commandSpec) ([]byte, []byte, error) {
|
||||
runner.outputCommands = append(runner.outputCommands, spec)
|
||||
if runner.outputFunc != nil {
|
||||
return runner.outputFunc(ctx, spec)
|
||||
}
|
||||
return []byte(runner.goEnvOutput), []byte(runner.goEnvStderr), runner.goEnvErr
|
||||
}
|
||||
|
||||
func (runner *recordingRunner) run(_ context.Context, spec commandSpec, stdout, _ io.Writer) error {
|
||||
runner.runCommands = append(runner.runCommands, spec)
|
||||
_, _ = io.WriteString(stdout, "synthetic lint output\n")
|
||||
index := len(runner.runCommands) - 1
|
||||
if index < len(runner.runErrors) {
|
||||
return runner.runErrors[index]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type syntheticExitError struct {
|
||||
code int
|
||||
}
|
||||
|
||||
func (err syntheticExitError) Error() string {
|
||||
return "synthetic command failure"
|
||||
}
|
||||
|
||||
func (err syntheticExitError) ExitCode() int {
|
||||
return err.code
|
||||
}
|
||||
|
||||
func TestRunUsesCanonicalNativeAndCrossTargetPasses(t *testing.T) {
|
||||
runner := &recordingRunner{
|
||||
paths: map[string]string{
|
||||
"go": "/tools/go",
|
||||
"golangci-lint": "/tools/golangci-lint",
|
||||
},
|
||||
goEnvOutput: `{"GOOS":"linux","CGO_ENABLED":"1"}`,
|
||||
}
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
environ := []string{
|
||||
"PATH=/tools",
|
||||
"BD_LINT_NEW_FROM_MERGE_BASE=origin/main",
|
||||
}
|
||||
|
||||
code := run(nil, "/repo", environ, &stdout, &stderr, runner)
|
||||
if code != 0 {
|
||||
t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String())
|
||||
}
|
||||
wantProbes := 1
|
||||
if runtime.GOOS == "windows" {
|
||||
// One native probe plus one per cross-target pass.
|
||||
wantProbes = 3
|
||||
}
|
||||
if len(runner.outputCommands) != wantProbes {
|
||||
t.Fatalf("go env calls = %d, want %d", len(runner.outputCommands), wantProbes)
|
||||
}
|
||||
wantGoArgs := []string{"env", "-json", "GOOS", "CGO_ENABLED", "GOROOT", "GOVERSION"}
|
||||
if got := runner.outputCommands[0].args; !reflect.DeepEqual(got, wantGoArgs) {
|
||||
t.Fatalf("go env args = %#v, want %#v", got, wantGoArgs)
|
||||
}
|
||||
if len(runner.runCommands) != 3 {
|
||||
t.Fatalf("lint calls = %d, want 3", len(runner.runCommands))
|
||||
}
|
||||
wantLintArgs := []string{
|
||||
"run",
|
||||
"--config=.golangci.yml",
|
||||
"--modules-download-mode=readonly",
|
||||
"--timeout=5m",
|
||||
"--build-tags=gms_pure_go",
|
||||
"--new-from-merge-base=origin/main",
|
||||
"./...",
|
||||
}
|
||||
for index, command := range runner.runCommands {
|
||||
if command.name != "/tools/golangci-lint" {
|
||||
t.Fatalf("lint call %d executable = %q", index, command.name)
|
||||
}
|
||||
if command.dir != "/repo" {
|
||||
t.Fatalf("lint call %d dir = %q, want /repo", index, command.dir)
|
||||
}
|
||||
if !reflect.DeepEqual(command.args, wantLintArgs) {
|
||||
t.Fatalf("lint call %d args = %#v, want %#v", index, command.args, wantLintArgs)
|
||||
}
|
||||
}
|
||||
assertEnvironmentValue(t, runner.runCommands[0].env, "CGO_ENABLED", "1", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[0].env, "BEADS_BUILD_TAGS", "gms_pure_go", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[0].env, "GOFLAGS", "-tags=gms_pure_go", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[1].env, "GOOS", "windows", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[1].env, "GOARCH", "amd64", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[1].env, "CGO_ENABLED", "0", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[1].env, "GOWORK", "off", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[2].env, "GOOS", "darwin", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[2].env, "GOARCH", "arm64", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[2].env, "CGO_ENABLED", "0", false)
|
||||
assertEnvironmentValue(t, runner.runCommands[2].env, "GOWORK", "off", false)
|
||||
for _, heading := range []string{
|
||||
"==> golangci-lint (native)",
|
||||
"==> golangci-lint (windows/amd64, non-CGO)",
|
||||
"==> golangci-lint (darwin/arm64, non-CGO)",
|
||||
} {
|
||||
if !strings.Contains(stdout.String(), heading) {
|
||||
t.Fatalf("missing lane heading %q in output:\n%s", heading, stdout.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLintArgsKeepsHostileMergeBaseInOneArgument(t *testing.T) {
|
||||
mergeBase := `origin/main; printf injected >&2`
|
||||
args := lintArgs(mergeBase)
|
||||
want := "--new-from-merge-base=" + mergeBase
|
||||
count := 0
|
||||
for _, arg := range args {
|
||||
if arg == want {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("hostile merge base occurrence count = %d, want 1; args=%#v", count, args)
|
||||
}
|
||||
if len(args) != 7 {
|
||||
t.Fatalf("hostile merge base changed argv cardinality: %#v", args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSkipsDuplicateNativeWindowsNonCGOPass(t *testing.T) {
|
||||
runner := &recordingRunner{
|
||||
paths: map[string]string{
|
||||
"go": "go",
|
||||
"golangci-lint": "golangci-lint",
|
||||
},
|
||||
goEnvOutput: `{"GOOS":"windows","CGO_ENABLED":"0"}`,
|
||||
}
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
code := run(nil, "/repo", []string{"CGO_ENABLED=0"}, &stdout, &stderr, runner)
|
||||
if code != 0 {
|
||||
t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String())
|
||||
}
|
||||
// The native pass already covers windows/non-CGO, so only the native and
|
||||
// darwin cross-target passes remain.
|
||||
if len(runner.runCommands) != 2 {
|
||||
t.Fatalf("lint calls = %d, want 2", len(runner.runCommands))
|
||||
}
|
||||
assertEnvironmentValue(t, runner.runCommands[1].env, "GOOS", "darwin", false)
|
||||
if !strings.Contains(stdout.String(), "==> golangci-lint (windows/amd64, non-CGO) already covered by native pass") {
|
||||
t.Fatalf("missing duplicate-pass diagnostic:\n%s", stdout.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSkipsDuplicateNativeDarwinNonCGOPass(t *testing.T) {
|
||||
runner := &recordingRunner{
|
||||
paths: map[string]string{
|
||||
"go": "go",
|
||||
"golangci-lint": "golangci-lint",
|
||||
},
|
||||
goEnvOutput: `{"GOOS":"darwin","CGO_ENABLED":"0"}`,
|
||||
}
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
code := run(nil, "/repo", []string{"CGO_ENABLED=0"}, &stdout, &stderr, runner)
|
||||
if code != 0 {
|
||||
t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String())
|
||||
}
|
||||
// The native pass already covers darwin/non-CGO, so only the native and
|
||||
// windows cross-target passes remain.
|
||||
if len(runner.runCommands) != 2 {
|
||||
t.Fatalf("lint calls = %d, want 2", len(runner.runCommands))
|
||||
}
|
||||
assertEnvironmentValue(t, runner.runCommands[1].env, "GOOS", "windows", false)
|
||||
if !strings.Contains(stdout.String(), "==> golangci-lint (darwin/arm64, non-CGO) already covered by native pass") {
|
||||
t.Fatalf("missing duplicate-pass diagnostic:\n%s", stdout.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestOSProcessRunnerSeparatesStdoutAndStderr(t *testing.T) {
|
||||
const helperEnvironment = "BEADS_PR_LINT_OUTPUT_HELPER"
|
||||
if os.Getenv(helperEnvironment) == "1" {
|
||||
_, _ = io.WriteString(os.Stdout, `{"GOOS":"windows","CGO_ENABLED":"0"}`)
|
||||
_, _ = io.WriteString(os.Stderr, "synthetic benign go env warning\n")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
stdout, stderr, err := (osProcessRunner{}).output(context.Background(), commandSpec{
|
||||
name: os.Args[0],
|
||||
args: []string{"-test.run=^TestOSProcessRunnerSeparatesStdoutAndStderr$"},
|
||||
env: append(os.Environ(), helperEnvironment+"=1"),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("output helper failed: %v; stderr=%s", err, stderr)
|
||||
}
|
||||
if got, want := string(stdout), `{"GOOS":"windows","CGO_ENABLED":"0"}`; got != want {
|
||||
t.Fatalf("stdout = %q, want JSON-only %q", got, want)
|
||||
}
|
||||
if got, want := string(stderr), "synthetic benign go env warning\n"; got != want {
|
||||
t.Fatalf("stderr = %q, want warning-only %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunParsesGoEnvStdoutWhenSuccessfulCommandWarnsOnStderr(t *testing.T) {
|
||||
runner := &recordingRunner{
|
||||
paths: map[string]string{
|
||||
"go": "go",
|
||||
"golangci-lint": "golangci-lint",
|
||||
},
|
||||
goEnvOutput: `{"GOOS":"windows","CGO_ENABLED":"0"}`,
|
||||
goEnvStderr: "synthetic benign go env warning\n",
|
||||
}
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
code := run(nil, "/repo", []string{"CGO_ENABLED=0"}, &stdout, &stderr, runner)
|
||||
if code != 0 {
|
||||
t.Fatalf("run exit = %d, want 0; stderr=%s", code, stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "synthetic benign go env warning") {
|
||||
t.Fatalf("go env stderr warning was not surfaced: %q", stderr.String())
|
||||
}
|
||||
if strings.Contains(stderr.String(), "parse native Go target") {
|
||||
t.Fatalf("go env stderr corrupted stdout JSON parsing: %q", stderr.String())
|
||||
}
|
||||
if len(runner.runCommands) != 2 {
|
||||
t.Fatalf("lint calls = %d, want the native Windows/non-CGO pass plus the darwin cross-lint", len(runner.runCommands))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPreservesNativeLintExitCodeAndStops(t *testing.T) {
|
||||
runner := &recordingRunner{
|
||||
paths: map[string]string{
|
||||
"go": "go",
|
||||
"golangci-lint": "golangci-lint",
|
||||
},
|
||||
goEnvOutput: `{"GOOS":"linux","CGO_ENABLED":"1"}`,
|
||||
runErrors: []error{syntheticExitError{code: 23}},
|
||||
}
|
||||
var stdout bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
|
||||
code := run(nil, "/repo", nil, &stdout, &stderr, runner)
|
||||
if code != 23 {
|
||||
t.Fatalf("run exit = %d, want 23", code)
|
||||
}
|
||||
if len(runner.runCommands) != 1 {
|
||||
t.Fatalf("lint calls = %d, want only failed native pass", len(runner.runCommands))
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "golangci-lint (native) failed") {
|
||||
t.Fatalf("missing native failure diagnostic:\n%s", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunFailsClearlyWhenCapabilityIsMissing(t *testing.T) {
|
||||
for _, missing := range []string{"go", "golangci-lint"} {
|
||||
t.Run(missing, func(t *testing.T) {
|
||||
paths := map[string]string{
|
||||
"go": "go",
|
||||
"golangci-lint": "golangci-lint",
|
||||
}
|
||||
delete(paths, missing)
|
||||
runner := &recordingRunner{paths: paths}
|
||||
var stderr bytes.Buffer
|
||||
|
||||
if code := run(nil, "/repo", nil, io.Discard, &stderr, runner); code == 0 {
|
||||
t.Fatal("missing capability unexpectedly passed")
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(stderr.String()), missing) || !strings.Contains(stderr.String(), "PATH") {
|
||||
t.Fatalf("missing %s diagnostic is unclear: %q", missing, stderr.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildEnvironmentMatchesBuildFlagsContract(t *testing.T) {
|
||||
environ := []string{
|
||||
"PATH=/tools",
|
||||
"CGO_ENABLED=0",
|
||||
"GOFLAGS=-mod=readonly",
|
||||
"BEADS_BUILD_TAGS=stale",
|
||||
}
|
||||
got := buildEnvironment(environ, false)
|
||||
assertEnvironmentValue(t, got, "CGO_ENABLED", "0", false)
|
||||
assertEnvironmentValue(t, got, "GOFLAGS", "-mod=readonly -tags=gms_pure_go", false)
|
||||
assertEnvironmentValue(t, got, "BEADS_BUILD_TAGS", "gms_pure_go", false)
|
||||
|
||||
defaulted := buildEnvironment([]string{"CGO_ENABLED="}, false)
|
||||
assertEnvironmentValue(t, defaulted, "CGO_ENABLED", "1", false)
|
||||
assertEnvironmentValue(t, defaulted, "GOFLAGS", "-tags=gms_pure_go", false)
|
||||
}
|
||||
|
||||
func TestBuildEnvironmentNormalizesWindowsKeyIdentity(t *testing.T) {
|
||||
environ := []string{
|
||||
"cgo_enabled=0",
|
||||
"CGO_ENABLED=1",
|
||||
"GoFlags=-mod=readonly",
|
||||
"GOFLAGS=-trimpath",
|
||||
"beads_build_tags=stale",
|
||||
"BEADſ_BUILD_TAGS=near-collision",
|
||||
"MALFORMED",
|
||||
"=C:=C:\\source\\beads",
|
||||
}
|
||||
got := buildEnvironment(environ, true)
|
||||
|
||||
assertSingleLogicalEnvironmentKey(t, got, "CGO_ENABLED")
|
||||
assertSingleLogicalEnvironmentKey(t, got, "GOFLAGS")
|
||||
assertSingleLogicalEnvironmentKey(t, got, "BEADS_BUILD_TAGS")
|
||||
assertEnvironmentValue(t, got, "CGO_ENABLED", "1", true)
|
||||
assertEnvironmentValue(t, got, "GOFLAGS", "-trimpath -tags=gms_pure_go", true)
|
||||
assertEnvironmentValue(t, got, "BEADS_BUILD_TAGS", "gms_pure_go", true)
|
||||
for _, preserved := range []string{"BEADſ_BUILD_TAGS=near-collision", "MALFORMED", "=C:=C:\\source\\beads"} {
|
||||
if !containsExact(got, preserved) {
|
||||
t.Fatalf("environment entry %q was not preserved in %#v", preserved, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunRejectsArguments(t *testing.T) {
|
||||
var stderr bytes.Buffer
|
||||
code := run([]string{"--unexpected"}, "/repo", nil, io.Discard, &stderr, &recordingRunner{})
|
||||
if code != 2 {
|
||||
t.Fatalf("run exit = %d, want 2", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "usage:") {
|
||||
t.Fatalf("missing usage diagnostic: %q", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
func assertEnvironmentValue(t *testing.T, environ []string, key, want string, caseInsensitive bool) {
|
||||
t.Helper()
|
||||
got, found := environmentValue(environ, key, caseInsensitive)
|
||||
if !found || got != want {
|
||||
t.Fatalf("%s = %q, found=%v, want %q; env=%#v", key, got, found, want, environ)
|
||||
}
|
||||
}
|
||||
|
||||
func assertSingleLogicalEnvironmentKey(t *testing.T, environ []string, wanted string) {
|
||||
t.Helper()
|
||||
count := 0
|
||||
for _, entry := range environ {
|
||||
key, _, ok := strings.Cut(entry, "=")
|
||||
if ok && strings.ToLower(key) == strings.ToLower(wanted) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("logical environment key %s occurs %d times in %#v", wanted, count, environ)
|
||||
}
|
||||
}
|
||||
|
||||
func containsExact(values []string, wanted string) bool {
|
||||
for _, value := range values {
|
||||
if value == wanted {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestRunUsesEachWindowsPassSelectedToolchain(t *testing.T) {
|
||||
if runtime.GOOS != "windows" {
|
||||
t.Skip("Windows auto-selection creates the extra process")
|
||||
}
|
||||
nativeRoot, crossRoot := filepath.Join(t.TempDir(), "workspace SDK"), filepath.Join(t.TempDir(), "module SDK")
|
||||
environ := []string{"Path=original", "GOTOOLCHAIN=auto", "GOROOT=custom", "GOWORK=workspace"}
|
||||
runner := &recordingRunner{paths: map[string]string{"go": "launcher", "golangci-lint": "lint"}}
|
||||
var probeContext context.Context
|
||||
runner.outputFunc = func(ctx context.Context, spec commandSpec) ([]byte, []byte, error) {
|
||||
if probeContext != nil && ctx != probeContext {
|
||||
t.Fatal("toolchain discovery escaped the shared probe deadline")
|
||||
}
|
||||
probeContext = ctx
|
||||
selected := nativeGoEnvironment{GOOS: "windows", CGOEnabled: "1", GOROOT: nativeRoot, GOVERSION: "go1.26.7"}
|
||||
if work, _ := environmentValue(spec.env, "GOWORK", true); work == "off" {
|
||||
selected.GOROOT, selected.GOVERSION, selected.CGOEnabled = crossRoot, "go1.26.5", "0"
|
||||
}
|
||||
assertEnvironmentValue(t, spec.env, "PATH", "original", true)
|
||||
if spec.name != "launcher" {
|
||||
if spec.name != filepath.Join(selected.GOROOT, "bin", "go.exe") {
|
||||
t.Fatalf("candidate = %q, root = %q", spec.name, selected.GOROOT)
|
||||
}
|
||||
assertEnvironmentValue(t, spec.env, "GOTOOLCHAIN", "local", true)
|
||||
}
|
||||
data, err := json.Marshal(selected)
|
||||
return data, nil, err
|
||||
}
|
||||
var stderr bytes.Buffer
|
||||
if code := run(nil, ".", environ, io.Discard, &stderr, runner); code != 0 || len(runner.runCommands) != 2 {
|
||||
t.Fatalf("run = %d, passes = %d, stderr=%s", code, len(runner.runCommands), &stderr)
|
||||
}
|
||||
for index, root := range []string{nativeRoot, crossRoot} {
|
||||
env := runner.runCommands[index].env
|
||||
assertSingleLogicalEnvironmentKey(t, env, "PATH")
|
||||
assertEnvironmentValue(t, env, "PATH", filepath.Join(root, "bin")+";original", true)
|
||||
assertEnvironmentValue(t, env, "GOTOOLCHAIN", "auto", true)
|
||||
assertEnvironmentValue(t, env, "GOROOT", "custom", true)
|
||||
}
|
||||
if environ[0] != "Path=original" {
|
||||
t.Fatal("lint changed the caller's PATH")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectedGoFallsBackForNonstandardSDK(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
selected := nativeGoEnvironment{GOROOT: root, GOVERSION: "go1.26.7"}
|
||||
environ := []string{"Path=original", "GOTOOLCHAIN=auto", "GOROOT=custom"}
|
||||
t.Run("PATH separator", func(t *testing.T) {
|
||||
unsupported := selected
|
||||
unsupported.GOROOT += string(os.PathListSeparator) + "SDK"
|
||||
data, err := json.Marshal(unsupported)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var diagnostic bytes.Buffer
|
||||
got := preferSelectedGo(context.Background(), ".", environ, unsupported, &diagnostic, &recordingRunner{goEnvOutput: string(data)})
|
||||
if !reflect.DeepEqual(got, environ) || !strings.Contains(diagnostic.String(), "retaining original Go PATH") {
|
||||
t.Fatalf("fallback env=%v, diagnostic=%s", got, &diagnostic)
|
||||
}
|
||||
})
|
||||
for _, tc := range []struct {
|
||||
name, root, version string
|
||||
err error
|
||||
}{
|
||||
{"missing executable", root, "go1.26.7", os.ErrNotExist},
|
||||
{"wrong version", root, "go1.26.5", nil},
|
||||
{"different root", filepath.Join(root, "other"), "go1.26.7", nil},
|
||||
{"deadline", root, "go1.26.7", context.DeadlineExceeded},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
data, err := json.Marshal(nativeGoEnvironment{GOROOT: tc.root, GOVERSION: tc.version})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := &recordingRunner{goEnvOutput: string(data), goEnvErr: tc.err}
|
||||
var diagnostic bytes.Buffer
|
||||
got := preferSelectedGo(context.Background(), ".", environ, selected, &diagnostic, runner)
|
||||
if !reflect.DeepEqual(got, environ) || !strings.Contains(diagnostic.String(), "retaining original Go PATH") {
|
||||
t.Fatalf("fallback env=%v, diagnostic=%s", got, &diagnostic)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectedGoIsResolvedByLintChild(t *testing.T) {
|
||||
const marker = "BEADS_SELECTED_GO_PROBE"
|
||||
if expected := os.Getenv(marker); expected != "" {
|
||||
resolved, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, gotErr := os.Stat(resolved)
|
||||
want, wantErr := os.Stat(expected)
|
||||
if gotErr != nil || wantErr != nil || !os.SameFile(got, want) {
|
||||
t.Fatalf("child resolved %q, want identity of %q", resolved, expected)
|
||||
}
|
||||
return
|
||||
}
|
||||
if runtime.GOOS != "windows" {
|
||||
t.Skip("Windows executable discovery")
|
||||
}
|
||||
root := t.TempDir()
|
||||
selectedBin, oldBin := filepath.Join(root, "selected SDK", "bin"), filepath.Join(root, "old SDK", "bin")
|
||||
for _, bin := range []string{selectedBin, oldBin} {
|
||||
if err := os.MkdirAll(bin, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(bin, "go.exe"), nil, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
selected := nativeGoEnvironment{GOROOT: filepath.Dir(selectedBin), GOVERSION: "go1.26.7"}
|
||||
data, err := json.Marshal(selected)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
environ := setEnvironment(os.Environ(), map[string]string{"PATH": oldBin, marker: filepath.Join(selectedBin, "go.exe")}, true)
|
||||
cmd := exec.Command(os.Args[0], "-test.run=^TestSelectedGoIsResolvedByLintChild$")
|
||||
cmd.Env = preferSelectedGo(context.Background(), root, environ, selected, io.Discard, &recordingRunner{goEnvOutput: string(data)})
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
t.Fatalf("lint child Go discovery: %v\n%s", err, output)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildFlagsContractMatchesShellSource reads .buildflags itself and pins
|
||||
// the driver constants to it. TestBuildEnvironmentMatchesBuildFlagsContract
|
||||
// pins today's values from the Go side only, so the two files could drift
|
||||
// apart silently: buildEnvironment appends its own -tags value and overrides
|
||||
// rather than merges, which means a tag added to .buildflags alone would be
|
||||
// dropped by the driver with every existing test still green.
|
||||
func TestBuildFlagsContractMatchesShellSource(t *testing.T) {
|
||||
_, thisFile, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("runtime.Caller(0) failed")
|
||||
}
|
||||
repoRoot := filepath.Dir(filepath.Dir(bazeltest.CallerDir(thisFile, "scripts/pr-lint")))
|
||||
path := filepath.Join(repoRoot, ".buildflags")
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", path, err)
|
||||
}
|
||||
source := string(data)
|
||||
|
||||
// export BEADS_BUILD_TAGS="gms_pure_go"
|
||||
tags := regexp.MustCompile(`(?m)^export BEADS_BUILD_TAGS="([^"]*)"$`).FindStringSubmatch(source)
|
||||
if tags == nil {
|
||||
t.Fatalf(".buildflags no longer exports BEADS_BUILD_TAGS in the expected form:\n%s", source)
|
||||
}
|
||||
if tags[1] != beadsBuildTags {
|
||||
t.Errorf("BEADS_BUILD_TAGS drift: .buildflags has %q, driver constant beadsBuildTags is %q; "+
|
||||
"buildEnvironment overrides rather than merges -tags, so the shell value would be silently dropped",
|
||||
tags[1], beadsBuildTags)
|
||||
}
|
||||
|
||||
// : "${CGO_ENABLED:=1}"
|
||||
cgo := regexp.MustCompile(`(?m)^: "\$\{CGO_ENABLED:=([^}]*)\}"$`).FindStringSubmatch(source)
|
||||
if cgo == nil {
|
||||
t.Fatalf(".buildflags no longer defaults CGO_ENABLED in the expected form:\n%s", source)
|
||||
}
|
||||
if got, _ := environmentValue(buildEnvironment([]string{"CGO_ENABLED="}, false), "CGO_ENABLED", false); got != cgo[1] {
|
||||
t.Errorf("CGO_ENABLED default drift: .buildflags defaults to %q, buildEnvironment defaults to %q", cgo[1], got)
|
||||
}
|
||||
|
||||
// if [[ "${GOFLAGS:-}" != *gms_pure_go* ]]; then
|
||||
guard := regexp.MustCompile(`(?m)^if \[\[ "\$\{GOFLAGS:-\}" != \*([^*]*)\* \]\]; then$`).FindStringSubmatch(source)
|
||||
if guard == nil {
|
||||
t.Fatalf(".buildflags no longer guards GOFLAGS in the expected form:\n%s", source)
|
||||
}
|
||||
if guard[1] != beadsBuildTags {
|
||||
t.Errorf("GOFLAGS guard drift: .buildflags skips its append when GOFLAGS contains %q, "+
|
||||
"driver skips on %q; the two must test the same token or one appends when the other does not",
|
||||
guard[1], beadsBuildTags)
|
||||
}
|
||||
// The shell guard is a substring match (*tag*), and buildEnvironment uses
|
||||
// strings.Contains for the same reason. Pin that agreement: making only the
|
||||
// Go side stricter would append a tag the wrapper had already decided was
|
||||
// present.
|
||||
preset := []string{"GOFLAGS=-tags=" + guard[1] + "_x"}
|
||||
if got, _ := environmentValue(buildEnvironment(preset, false), "GOFLAGS", false); got != preset[0][len("GOFLAGS="):] {
|
||||
t.Errorf("GOFLAGS guard is no longer the substring test .buildflags performs: got %q, want %q",
|
||||
got, preset[0][len("GOFLAGS="):])
|
||||
}
|
||||
}
|
||||
|
|
@ -7,9 +7,12 @@ go_test(
|
|||
"prlintmake_test.go",
|
||||
],
|
||||
data = [
|
||||
"//:.buildflags",
|
||||
"//:go.mod",
|
||||
"//scripts:ci/fmt-check.sh",
|
||||
"//scripts:ci/gofmt-bin.sh",
|
||||
"//scripts:ci/lib/timing.sh",
|
||||
"//scripts:ci/pr-lint.sh",
|
||||
],
|
||||
deps = ["//internal/testutil/bazeltest"],
|
||||
)
|
||||
|
|
|
|||
|
|
@ -78,6 +78,115 @@ func TestFmtCheckPreservesGofmtFailure(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPRLintWrapperDelegatesPolicyToCheckoutGoDriver(t *testing.T) {
|
||||
run := runPRLintWrapper(t, "exit 0\n")
|
||||
if run.err != nil {
|
||||
t.Fatalf("pr-lint wrapper failed: %v\n%s", run.err, run.output)
|
||||
}
|
||||
if run.goArgs != "run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint" {
|
||||
t.Fatalf("go args = %q, want checkout driver invocation", run.goArgs)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"CGO_ENABLED=1",
|
||||
"BEADS_BUILD_TAGS=gms_pure_go",
|
||||
"GOFLAGS=-mod=readonly -tags=gms_pure_go",
|
||||
"BD_LINT_NEW_FROM_MERGE_BASE=origin/main",
|
||||
} {
|
||||
if !strings.Contains(run.goEnvironment, want+"\n") {
|
||||
t.Fatalf("driver environment missing %q:\n%s", want, run.goEnvironment)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(run.output, "==> golangci-lint (native + windows/darwin non-CGO)") ||
|
||||
!strings.Contains(run.output, "<== golangci-lint (native + windows/darwin non-CGO) succeeded") {
|
||||
t.Fatalf("aggregate lint timing is not attributable:\n%s", run.output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPRLintWrapperReportsGoRunFailure(t *testing.T) {
|
||||
run := runPRLintWrapper(t, "printf 'exit status 42\\n' >&2\nexit 1\n")
|
||||
if got := processExitCode(run.err); got == 0 {
|
||||
t.Fatalf("exit = %d, want nonzero; error=%v\n%s", got, run.err, run.output)
|
||||
}
|
||||
if !strings.Contains(run.output, "exit status 42") || !strings.Contains(run.output, "failed after") {
|
||||
t.Fatalf("missing aggregate failure diagnostic:\n%s", run.output)
|
||||
}
|
||||
}
|
||||
|
||||
type prLintWrapperRun struct {
|
||||
output string
|
||||
err error
|
||||
goArgs string
|
||||
goEnvironment string
|
||||
}
|
||||
|
||||
func runPRLintWrapper(t *testing.T, goBody string) prLintWrapperRun {
|
||||
t.Helper()
|
||||
bash := testBash(t)
|
||||
testRoot := t.TempDir()
|
||||
shimDir := filepath.Join(testRoot, "pr-lint shims")
|
||||
if err := os.MkdirAll(shimDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// fmt-check.sh ignores a PATH gofmt (be-gx8), so the gofmt shim goes in
|
||||
// through GOFMT, as in runFmtCheck. Only the go shim needs to be on PATH.
|
||||
gofmt := filepath.Join(testRoot, "gofmt")
|
||||
writeShellExecutable(t, bash, gofmt, "#!/usr/bin/env bash\nset -euo pipefail\nexit 0\n")
|
||||
writeShellExecutable(t, bash, filepath.Join(shimDir, "go"), `#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
printf '%s\n' "$*" >"$GO_ARGS_MARKER"
|
||||
printf 'CGO_ENABLED=%s\n' "${CGO_ENABLED-}" >"$GO_ENV_MARKER"
|
||||
printf 'BEADS_BUILD_TAGS=%s\n' "${BEADS_BUILD_TAGS-}" >>"$GO_ENV_MARKER"
|
||||
printf 'GOFLAGS=%s\n' "${GOFLAGS-}" >>"$GO_ENV_MARKER"
|
||||
printf 'BD_LINT_NEW_FROM_MERGE_BASE=%s\n' "${BD_LINT_NEW_FROM_MERGE_BASE-}" >>"$GO_ENV_MARKER"
|
||||
`+goBody)
|
||||
|
||||
argsMarker := filepath.Join(testRoot, "go-args")
|
||||
envMarker := filepath.Join(testRoot, "go-environment")
|
||||
path := shimDir + string(os.PathListSeparator) + os.Getenv("PATH")
|
||||
if runtime.GOOS == "windows" {
|
||||
path = msysPath(shimDir) + ":/usr/bin:/bin"
|
||||
}
|
||||
cmd := exec.Command(
|
||||
bash,
|
||||
"--noprofile",
|
||||
"--norc",
|
||||
"--",
|
||||
shellVisiblePath(filepath.Join(sourceRepoRoot(), "scripts", "ci", "pr-lint.sh")),
|
||||
)
|
||||
cmd.Dir = sourceRepoRoot()
|
||||
cmd.Env = environment(map[string]string{
|
||||
"BASH_ENV": "",
|
||||
"BASHOPTS": "",
|
||||
"BD_LINT_NEW_FROM_MERGE_BASE": "origin/main",
|
||||
"BEADS_BUILD_TAGS": "stale",
|
||||
"CGO_ENABLED": "",
|
||||
"ENV": "",
|
||||
"GOFLAGS": "-mod=readonly",
|
||||
"GOFMT": shellVisiblePath(gofmt),
|
||||
"GO_ARGS_MARKER": shellVisiblePath(argsMarker),
|
||||
"GO_ENV_MARKER": shellVisiblePath(envMarker),
|
||||
"LANG": "C",
|
||||
"LC_ALL": "C",
|
||||
"PATH": path,
|
||||
"SHELLOPTS": "",
|
||||
})
|
||||
output, runErr := cmd.CombinedOutput()
|
||||
args, argsErr := os.ReadFile(argsMarker)
|
||||
if argsErr != nil {
|
||||
t.Fatalf("read go argument marker: %v\n%s", argsErr, output)
|
||||
}
|
||||
environment, envErr := os.ReadFile(envMarker)
|
||||
if envErr != nil {
|
||||
t.Fatalf("read go environment marker: %v\n%s", envErr, output)
|
||||
}
|
||||
return prLintWrapperRun{
|
||||
output: normalizeNewlines(string(output)),
|
||||
err: runErr,
|
||||
goArgs: strings.TrimSpace(normalizeNewlines(string(args))),
|
||||
goEnvironment: normalizeNewlines(string(environment)),
|
||||
}
|
||||
}
|
||||
|
||||
// runFmtCheck runs scripts/ci/fmt-check.sh against a gofmt shim with the given
|
||||
// body, and returns the shim path fmt-check.sh is expected to report.
|
||||
//
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue