fix(build): run go fix to a fixpoint in task fmt
One go fix pass applies each fixer once, to the parse it started with, so a rewrite made by one fixer is only fixed up by the next pass: stditerators turns `for i := range t.NumField()` into a field iterator that copies its loop variable, and forvar deletes that copy on the following pass. A single pass therefore left the copy behind for copyloopvar to reject, and no fmt-then-lint sequence healed it. task fmt now applies passes until a dry run reports nothing left to fix, and fails loudly instead of handing the lint step a tree it must reject. deployment_contract_test.go pins the fixpoint proof rather than the old single-invocation literal.
This commit is contained in:
2 files changed
+44
-5
No files matched your search
+21
-1
@@ -12,7 +12,27 @@ tasks:
|
||||
fmt:
|
||||
desc: Fix and format Go source with the gate's configured formatters
|
||||
cmds:
|
||||
- go fix ./...
|
||||
# One go fix pass is not a fixpoint: it applies each fixer once, to the
|
||||
# parse it started with, so a rewrite made by one fixer is only fixed up
|
||||
# by the next pass. stditerators turns `for i := range t.NumField()` into
|
||||
# a field iterator that copies its loop variable, and forvar deletes that
|
||||
# copy on the following pass -- leaving the copy behind for copyloopvar
|
||||
# to reject. Apply passes until a dry run reports nothing left to fix,
|
||||
# and fail rather than hand the lint step a tree it must reject.
|
||||
- |
|
||||
set -eu
|
||||
converged=false
|
||||
for pass in 1 2 3 4 5 6 7 8 9 10; do
|
||||
go fix ./...
|
||||
if go fix -diff ./... >/dev/null; then
|
||||
converged=true
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ "$converged" != true ]; then
|
||||
echo "go fix still rewrites the tree after 10 passes" >&2
|
||||
exit 1
|
||||
fi
|
||||
# This must use the same engine and configuration as task lint. Running
|
||||
# standalone gofumpt cannot reproduce gci's custom import grouping, so it
|
||||
# can leave a tree that the gate rejects.
|
||||
|
||||
@@ -76,16 +76,35 @@ func TestFormattingUsesOneConfiguredWriter(t *testing.T) {
|
||||
}
|
||||
fmtTask := taskfile[fmtStart:fmtEnd]
|
||||
|
||||
fix := strings.Index(fmtTask, "- go fix ./...")
|
||||
format := strings.Index(fmtTask, "- golangci-lint fmt")
|
||||
const (
|
||||
applyFix = "go fix ./..."
|
||||
verifyFix = "go fix -diff ./..."
|
||||
)
|
||||
fix := strings.Index(fmtTask, applyFix)
|
||||
format := strings.Index(fmtTask, "golangci-lint fmt")
|
||||
if fix < 0 || format < 0 || fix > format {
|
||||
t.Error("fmt task must run go fix before the configured golangci-lint formatters")
|
||||
}
|
||||
if strings.Contains(fmtTask, "gofumpt -") {
|
||||
t.Error("fmt task bypasses the configured formatter set with standalone gofumpt")
|
||||
}
|
||||
if got := strings.Count(taskfile, "- go fix ./..."); got != 1 {
|
||||
t.Errorf("Taskfile runs go fix %d times, want exactly once through task fmt", got)
|
||||
// One go fix pass is not a fixpoint: stditerators rewrites
|
||||
// `for i := range t.NumField()` into a field iterator that copies its loop
|
||||
// variable, and forvar only deletes that copy on the following pass. A tree
|
||||
// that stops after one pass is therefore one copyloopvar rejects, so fmt has
|
||||
// to keep applying passes and prove the plateau with a dry run that exits
|
||||
// clean only when nothing is left to fix.
|
||||
if !strings.Contains(fmtTask, verifyFix) {
|
||||
t.Error("fmt task does not prove that repeated go fix passes have reached a fixpoint")
|
||||
}
|
||||
if got := strings.Count(fmtTask, applyFix); got != 1 {
|
||||
t.Errorf("fmt task applies go fix %d times inline, want 1: the repetition belongs to the fixpoint loop", got)
|
||||
}
|
||||
if got := strings.Count(fmtTask, verifyFix); got != 1 {
|
||||
t.Errorf("fmt task verifies the go fix fixpoint %d times, want 1", got)
|
||||
}
|
||||
if got, owned := strings.Count(taskfile, applyFix), strings.Count(fmtTask, applyFix); got != owned {
|
||||
t.Errorf("Taskfile invokes go fix %d times, %d of them through task fmt: formatting has one owner", got, owned)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user