fix(ci): require direct workflow build tags (#5679)

* fix(ci): require direct workflow build tags

Parse workflow run scalars structurally and require recognizable direct Go commands to declare the literal gms_pure_go tag. Keep shell and Make checks lightweight and preserve their existing conventions.

Fixes #5527

Agent-Signature: codex-gpt-5-unknown-reasoning on behalf of Ewen Cuthiell
Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>

* fix(ci): recognize double-dash Go build tag flags

Address PR #5679 review 5191180453. Preserve last-tag precedence and test argument boundaries, including --args.

Agent-Signature: codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell

* fix(ci): bazel-sync the new checker, restore its opt-out and the Bazel census

Maintainer review fixes for PR #5679, adoption iterations 1 and 2. Iteration
1's synthesis is `request_changes` on one major plus three minors; all four
are addressed. Iteration 2 approves with one minor, also addressed.

Major (Release Safety): the PR adds two production Go packages with no
BUILD.bazel, so `bazel build //...` could not see them and the repo's own
"BUILD files in sync" step fails on the resulting gazelle drift. Added the two
generated files via `make bazel-sync`; yaml.v3 is already a direct go.mod
requirement, so no module change is owed.

Minor (Release Safety): the new checker runs in the required pr-policy lane with
no way out, while the shell checker it partly replaces honors
`# build-tags: allow-bare` in a file's first five lines (check-build-tags.sh, and
engdocs/ICU-POLICY.md documents that window). CheckDir now honors the same
marker in the same window -- one convention, not two -- pinned by a table that
includes the fifth line and the sixth, so the two checkers cannot drift apart on
where the window ends.

Minor (Change Impact): dropping the workflow glob from `candidates` moved
workflows to the Go checker but also, silently, out of the `bazel_users` census --
and the Go checker does no Bazel handling at all, so .bazelrc could have lost its
tag with this gate still green. Workflows now feed the census through their own
`git ls-files` list and a separate loop, deliberately not rejoining the `go`-verb
scan. This is not hypothetical: `.github/workflows/bazel.yml` matches the Bazel
invocation regex, so it is exactly the file the census was losing.

Minor (Test Evidence): `Line` is the operator-facing half of a violation -- it is
what the error message points at -- and nothing asserted it, so reporting every
violation at line 1 would have been invisible. Added a `wantLine` column, asserted
on the multi-line "source is not workflow authority" case (line 2), and asserted
`Path`/`Job` where CheckDir fills them in.

One deviation, recorded: the finding also asked for `Line` on the existing
"continuation" case. That case is tagged, so it reports no violation and has no
Line to assert. Added a continuing-but-violating case instead, which makes the
same attribution observable -- a logical line spanning three physical ones is
reported at the first.

Not included: `make bazel-sync` also wants `//internal/debug` in
internal/utils/BUILD.bazel. That is base drift, not PR content -- absent at this
branch's rebase base but already present on origin/main, 24 commits ahead -- so it
heals on the next base refresh, and carrying it would duplicate main's own fix on
a file this PR never touches.

Minor (Test Evidence, iteration 2): the census half of the Change Impact fix
was right in code but pinned by nothing -- turning the workflow `bazel_users`
append into a no-op, or feeding the census-only list into the `go`-verb loop,
left every check_build_tags_test.go case green. Two cases on the existing
fixture harness now pin both directions: a workflow that runs `bazel build`
with an untagged .bazelrc fails and is named as the Bazel user, and a workflow
`run: go test ./...` passes here because scripts/checkworkflowtags owns it.
Each fails under its mutant, as does one more apiece (re-adding the workflow
glob to `candidates`; dropping `*.yml` from the census list), while the three
existing cases stay green under all four.

Validation: ./scripts/workflowtags passes (all four CheckDir tests and the full
CheckRun table), `go vet ./scripts/...` clean, `bash -n` clean, and both checkers
run green against the repository itself -- check-build-tags.sh reports 90 files
scanned all clear, and checkworkflowtags reports all direct commands clear.
For iteration 2, `go test ./scripts/` passes, and on a merge with current main
`bazel test //scripts:scripts_test` passes too; on this branch's own base that
target cannot build, because of the internal/utils drift noted above.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ci): pin main.yml's bd-cmd-test build to the literal gms_pure_go tag

