diff --git a/modules/mesh-build-image/cmd/mesh-build-image/build_test.go b/modules/mesh-build-image/cmd/mesh-build-image/build_test.go index b110cc5ba..b8a6cdf14 100644 --- a/modules/mesh-build-image/cmd/mesh-build-image/build_test.go +++ b/modules/mesh-build-image/cmd/mesh-build-image/build_test.go @@ -160,7 +160,7 @@ func TestBuildContextIsTheModulesDirectoryForADockerfileInASubdirectory(t *testi } // 1703c, issue 1703: a failing Dockerfile step fails the step, its reason saying buildah's error from stderr, never the -// last STEP line buildah writes on stdout; nothing is pushed. +// last STEP line buildah writes on stdout; nothing is pushed. A long stderr keeps its head and its tail (H-A-475). func TestBuildFailsWithBuildahsError(t *testing.T) { steps := "STEP 1/3: FROM docker.io/library/alpine\nSTEP 2/3: RUN apk add --no-cache ca-certificates\nfetch https://dl-cdn.alpinelinux.org/x\n" long := strings.Repeat("x", 2*CauseBytes) @@ -174,8 +174,8 @@ func TestBuildFailsWithBuildahsError(t *testing.T) { []string{"exit status 1", "Interactive authentication required", `building at STEP "RUN apk add`}, []string{"STEP 3/3", "COPY"}}, "a failed RUN": {forgekey.Ran{Status: 1, Stdout: steps, Stderr: "Error: building at STEP \"RUN false\": exit status 1\n"}, []string{`STEP "RUN false"`}, []string{"dl-cdn"}}, - "a long stderr is bounded, its end kept": {forgekey.Ran{Status: 125, Stdout: steps, Stderr: "early noise\n" + long + "\nError: the end\n"}, - []string{"Error: the end"}, []string{"early noise", long}}, + "a long stderr is bounded, its head and its end kept (H-A-475)": {forgekey.Ran{Status: 125, Stdout: steps, Stderr: "Error: the head\n" + long + "\nError: the end\n"}, + []string{"Error: the head", "bytes left out", "Error: the end"}, []string{long}}, "nothing on stderr": {forgekey.Ran{Status: 2, Stdout: steps}, []string{"exit status 2", "nothing on stderr", "dl-cdn"}, nil}, "never ran": {forgekey.Ran{Err: "ENOENT"}, []string{"ENOENT", "wrote nothing"}, nil}, "a pull the network cut": {forgekey.Ran{Status: 125, Stdout: steps, Stderr: "Error: creating build container: initializing source docker://alpine: pinging container registry registry-1.docker.io: Get \"https://registry-1.docker.io/v2/\": dial tcp: lookup registry-1.docker.io on 10.10.0.3:53: i/o timeout\n"}, diff --git a/modules/mesh-build-image/cmd/mesh-build-image/cause.go b/modules/mesh-build-image/cmd/mesh-build-image/cause.go index f0368b766..4c959fcfe 100644 --- a/modules/mesh-build-image/cmd/mesh-build-image/cause.go +++ b/modules/mesh-build-image/cmd/mesh-build-image/cause.go @@ -2,7 +2,8 @@ package main // Why a command failed, as the command said it (issue 1703). buildah writes its progress (STEP n/m, the RUN's own // output) on stdout and its error on stderr, so the last line of both together is a STEP line and hides the error: -// a failure's reason is stderr's last lines, never stdout's, and the whole tail of stderr is said in the journal. +// a failure's reason is stderr's lines, never stdout's, its head and its tail kept within the bound (H-A-475), and more of +// both is said in the journal. import ( "fmt" @@ -18,15 +19,16 @@ const ( TailBytes = 4096 ) -// cause says why a command failed: its exit status (or why it never ran to one) and the last lines it wrote on stderr, -// at most CauseBytes of them, joined with " | ". Only when stderr is empty does stdout's tail stand in, said as such. +// cause says why a command failed: its exit status (or why it never ran to one) and the lines it wrote on stderr, +// joined with " | ", their head and their tail within CauseBytes. Only when stderr is empty does stdout stand in, said +// as such. func cause(r forgekey.Ran) string { head := exitOf(r) - if said := tailLines(r.Stderr, CauseBytes, " | "); said != "" { + if said := keptLines(r.Stderr, CauseBytes, " | "); said != "" { return head + ": " + said } - if said := tailLines(r.Stdout, CauseBytes, " | "); said != "" { - return head + ", nothing on stderr; stdout ends: " + said + if said := keptLines(r.Stdout, CauseBytes, " | "); said != "" { + return head + ", nothing on stderr; stdout says: " + said } return head + ", and it wrote nothing" } @@ -39,32 +41,31 @@ func exitOf(r forgekey.Ran) string { return fmt.Sprintf("exit status %d", r.Status) } -// tailLines is the last non-empty lines of s, trimmed, joined with sep, at most max bytes: the lines are taken whole -// from the end while they fit, and when even the last one does not, its end is kept behind "…". -func tailLines(s string, max int, sep string) string { +// keptLines is the non-empty lines of s, trimmed, joined with sep, at most max bytes: when they do not fit, their +// first and their last part within max, with "… bytes left out …" between them (H-A-475), so a tool's error line +// at the start is never pushed out by what follows it, nor the end by what precedes it. +func keptLines(s string, max int, sep string) string { var lines []string for _, l := range strings.Split(s, "\n") { if l = strings.TrimSpace(l); l != "" { lines = append(lines, l) } } - var keep []string - n := 0 - for i := len(lines) - 1; i >= 0; i-- { - add := len(lines[i]) - if len(keep) > 0 { - add += len(sep) - } - if n+add > max { - if len(keep) == 0 { - keep = append(keep, "…"+cutFront(lines[i], max-len("…"))) - } - break - } - keep = append([]string{lines[i]}, keep...) - n += add + all := strings.Join(lines, sep) + if len(all) <= max { + return all } - return strings.Join(keep, sep) + mark := func(n int) string { return fmt.Sprintf(" … %d bytes left out … ", n) } + room := max - len(mark(len(all))) + if room < 2 { + room = 2 + } + head := all[:room/2] + for len(head) > 0 && !utf8.RuneStart(all[len(head)]) { + head = head[:len(head)-1] + } + tail := cutFront(all, room-room/2) + return head + mark(len(all)-len(head)-len(tail)) + tail } // cutFront is the last at most max bytes of s, starting on a whole character. @@ -82,9 +83,9 @@ func cutFront(s string, max int) string { return s } -// sayFailure writes the tail of a failed command's stderr (and of its stdout, where buildah's RUN output goes) to the -// journal, so what the reason had to leave out can still be read there. +// sayFailure writes the head and the tail of a failed command's stderr (and of its stdout, where buildah's RUN output +// goes) to the journal, so what the reason had to leave out can still be read there. func (m *Module) sayFailure(what string, r forgekey.Ran) { - m.say("%s failed (%s); stderr ends:\n%s\nstdout ends:\n%s", what, exitOf(r), - tailLines(r.Stderr, TailBytes, "\n"), tailLines(r.Stdout, TailBytes, "\n")) + m.say("%s failed (%s); stderr:\n%s\nstdout:\n%s", what, exitOf(r), + keptLines(r.Stderr, TailBytes, "\n"), keptLines(r.Stdout, TailBytes, "\n")) } diff --git a/modules/mesh-build-node/cmd/mesh-build-node/build_test.go b/modules/mesh-build-node/cmd/mesh-build-node/build_test.go index 328d80ae4..ad25c87e2 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/build_test.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/build_test.go @@ -273,8 +273,9 @@ func TestTheLaunchersAreTheSDKsRule(t *testing.T) { } } -// issue 1703: a failed command's reason keeps stderr's end and stdout's start, each bounded, so a long stdout (npm's -// progress) never pushes npm's error out, and tsc's first error on stdout stays in. +// issue 1703: a failed command's reason keeps stderr's head and end and stdout's start, each bounded, so a long stdout +// (npm's progress) never pushes npm's error out, and tsc's first error on stdout stays in. H-A-475: esbuild's error line +// at the head of stderr stays in before its Node launcher's stack trace, the part left out said by its size. func TestRanFailedKeepsTheError(t *testing.T) { noise := strings.Repeat("npm progress line\n", 200) cases := map[string]struct { @@ -283,9 +284,11 @@ func TestRanFailedKeepsTheError(t *testing.T) { wantNot []string }{ "passed": {forgekey.Ran{Stdout: "ok"}, nil, nil}, - "npm, long stdout": {forgekey.Ran{Status: 1, Stdout: noise, Stderr: "npm ERR! early\n" + noise + "npm ERR! code E404\n"}, []string{"status 1", "npm ERR! code E404"}, []string{"npm ERR! early"}}, - "tsc on stdout": {forgekey.Ran{Status: 2, Stdout: "src/a.ts(1,7): error TS2322: wrong\n" + noise}, []string{"error TS2322"}, nil}, - "never ran": {forgekey.Ran{Err: "ENOENT"}, []string{"ENOENT"}, nil}, + "npm, long stdout": {forgekey.Ran{Status: 1, Stdout: noise, Stderr: "npm ERR! early\n" + noise + "npm ERR! code E404\n"}, []string{"status 1", "npm ERR! early", "bytes left out", "npm ERR! code E404"}, nil}, + "esbuild, a launcher's stack trace": {forgekey.Ran{Status: 1, Stderr: "✘ [ERROR] Could not resolve \"./missing\"\n" + strings.Repeat(" at Object. (/node_modules/esbuild/lib/main.js:1:1)\n", 40)}, + []string{"status 1", `[ERROR] Could not resolve "./missing"`, "bytes left out", "main.js:1:1)"}, nil}, + "tsc on stdout": {forgekey.Ran{Status: 2, Stdout: "src/a.ts(1,7): error TS2322: wrong\n" + noise}, []string{"error TS2322"}, nil}, + "never ran": {forgekey.Ran{Err: "ENOENT"}, []string{"ENOENT"}, nil}, } for name, c := range cases { got := ranFailed(c.ran) diff --git a/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go b/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go index f984f9a22..a6bee5544 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/toolchain.go @@ -172,13 +172,8 @@ func (t Toolchain) open(dir string) error { return os.Chmod(mark, 0o644) } -func tail(s string) string { - s = strings.TrimSpace(s) - if len(s) > 400 { - s = s[len(s)-400:] - } - return s -} +// tail is what a failed toolchain install wrote on stderr, its head and its tail within 400 bytes (H-A-475). +func tail(s string) string { return headAndTail(strings.TrimSpace(s), 400) } func (t *Toolchain) homeOrEmpty() string { if t == nil { diff --git a/modules/mesh-build-node/cmd/mesh-build-node/workspace.go b/modules/mesh-build-node/cmd/mesh-build-node/workspace.go index 11f1936ce..0ad4edaf5 100644 --- a/modules/mesh-build-node/cmd/mesh-build-node/workspace.go +++ b/modules/mesh-build-node/cmd/mesh-build-node/workspace.go @@ -15,6 +15,7 @@ import ( "path/filepath" "regexp" "strings" + "unicode/utf8" "git.novox.be/novox/mesh-sdk/go/forgekey" ) @@ -180,18 +181,17 @@ func (m *Module) runIn(ctx context.Context, root string, write bool, dir string, return run(ctx, extra, "env", append([]string{"-C", dir, name}, args...)...) } -// ranFailed says why a command did not pass, or "" when it did (issue 1703): its exit status, the end of what it wrote -// on stderr (npm's error is its last lines) and the start of what it wrote on stdout (tsc writes its errors there, -// first first), each bounded, so a long stdout never pushes stderr's error out of the reason. +// ranFailed says why a command did not pass, or "" when it did (issue 1703): its exit status, the head and the tail of +// what it wrote on stderr (npm's error is its last lines; esbuild's is its first, before its Node launcher's stack +// trace, H-A-475) and the start of what it wrote on stdout (tsc writes its errors there, first first), each bounded, so +// a long stdout never pushes stderr's error out of the reason. func ranFailed(r forgekey.Ran) string { if r.Status == 0 && r.Err == "" { return "" } why := fmt.Sprintf("status %d %s", r.Status, r.Err) if e := strings.TrimSpace(r.Stderr); e != "" { - if len(e) > failedBytes/2 { - e = "... " + e[len(e)-failedBytes/2:] - } + e = headAndTail(e, failedBytes/2) why += ": stderr: " + e } if o := strings.TrimSpace(r.Stdout); o != "" { @@ -205,3 +205,26 @@ func ranFailed(r forgekey.Ran) string { // failedBytes bounds what ranFailed carries of a command's output, stderr and stdout half each. const failedBytes = 1500 + +// headAndTail is s when it fits in max bytes, else its first and its last part within max, with "… bytes left +// out …" between them (H-A-475): a launcher's stack trace at the end never pushes the tool's own error line at the +// start out of what is kept, nor the reverse. Both parts are cut on whole characters. +func headAndTail(s string, max int) string { + if len(s) <= max { + return s + } + mark := func(n int) string { return fmt.Sprintf(" … %d bytes left out … ", n) } + room := max - len(mark(len(s))) + if room < 2 { + room = 2 + } + head := s[:room/2] + for len(head) > 0 && !utf8.RuneStart(s[len(head)]) { + head = head[:len(head)-1] + } + tail := s[len(s)-(room-room/2):] + for len(tail) > 0 && !utf8.RuneStart(tail[0]) { + tail = tail[1:] + } + return head + mark(len(s)-len(head)-len(tail)) + tail +}