-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
Consider a test which panics:
func Test(t *testing.T) {
panic("oops")
}The testing package produces this output for this test:
--- FAIL: Test (0.00s)
panic: oops [recovered]
panic: oops
goroutine 3 [running]:
...
Note that the panic value ("oops") is printed twice, because the testing package recovers the panic, performs cleanup, and then re-panics with the same value:
https://go.googlesource.com/go/+/refs/tags/go1.23.5/src/testing/testing.go#1632
This isn't all that much of a problem when the panic message is short, but it can get confusing if the message is long. If the formatted panic value spans multiple lines, the interior panic is indented inconsistently:
--- FAIL: Test (0.00s)
panic: this
is
a
long
panic [recovered]
panic: this
is
a
long
panic
This can get exceedingly verbose if the panic message happens to contain stack information itself. (Perhaps that's an argument against putting stack information in panic messages.)
I'm not sure if this is a general problem with how we print panics which reuse the same value, or a problem with the testing package in particular. Perhaps the testing package should use a short, consistent message when re-panicking, since the original panic value will be printed when the panic terminates the program:
--- FAIL: Test (0.00s)
panic: oops [recovered]
panic: panic in Test
Or perhaps runtime.printpanics should detect the case where a panic value has been reused:
--- FAIL: Test (0.00s)
panic: oops [recovered, reraised]
goroutine 34 [running]:
...