#6940 pinned main.yml build-artifacts' non-race bd-cmd-test build as
`go test -tags "$BEADS_BUILD_TAGS" -c -o artifacts/bd-cmd-test ./cmd/bd`.
This PR requires a literal gms_pure_go tag on direct workflow Go commands
and rewrites that line to `go test -tags gms_pure_go -c ...`, so on
current main TestBazelDoltServerTiersMirrorPRRisk fails in both
`go test ./scripts` and //scripts:scripts_test. Restoring the pinned
spelling is not an option: checkworkflowtags rejects it ("direct `go test`
must declare literal gms_pure_go in -tags").

Update the pin to the literal spelling. It still pins what #6940 cares
about, a non-race bd-cmd-test built from ./cmd/bd: adding -race to that
line still fails the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Signed-off-by: Ewen Cuthiell <ewencuthiell@gmail.com>
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ecuthiell 2026-10-01 02:24:03 -06:00 • committed by GitHub
commit 46f182aa26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 666 additions and 21 deletions

View file

@ -134,7 +134,7 @@ jobs:
# -timeout=30m matches the macOS lanes this measures (pr.yml, main.yml).
# This is the job that produced the ./cmd/bd duration distribution, and
# at 10m it clips its own longest samples into timeout panics.
ci_time "macos short go test" -- go test -v -race -short -timeout=30m -skip '^TestEmbedded' ./...
ci_time "macos short go test" -- go test -tags=gms_pure_go -v -race -short -timeout=30m -skip '^TestEmbedded' ./...
macos-candidates:
name: Measure macOS no-short candidates
@ -499,7 +499,7 @@ jobs:
source scripts/ci/lib/timing.sh
source ./.buildflags
ci_time "build cmd/bd subprocess binary" -- go build -tags "$BEADS_BUILD_TAGS" -o artifacts/bd ./cmd/bd
ci_time "build cmd/bd subprocess binary" -- go build -tags gms_pure_go -o artifacts/bd ./cmd/bd
- name: Upload prebuilt bd
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
@ -667,7 +667,7 @@ jobs:
source scripts/ci/lib/timing.sh
source ./.buildflags
ci_time "build candidate binary" -- go build -o /tmp/bd-candidate ./cmd/bd
ci_time "build candidate binary" -- go build -tags gms_pure_go -o /tmp/bd-candidate ./cmd/bd
export CANDIDATE_BIN=/tmp/bd-candidate
if [[ -z "$SMOKE_VERSION" ]]; then
SMOKE_VERSION="$(gh release list --repo "$GITHUB_REPOSITORY" --limit 1 --json tagName --jq '.[0].tagName')"
@ -763,4 +763,3 @@ jobs:
path: artifacts/
retention-days: 7
if-no-files-found: ignore

View file

@ -93,9 +93,9 @@ jobs:
source ./.buildflags
go_version="$(go version)"
commit="$(git rev-parse HEAD)"
go build -tags "$BEADS_BUILD_TAGS" -o artifacts/bd-linux-gms-pure ./cmd/bd
go build -tags gms_pure_go -o artifacts/bd-linux-gms-pure ./cmd/bd
chmod +x artifacts/bd-linux-gms-pure
go test -tags "$BEADS_BUILD_TAGS" -c -o artifacts/bd-cmd-test ./cmd/bd
go test -tags gms_pure_go -c -o artifacts/bd-cmd-test ./cmd/bd
chmod +x artifacts/bd-cmd-test
(
cd artifacts
@ -130,9 +130,8 @@ jobs:
path: ${{ runner.temp }}/go-cache/non-race
key: beads-go-build-v2-${{ runner.os }}-${{ runner.arch }}-go-${{ steps.setup-go.outputs.go-version }}-base-gms_pure_go-non-race-${{ github.sha }}
# Fast check: every `go build|test|run|generate|install` invocation in
# tracked scripts/hooks/CI carries -tags=gms_pure_go. Prevents ICU-linkage
# regressions from re-entering the build (see engdocs/ICU-POLICY.md).
# Fast shell/Make guard for direct source-time build-tag declarations.
# Workflow `run` steps are checked structurally in the required policy wrapper.
check-build-tags:
name: Check build-tag policy
runs-on: ubuntu-latest

View file

@ -56,7 +56,7 @@ jobs:
source ./.buildflags
go_version="$(go version)"
commit="$(git rev-parse HEAD)"
go build -tags "$BEADS_BUILD_TAGS" -o artifacts/bd-linux-gms-pure ./cmd/bd
go build -tags gms_pure_go -o artifacts/bd-linux-gms-pure ./cmd/bd
chmod +x artifacts/bd-linux-gms-pure
(
cd artifacts
@ -154,9 +154,8 @@ jobs:
run: |
echo "::warning title=Example build check skipped::checkout or Go setup failed, so examples/ could not be checked this run (checkout: ${{ steps.checkout.outcome }}, setup-go: ${{ steps.setup-go.outcome }}). Advisory only; this does not block the merge."
# Fast check: every `go build|test|run|generate|install` invocation in
# tracked scripts/hooks/CI carries -tags=gms_pure_go. Prevents ICU-linkage
# regressions from re-entering the build (see engdocs/ICU-POLICY.md).
# Fast shell/Make guard for direct source-time build-tag declarations.
# Workflow `run` steps are checked structurally in the required policy wrapper.
check-build-tags:
name: Check build-tag policy
runs-on: ubuntu-latest

View file

@ -86,7 +86,7 @@ passes the tag inline (`-tags gms_pure_go`).
CI runs `scripts/check-build-tags.sh` on every PR (see the `Check build-tag
policy` job in `.github/workflows/pr.yml`). It fails if any tracked shell script,
CI workflow, git hook, or the Makefile contains a
git hook, or the Makefile contains a
`go build|test|run|generate|install` invocation that:
- does not carry `-tags=...gms_pure_go`, AND
@ -95,8 +95,19 @@ CI workflow, git hook, or the Makefile contains a
value contains `gms_pure_go`, AND
- is not a third-party tool install (`go install X@version` / `go run X@version`).
This is the source-time companion to `scripts/verify-cgo.sh` (runtime).
Between the two, an ICU regression cannot reach a release binary.
The required policy wrapper separately parses GitHub Actions YAML and checks
the first recognizable direct first-party Go command on each logical line in
`jobs.*.steps[*].run`. Each such command declares the literal `gms_pure_go` tag
in its own `-tags` argument;
sources and variables elsewhere in a workflow do not establish that source
convention. This is deliberately not shell control-flow analysis: dynamic
payloads, later compound-command segments, command substitutions, and
sourced-script internals are outside the check's contract. The pinned-tool
exception covers the repository's simple `go install path@version` and
`go run path@version` source forms.
These source-time guards complement `scripts/verify-cgo.sh`, which checks the
release binary at runtime.
To intentionally opt a file out (e.g. because it tests the ICU path),
add `# build-tags: allow-bare` within the first five lines of the file.

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash
# check-build-tags.sh — source-time guard for ICU regression.
#
# Scans tracked scripts, CI workflows, and git hooks. Fails when a
# Scans tracked shell scripts, git hooks, and the Makefile. Fails when a
# `go build|test|run|generate|install` invocation neither:
# (a) carries -tags=...gms_pure_go itself, nor
# (b) appears in a file that sources .buildflags beforehand, nor
@ -12,6 +12,9 @@
# scanned file invokes Bazel, the script instead requires .bazelrc to set
# `--@rules_go//go/config:tags=...gms_pure_go` for build (and so test/run).
#
# GitHub Actions `run` steps are no longer scanned here; they are checked
# structurally by scripts/checkworkflowtags.
#
# This is the source-time companion to scripts/verify-cgo.sh (which is a
# runtime check on release binaries). See engdocs/ICU-POLICY.md.
@ -21,17 +24,29 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$REPO_ROOT"
# Candidate files: shell scripts, workflows, git hooks, the Makefile.
# Candidate files: shell scripts, git hooks, and the Makefile. Workflow `run`
# steps are checked structurally by scripts/checkworkflowtags, so they are no
# longer scanned for bare `go` commands here.
mapfile -t candidates < <(
git ls-files \
'*.sh' \
'.github/workflows/*.yml' \
'.github/workflows/*.yaml' \
'.github/scripts/*' \
'.githooks/*' \
'Makefile' 2>/dev/null || true
)
# ...but the Bazel census below is a separate question, and workflows are where
# most `bazel` invocations live. Moving workflows to the Go checker dropped them
# from `bazel_users` as a side effect, which would let .bazelrc lose its tag with
# this gate still green — and scripts/checkworkflowtags does no Bazel handling at
# all. So workflows keep feeding the census through their own list. Kept separate
# from `candidates` on purpose: this list must NOT re-enter the `go`-verb scan.
mapfile -t bazel_census_only < <(
git ls-files \
'.github/workflows/*.yml' \
'.github/workflows/*.yaml' 2>/dev/null || true
)
# Files that intentionally opt out of the policy.
opt_out_regex='^(scripts/test-cgo\.sh|scripts/test-icu-path\.sh|scripts/check-build-tags\.sh|examples/)'
@ -165,6 +180,16 @@ for f in "${candidates[@]}"; do
done < <(grep -n -E '\bgo[[:space:]]+(build|test|run|generate|install)\b' "$f" 2>/dev/null || true)
done
# Workflows contribute to the Bazel census only. Deliberately a separate loop
# rather than another arm in the one above: nothing here may reach the `go`-verb
# scan, which scripts/checkworkflowtags now owns for these files.
for f in "${bazel_census_only[@]}"; do
[[ -f "$f" ]] || continue
if grep -Eq "$bazel_invocation_regex" "$f" 2>/dev/null; then
bazel_users+=("$f")
fi
done
# Bazel: every `bazel` invocation reads the committed .bazelrc, so the tag
# must be set there once. Accept `build` or `common` (test/run inherit build).
bazelrc_tag_regex='^[[:space:]]*(build|common)[[:space:]]+(.*[[:space:]])?--@(@)?rules_go//go/config:tags=([^[:space:]]*,)?gms_pure_go(,[^[:space:]]*)?([[:space:]]|$)'

View file

@ -101,3 +101,38 @@ func TestCheckBuildTagsRequiresBazelrcTagForBazelUsers(t *testing.T) {
t.Fatalf(".bazelrc without gms_pure_go was accepted:\n%s", out)
}
}
// Workflows are not scanned for `go` commands here, but they still feed the
// Bazel census: a workflow that invokes Bazel requires the .bazelrc tag and is
// named as the reason.
func TestCheckBuildTagsCountsWorkflowBazelUsers(t *testing.T) {
out, err := runCheckBuildTags(t, map[string]string{
".github/workflows/ci.yml": "jobs:\n" +
" build:\n" +
" runs-on: ubuntu-latest\n" +
" steps:\n" +
" - run: bazel build //...\n",
".bazelrc": "build --incompatible_strict_action_env\n",
})
if err == nil {
t.Fatalf(".bazelrc without gms_pure_go was accepted for a workflow Bazel user:\n%s", out)
}
if !strings.Contains(out, ".github/workflows/ci.yml") {
t.Errorf("expected .github/workflows/ci.yml to be reported as a Bazel user, got:\n%s", out)
}
}
// Workflow `run` steps are checked structurally by scripts/checkworkflowtags;
// the line scan here must not police them as well.
func TestCheckBuildTagsSkipsWorkflowGoCommands(t *testing.T) {
out, err := runCheckBuildTags(t, map[string]string{
".github/workflows/ci.yml": "jobs:\n" +
" test:\n" +
" runs-on: ubuntu-latest\n" +
" steps:\n" +
" - run: go test ./...\n",
})
if err != nil {
t.Fatalf("a workflow go command was scanned by check-build-tags.sh: %v\n%s", err, out)
}
}

View file

@ -0,0 +1,15 @@
load("@rules_go//go:def.bzl", "go_binary", "go_library")
go_library(
name = "checkworkflowtags_lib",
srcs = ["main.go"],
importpath = "github.com/steveyegge/beads/scripts/checkworkflowtags",
visibility = ["//visibility:private"],
deps = ["//scripts/workflowtags"],
)
go_binary(
name = "checkworkflowtags",
embed = [":checkworkflowtags_lib"],
visibility = ["//visibility:public"],
)

View file

@ -0,0 +1,24 @@
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/steveyegge/beads/scripts/workflowtags"
)
func main() {
violations, err := workflowtags.CheckDir(filepath.Join(".github", "workflows"))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
for _, violation := range violations {
fmt.Fprintf(os.Stderr, "%s job %q step %q run line %d: %s\n", violation.Path, violation.Job, violation.Step, violation.Line, violation.Message)
}
if len(violations) != 0 {
os.Exit(1)
}
fmt.Println("workflow build-tag declarations: all direct commands clear")
}

View file

@ -103,6 +103,8 @@ build_docs_binary() {
}
ci_time "check build-tag policy" -- ./scripts/check-build-tags.sh
ci_time "check workflow build-tag declarations" -- \
go run -tags=gms_pure_go ./scripts/checkworkflowtags
ci_time "check go install guidance" -- ./scripts/check-go-install-guidance.sh
ci_time "check version consistency" -- ./scripts/check-versions.sh
ci_time "build bd for docs checks" -- build_docs_binary

View file

@ -3183,7 +3183,7 @@ func TestBazelDoltServerTiersMirrorPRRisk(t *testing.T) {
// is not race: bd_proxied_test (race, like PR Risk's) is the stricter of
// the two. Pinned so a change there is a decision, not drift.
mainYML := readCIWorkflow(t, "main.yml")
if run := mainYML.job(t, "build-artifacts").step(t, "Build reusable Linux artifacts").Run; !strings.Contains(run, `go test -tags "$BEADS_BUILD_TAGS" -c -o artifacts/bd-cmd-test ./cmd/bd`+"\n") {
if run := mainYML.job(t, "build-artifacts").step(t, "Build reusable Linux artifacts").Run; !strings.Contains(run, `go test -tags gms_pure_go -c -o artifacts/bd-cmd-test ./cmd/bd`+"\n") {
t.Errorf("main.yml build-artifacts no longer builds the non-race bd-cmd-test this tier is documented against (.bazelrc, cmd/bd:bd_proxied_test):\n%s", run)
}
if got := mainYML.job(t, "test-proxied-cmd").step(t, "Test proxied-server cmd shard").Env["BEADS_TEST_CMD_BINARY"]; got != "${{ github.workspace }}/ci-build-artifacts/bd-cmd-test" {

View file

@ -0,0 +1,15 @@
load("@rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "workflowtags",
srcs = ["workflowtags.go"],
importpath = "github.com/steveyegge/beads/scripts/workflowtags",
visibility = ["//visibility:public"],
deps = ["@in_gopkg_yaml_v3//:yaml_v3"],
)
go_test(
name = "workflowtags_test",
srcs = ["workflowtags_test.go"],
embed = [":workflowtags"],
)

View file

@ -0,0 +1,317 @@
// Package workflowtags checks the repository's source convention for direct Go
// commands in GitHub Actions run steps. It recognizes only the first simple
// command on each logical line and deliberately does not interpret arbitrary
// shell control flow, substitutions, or sourced scripts.
package workflowtags
import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"unicode"
"gopkg.in/yaml.v3"
)
var assignment = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*=`)
// Violation identifies a direct workflow Go command missing the required tag.
type Violation struct {
Path string
Job string
Step string
Line int
Message string
}
// CheckDir checks every workflow YAML file immediately under dir.
func CheckDir(dir string) ([]Violation, error) {
paths, err := filepath.Glob(filepath.Join(dir, "*.y*ml"))
if err != nil {
return nil, err
}
if len(paths) == 0 {
return nil, fmt.Errorf("no GitHub Actions workflows found under %s", dir)
}
sort.Strings(paths)
var violations []Violation
for _, path := range paths {
// #nosec G304 -- path is emitted by the bounded workflow glob above.
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if allowsBareCommands(data) {
continue
}
var workflow workflow
if err := yaml.Unmarshal(data, &workflow); err != nil {
return nil, fmt.Errorf("parse %s: %w", path, err)
}
jobNames := make([]string, 0, len(workflow.Jobs))
for jobName := range workflow.Jobs {
jobNames = append(jobNames, jobName)
}
sort.Strings(jobNames)
for _, jobName := range jobNames {
job := workflow.Jobs[jobName]
for stepIndex, step := range job.Steps {
stepName := step.Name
if stepName == "" {
stepName = fmt.Sprintf("step %d", stepIndex+1)
}
for _, runViolation := range CheckRun(step.Run) {
runViolation.Path = path
runViolation.Job = jobName
runViolation.Step = stepName
violations = append(violations, runViolation)
}
}
}
}
return violations, nil
}
type workflow struct {
Jobs map[string]workflowJob `yaml:"jobs"`
}
type workflowJob struct {
Steps []workflowStep `yaml:"steps"`
}
type workflowStep struct {
Name string `yaml:"name"`
Run string `yaml:"run"`
}
// allowsBareCommands reports whether a workflow opts out of this check with the
// repository's existing marker in its first five lines.
//
// This check runs in the required pr-policy lane, so without an opt-out a
// workflow that legitimately needs a bare `go` command has no way through. The
// marker and its five-line window are not invented here: scripts/check-build-tags.sh
// already honors exactly `^# build-tags: allow-bare` within `head -n 5`, and
// engdocs/ICU-POLICY.md documents it as the file-level escape. Matching that
// spelling and that window keeps one convention rather than two, so an operator
// who learned the shell checker's marker finds it works on workflows too.
func allowsBareCommands(data []byte) bool {
for i, line := range strings.Split(string(data), "\n") {
if i >= 5 {
return false
}
if strings.HasPrefix(line, "# build-tags: allow-bare") {
return true
}
}
return false
}
// CheckRun checks recognizable direct commands in one decoded run scalar.
func CheckRun(run string) []Violation {
lines := strings.Split(strings.ReplaceAll(run, "\r\n", "\n"), "\n")
var violations []Violation
for i := 0; i < len(lines); i++ {
lineNumber := i + 1
logical := lines[i]
for trailingUnquotedBackslash(logical) && i+1 < len(lines) {
logical = strings.TrimSpace(strings.TrimSuffix(strings.TrimRightFunc(logical, unicode.IsSpace), "\\")) + " " + strings.TrimSpace(lines[i+1])
i++
}
words, err := tokenize(logical)
if err != nil {
// Malformed or complex shell syntax is outside this bounded source check.
continue
}
goArgs, ok := directGoArgs(words)
if !ok || exemptVersionedTool(goArgs) || hasRequiredTag(goArgs) {
continue
}
violations = append(violations, Violation{
Line: lineNumber,
Message: fmt.Sprintf("direct `go %s` must declare literal gms_pure_go in -tags", goArgs[0]),
})
}
return violations
}
func directGoArgs(words []string) ([]string, bool) {
words = skipEnvPrefix(words)
if len(words) > 0 && words[0] == "ci_time" {
separator := -1
for i, word := range words[1:] {
if word == "--" {
separator = i + 1
break
}
}
if separator < 0 {
return nil, false
}
words = skipEnvPrefix(words[separator+1:])
}
if len(words) < 2 || words[0] != "go" {
return nil, false
}
switch words[1] {
case "build", "test", "run", "generate", "install":
return words[1:], true
default:
return nil, false
}
}
func skipEnvPrefix(words []string) []string {
if len(words) > 0 && words[0] == "env" {
words = words[1:]
for len(words) > 0 {
switch {
case words[0] == "--":
words = words[1:]
goto assignments
case words[0] == "-i" || words[0] == "--ignore-environment":
words = words[1:]
case words[0] == "-u" || words[0] == "--unset" || words[0] == "-C" || words[0] == "--chdir":
if len(words) < 2 {
return nil
}
words = words[2:]
case strings.HasPrefix(words[0], "--unset=") || strings.HasPrefix(words[0], "--chdir="):
words = words[1:]
default:
goto assignments
}
}
}
assignments:
for len(words) > 0 && assignment.MatchString(words[0]) {
words = words[1:]
}
return words
}
func hasRequiredTag(goArgs []string) bool {
lastTags := ""
found := false
for i := 1; i < len(goArgs); i++ {
arg := goArgs[i]
if arg == "--" || (goArgs[0] == "test" && (arg == "-args" || arg == "--args")) {
break
}
switch {
case arg == "-tags" || arg == "--tags":
if i+1 >= len(goArgs) {
lastTags = ""
found = true
break
}
i++
lastTags = goArgs[i]
found = true
case strings.HasPrefix(arg, "-tags=") || strings.HasPrefix(arg, "--tags="):
_, lastTags, _ = strings.Cut(arg, "=")
found = true
}
}
if !found {
return false
}
for _, tag := range strings.FieldsFunc(lastTags, func(r rune) bool { return r == ',' || unicode.IsSpace(r) }) {
if tag == "gms_pure_go" {
return true
}
}
return false
}
// The repository's pinned-tool convention is intentionally narrow: unflagged
// external package operands with versions. For go run, later words are program
// arguments; for go install, every package operand must qualify.
func exemptVersionedTool(goArgs []string) bool {
if len(goArgs) < 2 || (goArgs[0] != "run" && goArgs[0] != "install") {
return false
}
packages := goArgs[1:]
if goArgs[0] == "run" {
packages = packages[:1]
}
for _, pkg := range packages {
at := strings.LastIndexByte(pkg, '@')
if at <= 0 || at == len(pkg)-1 || strings.HasPrefix(pkg, ".") || strings.HasPrefix(pkg, "/") || !strings.Contains(pkg[:at], "/") {
return false
}
}
return true
}
func trailingUnquotedBackslash(line string) bool {
trimmed := strings.TrimRightFunc(line, unicode.IsSpace)
if !strings.HasSuffix(trimmed, "\\") {
return false
}
backslashes := 0
for i := len(trimmed) - 1; i >= 0 && trimmed[i] == '\\'; i-- {
backslashes++
}
return backslashes%2 == 1
}
// tokenize covers only the simple command source forms owned by this check.
// At the first unquoted shell operator it returns the first simple command.
func tokenize(line string) ([]string, error) {
var words []string
var word strings.Builder
var quote rune
escaped := false
flush := func() {
if word.Len() > 0 {
words = append(words, word.String())
word.Reset()
}
}
for _, r := range strings.TrimSpace(line) {
if escaped {
word.WriteRune(r)
escaped = false
continue
}
if quote != 0 {
if r == quote {
quote = 0
} else if r == '\\' && quote == '"' {
escaped = true
} else {
word.WriteRune(r)
}
continue
}
switch {
case r == '\\':
escaped = true
case r == '\'' || r == '"':
quote = r
case unicode.IsSpace(r):
flush()
case r == '#' && word.Len() == 0:
return words, nil
case r == '#':
word.WriteRune(r)
case r == ';' || r == '|' || r == '&':
flush()
return words, nil
default:
word.WriteRune(r)
}
}
if escaped || quote != 0 {
return nil, fmt.Errorf("unterminated quote or escape")
}
flush()
return words, nil
}

View file

@ -0,0 +1,204 @@
package workflowtags
import (
"os"
"path/filepath"
"testing"
)
func TestCheckRun(t *testing.T) {
tests := []struct {
name string
run string
violations int
// wantLine, when non-zero, pins the reported physical line of a single
// violation. The Line field is the operator-facing half of this check —
// it is what the error message points at — and nothing asserted it, so a
// regression that reported every violation at line 1 would have been
// invisible. Only meaningful on multi-line runs.
wantLine int
}{
{name: "direct tagged", run: `go test -tags=gms_pure_go ./...`},
{name: "double dash tagged", run: `go build --tags=gms_pure_go ./...`},
{name: "double dash tag operand", run: `go test --tags "netgo,gms_pure_go" ./...`},
{name: "assignment prefix", run: `CGO_ENABLED=0 go build -tags "netgo,gms_pure_go" ./cmd/bd`},
{name: "env prefix", run: `env CGO_ENABLED=0 go test '-tags=integration,gms_pure_go' ./...`},
{name: "env unset", run: `env -u GOFLAGS go test ./...`, violations: 1},
{name: "env long unset operand", run: `env --unset GOFLAGS go test ./...`, violations: 1},
{name: "env long unset", run: `env --unset=GOFLAGS go test ./...`, violations: 1},
{name: "env chdir", run: `env -C /tmp go build ./cmd/bd`, violations: 1},
{name: "env long chdir", run: `env --chdir /tmp go build ./cmd/bd`, violations: 1},
{name: "env ignore environment tagged", run: `env -i go test -tags=gms_pure_go ./...`},
{name: "env double dash", run: `env -- go test ./...`, violations: 1},
{name: "timed command", run: `ci_time "label mentions go test" -- go test -tags gms_pure_go ./...`},
{name: "quoted hash", run: `go test -tags=gms_pure_go -run 'Test#Case' ./...`},
{name: "continuation", run: "go test \\\n -tags=gms_pure_go \\\n ./..."},
{name: "pinned install", run: `go install example.com/tool@v1.2.3`},
{name: "multiple pinned installs", run: `go install example.com/tool@v1.2.3 example.com/other@latest`},
{name: "pinned run", run: `go run example.com/tool@latest`},
{name: "pinned run arguments", run: `go run example.com/tool@latest --help`},
{name: "pinned tool with flags is outside exemption", run: `go install -pgo off example.com/tool@v1.2.3`, violations: 1},
{name: "bare", run: `go test ./...`, violations: 1},
{name: "source is not workflow authority", run: "source ./.buildflags\ngo test ./...", violations: 1, wantLine: 2},
// The continuation case above is tagged, so it reports no violation and
// cannot show which physical line a continued command is attributed to.
// This is the same shape that does violate, so the attribution is
// observable: a logical line spanning three physical ones must be
// reported at the first.
{name: "continuation reports the first physical line", run: "go test \\\n -race \\\n ./...", violations: 1, wantLine: 1},
{name: "variable tag", run: `go build -tags "$BEADS_BUILD_TAGS" ./cmd/bd`, violations: 1},
{name: "double dash variable tag", run: `go build --tags="$BEADS_BUILD_TAGS" ./cmd/bd`, violations: 1},
{name: "tag substring prefix", run: `go test -tags=notgms_pure_go ./...`, violations: 1},
{name: "tag substring suffix", run: `go test -tags=gms_pure_go_extra ./...`, violations: 1},
{name: "last tag wins negative", run: `go test -tags=gms_pure_go -tags=integration ./...`, violations: 1},
{name: "last tag wins positive", run: `go test -tags=integration -tags=gms_pure_go ./...`},
{name: "last double dash tag wins negative", run: `go test -tags=gms_pure_go --tags=integration ./...`, violations: 1},
{name: "last double dash tag wins positive", run: `go test -tags=integration --tags=gms_pure_go ./...`},
{name: "last single dash tag wins negative", run: `go test --tags=gms_pure_go -tags=integration ./...`, violations: 1},
{name: "last single dash tag wins positive", run: `go test --tags=integration -tags=gms_pure_go ./...`},
{name: "empty double dash tag overrides", run: `go test -tags=gms_pure_go --tags= ./...`, violations: 1},
{name: "missing double dash tag overrides", run: `go test -tags=gms_pure_go --tags`, violations: 1},
{name: "tag after separator", run: `go test ./... -- -tags=gms_pure_go`, violations: 1},
{name: "double dash tag after separator", run: `go test ./... -- --tags=gms_pure_go`, violations: 1},
{name: "tag after test args", run: `go test ./... -args -tags=gms_pure_go`, violations: 1},
{name: "double dash tag after test args", run: `go test ./... -args --tags=gms_pure_go`, violations: 1},
{name: "tag after double dash test args", run: `go test ./... --args -tags=gms_pure_go`, violations: 1},
{name: "double dash tag after double dash test args", run: `go test ./... --args --tags=gms_pure_go`, violations: 1},
{name: "tag before double dash test args", run: `go test --tags=gms_pure_go ./... --args --tags=integration`},
{name: "missing tag value", run: `go test -tags ./...`, violations: 1},
{name: "unrelated version text", run: `go run ./cmd/bd --label @v1`, violations: 1},
{name: "unquoted hash is argument text", run: `go test -run Test#Case -tags=gms_pure_go ./...`},
{name: "compound suffix outside bounded command", run: `go test -tags=gms_pure_go ./... && go test ./...`},
{name: "compound suffix cannot authorize first command", run: `go test ./... && go test -tags=gms_pure_go ./...`, violations: 1},
{name: "dynamic shell excluded", run: `bash -c 'go test ./...'`},
{name: "eval excluded", run: `eval 'go test ./...'`},
{name: "substitution excluded", run: `out="$(go test ./...)"`},
{name: "wrapper excluded", run: `gotestsum -- -tags=gms_pure_go ./...`},
{name: "comment ignored", run: `# explain go test behavior`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := CheckRun(tt.run)
if len(got) != tt.violations {
t.Fatalf("got %d violation(s), want %d: %#v", len(got), tt.violations, got)
}
if tt.wantLine != 0 {
if len(got) != 1 {
t.Fatalf("wantLine needs exactly one violation, got %d: %#v", len(got), got)
}
if got[0].Line != tt.wantLine {
t.Errorf("violation line = %d, want %d: %#v", got[0].Line, tt.wantLine, got[0])
}
}
})
}
}
func TestCheckDirParsesOnlyRunScalars(t *testing.T) {
dir := t.TempDir()
fixture := `name: Explain go test behavior
jobs:
check:
steps:
- name: Inline
run: go test -tags=gms_pure_go ./...
- name: Literal
run: |
go build -tags gms_pure_go ./cmd/bd
- name: Folded
run: >-
go test
-tags=gms_pure_go
./...
`
if err := os.WriteFile(filepath.Join(dir, "fixture.yml"), []byte(fixture), 0o600); err != nil {
t.Fatal(err)
}
violations, err := CheckDir(dir)
if err != nil {
t.Fatal(err)
}
if len(violations) != 0 {
t.Fatalf("unexpected violations: %#v", violations)
}
}
func TestCheckDirRejectsCrossStepSourceAuthority(t *testing.T) {
dir := t.TempDir()
fixture := `jobs:
check:
steps:
- name: Source flags
run: source ./.buildflags
- name: Bare command
run: go test ./...
`
if err := os.WriteFile(filepath.Join(dir, "fixture.yml"), []byte(fixture), 0o600); err != nil {
t.Fatal(err)
}
violations, err := CheckDir(dir)
if err != nil {
t.Fatal(err)
}
if len(violations) != 1 || violations[0].Step != "Bare command" {
t.Fatalf("violations = %#v, want one in Bare command", violations)
}
// Path and Job are the rest of the operator-facing attribution: the error
// line names all three, and CheckDir is the only thing that fills them in
// (CheckRun leaves them empty), so they belong to this test.
if want := filepath.Join(dir, "fixture.yml"); violations[0].Path != want {
t.Errorf("violation path = %q, want %q", violations[0].Path, want)
}
if violations[0].Job != "check" {
t.Errorf("violation job = %q, want %q", violations[0].Job, "check")
}
}
func TestCheckDirRejectsEmptyInventory(t *testing.T) {
if _, err := CheckDir(t.TempDir()); err == nil {
t.Fatal("CheckDir succeeded without workflow files")
}
}
// TestCheckDirHonorsAllowBareMarker pins the file-level opt-out. This check runs
// in the required pr-policy lane, so a workflow that legitimately needs a bare
// `go` command needs a way through, and the repository already has one:
// scripts/check-build-tags.sh honors `# build-tags: allow-bare` within the first
// five lines, and engdocs/ICU-POLICY.md documents that window. The same spelling
// and the same window apply here rather than a second convention.
func TestCheckDirHonorsAllowBareMarker(t *testing.T) {
violating := `jobs:
check:
steps:
- name: Bare command
run: go test ./...
`
for _, tc := range []struct {
name string
prefix string
violations int
}{
{name: "no marker", prefix: "", violations: 1},
{name: "marker on the first line", prefix: "# build-tags: allow-bare\n"},
{name: "marker on the fifth line", prefix: "#\n#\n#\n#\n# build-tags: allow-bare\n"},
// Sixth line is outside the window the shell checker uses, so the
// opt-out must not apply -- otherwise the two checkers disagree about
// the same marker.
{name: "marker past the window", prefix: "#\n#\n#\n#\n#\n# build-tags: allow-bare\n", violations: 1},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "fixture.yml"), []byte(tc.prefix+violating), 0o600); err != nil {
t.Fatal(err)
}
violations, err := CheckDir(dir)
if err != nil {
t.Fatal(err)
}
if len(violations) != tc.violations {
t.Fatalf("got %d violation(s), want %d: %#v", len(violations), tc.violations, violations)
}
})
}
}