Repository navigation
Fix leading newline in StackTrace %+v output - #255
draarivpatel-ui wants to merge 1 commit into
Conversation
Formatting a StackTrace directly with %+v prefixed every frame with a newline, including the first, so the output always began with a stray blank line. Frames are now separated by newlines instead of each being prefixed with one. The verbose error formatting in fundamental and withStack writes the separator newline before the first frame, so the output of %+v on errors themselves is unchanged; only direct formatting of a StackTrace loses the spurious leading newline. Fixes pkg#230
|
I’m not sure this was really even worth the tokens on this? The issue is over 6 years old already. It wasn’t really worth effort to fix before the package was basically obsoleted by Go standard library changes. Also, changing the output of libraries like this that have been unchanged for a long time, can unexpectedly break a lot of things. I’m not sure there’s any real value here to changing a purely cosmetic output of an uncommon use case, and risk breaking anyone still depending on the package—and more so depending on the stability of this package. |
puellanivis
left a comment
There was a problem hiding this comment.
Not saying the code should be accepted or merged. Just providing a good faith review of the code on its face.
| case 'v': | ||
| if s.Flag('+') { | ||
| io.WriteString(s, f.msg) | ||
| if f.stack != nil && len(*f.stack) > 0 { |
There was a problem hiding this comment.
😐 if f.stack != nil then f.stack.Format is going to panic on line stack.go:147.
Good idea on the defensive programming, but it really ought to have been implemented different: build a if f.stack == nil { return } guard statement, which would even remove the existing bug that would cause this to panic.
But then, we obviously were never putting a nil *stack before, because we haven’t received reports of any crashes.
| case 'v': | ||
| if s.Flag('+') { | ||
| fmt.Fprintf(s, "%+v", w.Cause()) | ||
| if w.stack != nil && len(*w.stack) > 0 { |
There was a problem hiding this comment.
As above, wrong defensive programming pattern.
| st := stackTrace()[:2] | ||
| got := fmt.Sprintf("%+v", st) | ||
| if strings.HasPrefix(got, "\n") { | ||
| t.Errorf("fmt.Sprintf(\"%%+v\", stackTrace) has leading newline:\n%q", got) |
There was a problem hiding this comment.
Generally, we want to avoid needing to escape quotes like this. In the first place, it looks kind of ugly, and in the second, using a raw string gives us a better idea of what the string should actually look like.
| t.Errorf("fmt.Sprintf(\"%%+v\", stackTrace) has leading newline:\n%q", got) | |
| t.Errorf(`fmt.Sprintf("%%+v", stackTrace) has leading newline:\n%q`, got) |
| t.Errorf("fmt.Sprintf(\"%%+v\", stackTrace) has leading newline:\n%q", got) | ||
| } | ||
|
|
||
| // Frames must still be separated by newlines. |
There was a problem hiding this comment.
The test as it appears here in code does not actually test this.
| // Frames must still be separated by newlines. | ||
| first := fmt.Sprintf("%+v", st[0]) | ||
| both := fmt.Sprintf("%+v", st[:1]) | ||
| if len(st) > 1 && !strings.Contains(got, first) { |
There was a problem hiding this comment.
if len(st) > 1 && !strings.Contains(got, first) {What?😐 len(st) > 1 here is true by logical necessity.
Summary
Formatting a
StackTracedirectly with%+vprefixed every frame with a newline, including the first one, so the output always began with a stray blank line:Frames are now separated by newlines instead of each being prefixed with one. The verbose error formatting in
fundamentalandwithStackwrites the separator newline before the first frame, so the output of%+von errors themselves is byte-for-byte unchanged (the "between each frame" option discussed in the issue); only direct formatting of aStackTraceloses the spurious leading newline. Empty and nil stacks still print nothing.Test plan
TestStackTraceFormatNoLeadingNewlineas a regression test: it failed before the fix and passes after.TestStackTraceFormatexpectation, which encoded the buggy leading newline (flagged in the issue as needing care).TestFormatNew,TestFormatWrap,TestFormatWithStack, ...) pass unchanged, confirming verbose error output is identical.errors_test.goand theTestStackTraceline-number drift from inlining. Both fail identically before and after this change.Fixes #230