A failed build step keeps the head of its error, not the tail alone (H-A-475)
The Node build module kept only the last half of its bound of a failed
command's stderr, so esbuild's error line was pushed out by its Node
launcher's stack trace ("esbuild for code: status 1" with no cause). The
image build module's reason and journal line kept stderr's tail alone too,
and so did the Node module's toolchain install. Each now keeps the head and
the tail within its bound, with "… <n> bytes left out …" between them. The
Go build module keeps a failure's first line, not its tail, and is left as
it is.
Builds: design 47 H2 H-A-475
This commit is contained in:
@@ -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"},
|
||||
|
||||
@@ -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 "… <n> 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"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user