diff --git a/seaweed-block/.github/pull_request_template.md b/seaweed-block/.github/pull_request_template.md index bec2170b6d..25ad685875 100644 --- a/seaweed-block/.github/pull_request_template.md +++ b/seaweed-block/.github/pull_request_template.md @@ -14,6 +14,13 @@ Machine/INV starts with exactly one kind: text and rows land with or before the code. A fix without an invariant, or a guard term without a transition, is a CHANGE. +A PR that changes no behaviour uses one of these instead, checked against its files: + DOCS docs (not docs/spec), tests, and Go whose tokens are unchanged + (comments only; directive comments count as code). + TEST only tests, testdata and test helpers. + CI only .github/ (scripts/ ships install and deploy scripts). +DOCS may still change contract text (docs/design/invariants.md): the reviewer is the gate for that. + NONE needs a reason of at least 10 characters. Simplest is required when the PR changes non-test Go under core/ or cmd/. --> diff --git a/seaweed-block/.github/scripts/sametokens.go b/seaweed-block/.github/scripts/sametokens.go new file mode 100644 index 0000000000..c167f2429d --- /dev/null +++ b/seaweed-block/.github/scripts/sametokens.go @@ -0,0 +1,111 @@ +// Command sametokens reports whether two versions of a Go source file have the +// same tokens in the same order, ignoring comments that the compiler ignores. +// +// Usage: go run sametokens.go OLD NEW +// +// A missing file counts as empty. Directive comments are compared as tokens: +// //go:..., // +build, //line, /*line, //export; in a file that uses cgo +// (any "C" string literal, as in import "C"), every comment counts, since the +// preamble is code. Exit 0: same tokens; 1: different +// (the first difference is printed); 2: usage error or a file that does not scan. +package main + +import ( + "fmt" + "go/scanner" + "go/token" + "os" + "strings" +) + +type tok struct { + t token.Token + lit string +} + +func main() { + if len(os.Args) != 3 { + fmt.Fprintln(os.Stderr, "usage: sametokens OLD NEW") + os.Exit(2) + } + a, err := tokens(os.Args[1]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + b, err := tokens(os.Args[2]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + for i := 0; i < len(a) || i < len(b); i++ { + if i >= len(a) || i >= len(b) || a[i] != b[i] { + fmt.Printf("token %d differs: %v vs %v\n", i, at(a, i), at(b, i)) + os.Exit(1) + } + } +} + +func at(ts []tok, i int) string { + if i >= len(ts) { + return "end of file" + } + return fmt.Sprintf("%s %q", ts[i].t, ts[i].lit) +} + +// tokens scans a file and keeps every token except comments the compiler +// ignores. An automatic semicolon is compared by kind only. +func tokens(path string) ([]tok, error) { + src, err := os.ReadFile(path) + if os.IsNotExist(err) { + return nil, nil + } else if err != nil { + return nil, err + } + fset := token.NewFileSet() + file := fset.AddFile(path, -1, len(src)) + var s scanner.Scanner + var scanErr error + s.Init(file, src, func(pos token.Position, msg string) { + if scanErr == nil { + scanErr = fmt.Errorf("%s: %s", pos, msg) + } + }, scanner.ScanComments) + var all []tok + for { + _, t, lit := s.Scan() + if t == token.EOF { + break + } + if t == token.SEMICOLON { + lit = "" + } + all = append(all, tok{t, lit}) + } + if scanErr != nil { + return nil, scanErr + } + cgo := false + for _, x := range all { + if x.t == token.STRING && x.lit == "\"C\"" { + cgo = true + } + } + var out []tok + for _, x := range all { + if x.t == token.COMMENT && !cgo && !directive(x.lit) { + continue + } + out = append(out, x) + } + return out, nil +} + +func directive(c string) bool { + for _, p := range []string{"//go:", "// +build", "//+build", "//line ", "/*line ", "//export "} { + if strings.HasPrefix(c, p) { + return true + } + } + return false +} diff --git a/seaweed-block/.github/scripts/spec-trace-check-test.sh b/seaweed-block/.github/scripts/spec-trace-check-test.sh index 6b6128ff69..c5379d0ff5 100644 --- a/seaweed-block/.github/scripts/spec-trace-check-test.sh +++ b/seaweed-block/.github/scripts/spec-trace-check-test.sh @@ -80,5 +80,74 @@ expect_raw 2 "status cut off before its NUL" "M\0docs/a.md\0M" expect_raw 2 "unknown status" "R100\0core/a.go\0" expect_raw 0 "well-formed docs change" "M\0docs/a.md\0" + +# Kinds for PRs that change no behaviour: each is checked against its paths; +# DOCS also compares the old and new tokens of each changed non-test Go file. +# expect_kind WANT_EXIT NAME KIND OLD_GO NEW_GO [STATUS PATH]... +# OLD_GO/NEW_GO are core/x/s.go before and after ("-" = no blob for that side). +expect_kind() { + local want=$1 name=$2 k=$3 old=$4 new=$5 got=0 + shift 5 + printf '%s' "${ok/IMPL M5.Quiesce>Copy/$k} +$simplest" >"$tmp/body" + rm -rf "$tmp/blobs"; mkdir -p "$tmp/blobs/old/core/x" "$tmp/blobs/new/core/x" + [[ $old == - ]] || printf '%s' "$old" >"$tmp/blobs/old/core/x/s.go" + [[ $new == - ]] || printf '%s' "$new" >"$tmp/blobs/new/core/x/s.go" + if (($#)); then printf '%s\0' "$@"; fi | bash "$check" "$tmp/body" "$tmp/blobs" >/dev/null || got=$? + if [[ $got != "$want" ]]; then + echo "FAIL $name: exit $got, want $want" + fail=1 + fi +} +P=$'package x\n' +expect_kind 0 "DOCS: comment-only Go" DOCS "${P}var A = 1"$'\n' "${P}"$'// A is one.\nvar A = 1 // one\n' M docs/a.md M core/x/s.go A core/x/s_test.go +expect_kind 0 "DOCS: gofmt realignment" DOCS "${P}var A = 1"$'\n' "${P}var A = 1"$'\n' M core/x/s.go +expect_kind 0 "DOCS: block comment" DOCS "${P}var A = 1"$'\n' "${P}"$'/* doc */\nvar A = 1\n' M core/x/s.go +expect_kind 1 "DOCS: reorder" DOCS "${P}func f() { u(); p() }"$'\n' "${P}func f() { p(); u() }"$'\n' M core/x/s.go +expect_kind 1 "DOCS: build constraint comment" DOCS "$P" $'//go:build never\n\npackage x\n' M core/x/s.go +expect_kind 1 "DOCS: rune quote then // in a string" DOCS "${P}var _ = f('\"', \"http://a\")"$'\n' "${P}var _ = f('\"', \"http://b\")"$'\n' M core/x/s.go +expect_kind 1 "DOCS: line inside a raw string" DOCS "${P}var s = \`a"$'\n'"curl http://old | sh"$'\n'"\`"$'\n' "${P}var s = \`a"$'\n'"curl http://new | sh"$'\n'"\`"$'\n' M core/x/s.go +expect_kind 1 "DOCS: spaces inside a string" DOCS "${P}var s = \"a b\""$'\n' "${P}var s = \"a b\""$'\n' M core/x/s.go +expect_kind 1 "DOCS: cgo preamble" DOCS "${P}// #include "$'\nimport "C"\n' "${P}// #include "$'\nimport "C"\n' M core/x/s.go +expect_kind 1 "DOCS: cgo preamble, grouped import" DOCS "${P}// #include "$'\nimport (\n\t"C"\n)\n' "${P}// #include "$'\nimport (\n\t"C"\n)\n' M core/x/s.go +expect_kind 1 "DOCS: Go changed, no blob" DOCS - - M core/x/s.go +expect_kind 1 "DOCS: a model under docs/spec" DOCS - - M docs/spec/X.tla +expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS - - M deploy/x.env +expect_kind 0 "TEST: tests and testdata" TEST - - M core/x/s_test.go A core/x/testdata/in.bin +expect_kind 1 "TEST: production Go" TEST - - M core/x/s.go +expect_kind 0 "CI: workflow" CI - - M .github/workflows/w.yml +expect_kind 1 "CI: scripts/ is shipped" CI - - A scripts/install.sh +expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" - - M docs/a.md + +[[ $fail == 0 ]] && echo "spec-trace self-test: ok" +exit $fail + +import ( + "C" +) +' "${P}// #include " DOCS - - M core/x/s.go +expect_kind 1 "DOCS: a model under docs/spec" DOCS - - M docs/spec/X.tla +expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS - - M deploy/x.env +expect_kind 0 "TEST: tests and testdata" TEST - - M core/x/s_test.go A core/x/testdata/in.bin +expect_kind 1 "TEST: production Go" TEST - - M core/x/s.go +expect_kind 0 "CI: workflow" CI - - M .github/workflows/w.yml +expect_kind 1 "CI: scripts/ is shipped" CI - - A scripts/install.sh +expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" - - M docs/a.md + +[[ $fail == 0 ]] && echo "spec-trace self-test: ok" +exit $fail + +import ( + "C" +) +' M core/x/s.go +expect_kind 1 "DOCS: a model under docs/spec" DOCS - - M docs/spec/X.tla +expect_kind 1 "DOCS: a non-docs, non-Go file" DOCS - - M deploy/x.env +expect_kind 0 "TEST: tests and testdata" TEST - - M core/x/s_test.go A core/x/testdata/in.bin +expect_kind 1 "TEST: production Go" TEST - - M core/x/s.go +expect_kind 0 "CI: workflow" CI - - M .github/workflows/w.yml +expect_kind 1 "CI: scripts/ is shipped" CI - - A scripts/install.sh +expect_kind 1 "DOCS next to another kind" "DOCS FIX INV-X-001" - - M docs/a.md + [[ $fail == 0 ]] && echo "spec-trace self-test: ok" exit $fail diff --git a/seaweed-block/.github/scripts/spec-trace-check.sh b/seaweed-block/.github/scripts/spec-trace-check.sh index dac909ef10..0021fa1a37 100644 --- a/seaweed-block/.github/scripts/spec-trace-check.sh +++ b/seaweed-block/.github/scripts/spec-trace-check.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Usage: spec-trace-check.sh BODY_FILE < changes +# Usage: spec-trace-check.sh BODY_FILE [BLOBS] < changes # # Checks the spec trace in a PR description. changes are NUL-separated # STATUS, PATH pairs (post-merge-review-changed.sh). @@ -8,7 +8,13 @@ # optional indentation, a list marker or bold) and carry a value that is not # a template placeholder; "NONE:" needs a reason of at least 10 characters. # - Machine/INV: starts with exactly one PR kind: "IMPL .", -# "FIX " or "CHANGE ". NONE is not a kind. +# "FIX ", "CHANGE ", or one of the kinds a PR that changes +# no behaviour may use, each checked against what it changes: +# DOCS (docs outside docs/spec/, tests, and non-test Go whose tokens are +# unchanged: BLOBS holds old/ and new/ for each changed non-test +# Go file, compared by sametokens.go), TEST (only tests, testdata and test +# helpers), CI (only .github/). NONE is not a kind. Known limit: a docs file +# that is go:embed-ed into a binary changes behaviour under DOCS (none today). # - Simplest: (20+ characters) is present when the PR adds, modifies or # deletes non-test Go under core/ or cmd/. # @@ -16,7 +22,9 @@ # usage error or a malformed change list. set -euo pipefail -body_file=${1:?usage: spec-trace-check.sh BODY_FILE < changes} +body_file=${1:?usage: spec-trace-check.sh BODY_FILE [BLOBS] < changes} +blobs=${2-} +here=$(cd "$(dirname "$0")" && pwd) [[ -f $body_file ]] || { echo "spec-trace: body file $body_file missing" >&2; exit 2; } body=$(tr -d '\r' <"$body_file") @@ -57,21 +65,32 @@ done # One kind token, ending at the line end, a space, a comma or a slash. end='($|[[:space:],/])' -kind_re="^(IMPL [A-Za-z0-9_]+[.][A-Za-z0-9_>.-]+|FIX INV-[A-Z0-9]+(-[A-Z0-9]+)*|CHANGE [A-Za-z0-9_]+)$end" +kind_re="^(IMPL [A-Za-z0-9_]+[.][A-Za-z0-9_>.-]+|FIX INV-[A-Z0-9]+(-[A-Z0-9]+)*|CHANGE [A-Za-z0-9_]+|DOCS|TEST|CI)$end" +kind="" mi=$(value Machine/INV:) if [[ -n $mi && $mi != '<'* ]]; then if [[ ! $mi =~ $kind_re ]]; then - problem "Machine/INV: must start with IMPL ., FIX or CHANGE " - elif [[ ${mi#"${BASH_REMATCH[0]}"} =~ (^|[^A-Za-z0-9_])(IMPL|FIX|CHANGE)([^A-Za-z0-9_]|$) ]]; then - problem "Machine/INV: names more than one kind; a PR is exactly one of IMPL, FIX or CHANGE" + problem "Machine/INV: must start with IMPL ., FIX , CHANGE , DOCS, TEST or CI" + else + token=${BASH_REMATCH[1]} + if [[ ${mi#"${BASH_REMATCH[0]}"} =~ (^|[^A-Za-z0-9_])(IMPL|FIX|CHANGE|DOCS|TEST|CI)([^A-Za-z0-9_]|$) ]]; then + problem "Machine/INV: names more than one kind; a PR is exactly one kind" + else + kind=${token%% *} + fi fi fi +is_test() { [[ $1 == *_test.go || $1 == testdata/* || $1 == */testdata/* || $1 == */testutil/* ]]; } +# docs/spec holds the models: changing one is not a docs change. +is_doc() { [[ $1 != docs/spec/* ]] && [[ $1 == *.md || $1 == docs/* ]]; } core=0 +paths=() while IFS= read -r -d '' status; do [[ $status == [ADMT] ]] || { echo "spec-trace: malformed change list: status ${status@Q}" >&2; exit 2; } IFS= read -r -d '' path || { echo "spec-trace: malformed change list: status $status without a path" >&2; exit 2; } [[ -n $path ]] || { echo "spec-trace: malformed change list: empty path" >&2; exit 2; } + paths+=("$path") if [[ $path == core/*.go || $path == cmd/*.go ]] && [[ $path != *_test.go ]]; then core=1 fi @@ -85,4 +104,55 @@ if ((core)); then fi fi +case $kind in +CI) + for p in ${paths[@]+"${paths[@]}"}; do + [[ $p == .github/* ]] || problem "kind CI: $p is outside .github/ (scripts/ holds shipped install and deploy scripts)" + done + ;; +TEST) + for p in ${paths[@]+"${paths[@]}"}; do + is_test "$p" || problem "kind TEST: $p is not a test, testdata or test helper" + done + ;; +DOCS) + gofiles=() + for p in ${paths[@]+"${paths[@]}"}; do + if is_doc "$p" || is_test "$p"; then continue; fi + if [[ $p == *.go ]]; then gofiles+=("$p"); continue; fi + problem "kind DOCS: $p is neither docs, a test, nor Go" + done + if ((${#gofiles[@]})); then + if [[ -z $blobs || ! -d $blobs ]]; then + problem "kind DOCS: non-test Go changed but no old/new blobs were given to compare" + else + # Built here, not run with "go run": that would take the .go + # arguments as more source files of the program. + bin=$(mktemp -d) + trap 'rm -rf "$bin"' EXIT + if ! go build -o "$bin/sametokens" "$here/sametokens.go"; then + echo "spec-trace: could not build sametokens.go; the DOCS check did not run" >&2 + exit 2 + fi + for p in "${gofiles[@]}"; do + if [[ ! -e $blobs/old/$p && ! -e $blobs/new/$p ]]; then + problem "kind DOCS: $p has no old or new blob, so it was not compared" + continue + fi + rc=0 + "$bin/sametokens" "$blobs/old/$p" "$blobs/new/$p" >/dev/null || rc=$? + case $rc in + 0) ;; + 1) problem "kind DOCS: $p changes more than comments (its Go tokens differ)" ;; + *) + echo "spec-trace: sametokens failed on $p (exit $rc); the DOCS check did not run" >&2 + exit 2 + ;; + esac + done + fi + fi + ;; +esac + exit $bad diff --git a/seaweed-block/.github/workflows/spec-trace.yml b/seaweed-block/.github/workflows/spec-trace.yml index d16c1e6978..2e09b1c3cb 100644 --- a/seaweed-block/.github/workflows/spec-trace.yml +++ b/seaweed-block/.github/workflows/spec-trace.yml @@ -31,7 +31,9 @@ jobs: - name: Checkout base uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: - ref: ${{ github.event.pull_request.base.sha }} + # github.sha is the tip of the base branch when the event fires; the + # PR's base.sha can predate this workflow's own scripts. + ref: ${{ github.sha }} fetch-depth: 0 persist-credentials: false @@ -47,4 +49,14 @@ jobs: git fetch --no-tags origin "+refs/pull/${PR}/head:refs/remotes/pr/head" printf '%s' "$BODY" >body.txt bash .github/scripts/post-merge-review-changed.sh "$BASE" "$HEAD" >changed.bin - bash .github/scripts/spec-trace-check.sh body.txt "blobs/old/$p" 2>/dev/null || rm -f "blobs/old/$p" + git show "$HEAD:$p" >"blobs/new/$p" 2>/dev/null || rm -f "blobs/new/$p" + done + mkdir -p blobs + bash .github/scripts/spec-trace-check.sh body.txt blobs