From d2cfa8bc2ddca34a9d48312fe184d0e3fcb3bb19 Mon Sep 17 00:00:00 2001 From: Mohsen Sarrafan Date: Thu, 22 Oct 2026 02:21:34 -0400 Subject: [PATCH] Keep first failure in step output --- internal/core/stages.go | 2 +- internal/eco/maven_test.go | 4 ++-- internal/verify/run.go | 42 ++++++++++++++++++++++++++++++------- internal/verify/run_test.go | 23 ++++++++++++++++++++ 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/internal/core/stages.go b/internal/core/stages.go index 67f6d35..1914ac3 100644 --- a/internal/core/stages.go +++ b/internal/core/stages.go @@ -16,7 +16,7 @@ type Step struct { Cmd string Exit int Took time.Duration - Tail string // last 40 lines of combined output + Tail string // first failure and last 40 lines of combined output Log string // full log path under the state dir Skipped string // reason it did not run, "" if it ran } diff --git a/internal/eco/maven_test.go b/internal/eco/maven_test.go index 74d787e..f941a55 100644 --- a/internal/eco/maven_test.go +++ b/internal/eco/maven_test.go @@ -244,7 +244,7 @@ func TestMavenImports(t *testing.T) { } // TestMavenBumpReal checks that a bumped multi-module build still validates. -// The validate phase binds no plugins, so it runs offline. +// The validate phase binds no plugins, but it does fetch the imported BOM. func TestMavenBumpReal(t *testing.T) { if !have("mvn") { t.Skip("mvn not found") @@ -254,7 +254,7 @@ func TestMavenBumpReal(t *testing.T) { if _, err := (maven{}).Bump(context.Background(), root, d, "2.17.0", ""); err != nil { t.Fatal(err) } - if out, err := run(context.Background(), root, nil, "mvn -q -B -o validate"); err != nil { + if out, err := run(context.Background(), root, nil, "mvn -q -B validate"); err != nil { t.Fatalf("%v\n%s", err, out) } if d := depIn(t, maven{}, root, "api/pom.xml", d.Name, true); d.Version != "2.17.0" { diff --git a/internal/verify/run.go b/internal/verify/run.go index 17e5044..d6f4768 100644 --- a/internal/verify/run.go +++ b/internal/verify/run.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "slices" "strings" "time" @@ -237,16 +238,23 @@ func logName(cmd string) string { } const ( - tailLines = 40 - lineMax = 4096 + tailLines = 40 + firstLines = 10 + lineMax = 4096 ) +// failRe spots the line where a run first goes wrong. +var failRe = regexp.MustCompile(`(?i)^\s*not ok\b|\b(error|fail|failed|failure|panic)\b`) + // tail keeps the last tailLines lines written to it in a fixed ring, so a -// multi-gigabyte build log costs the same memory as a short one. +// multi-gigabyte build log costs the same memory as a short one. It also keeps +// the first failure, which long stack traces can push out of the ring. type tail struct { - ring [tailLines]string - n int - curr []byte + ring [tailLines]string + n int + curr []byte + first []string + firstAt int } func (t *tail) Write(p []byte) (int, error) { @@ -258,13 +266,23 @@ func (t *tail) Write(p []byte) (int, error) { return len(p), nil } t.add(rest[:i]) - t.ring[t.n%tailLines] = string(t.curr) - t.n++ + t.line(string(t.curr)) t.curr = t.curr[:0] rest = rest[i+1:] } } +func (t *tail) line(s string) { + if t.first == nil && failRe.MatchString(s) { + t.firstAt = t.n + } + if (t.first != nil || failRe.MatchString(s)) && len(t.first) < firstLines { + t.first = append(t.first, s) + } + t.ring[t.n%tailLines] = s + t.n++ +} + func (t *tail) add(b []byte) { if room := lineMax - len(t.curr); room > 0 { t.curr = append(t.curr, b[:min(len(b), room)]...) @@ -282,5 +300,13 @@ func (t *tail) String() string { if len(res) > tailLines { res = res[len(res)-tailLines:] } + start := t.n + min(len(t.curr), 1) - len(res) + if t.first != nil && t.firstAt < start { + head := slices.Clone(t.first[:min(len(t.first), start-t.firstAt)]) + if t.firstAt+len(head) < start { + head = append(head, "...") + } + res = append(head, res...) + } return strings.Join(res, "\n") } diff --git a/internal/verify/run_test.go b/internal/verify/run_test.go index de9cc3b..1b47c44 100644 --- a/internal/verify/run_test.go +++ b/internal/verify/run_test.go @@ -113,6 +113,29 @@ func TestTailBounds(t *testing.T) { } } +func TestTailKeepsFirstFailure(t *testing.T) { + var tl tail + fmt.Fprintln(&tl, "TAP version 13") + fmt.Fprintln(&tl, "not ok 1 - charge") + fmt.Fprintln(&tl, " error: reading 'create'") + for i := range 100 { + fmt.Fprintf(&tl, " at frame %d\n", i) + } + lines := strings.Split(tl.String(), "\n") + if lines[0] != "not ok 1 - charge" || lines[1] != " error: reading 'create'" || lines[firstLines] != "..." { + t.Errorf("tail lost the first failure:\n%s", strings.Join(lines[:firstLines+1], "\n")) + } + if len(lines) != firstLines+1+tailLines || lines[len(lines)-1] != " at frame 99" { + t.Errorf("tail kept %d lines, last %q", len(lines), lines[len(lines)-1]) + } + + var near tail + fmt.Fprint(&near, "ok\nnot ok 1\nmore") + if got := near.String(); got != "ok\nnot ok 1\nmore" { + t.Errorf("a failure inside the window was repeated: %q", got) + } +} + func TestRunStream(t *testing.T) { var buf bytes.Buffer _, err := Run(context.Background(), &Worktree{Dir: t.TempDir()}, Options{