Skip to content

Fix leading newline in StackTrace %+v output - #255

Open
draarivpatel-ui wants to merge 1 commit into
pkg:masterfrom
draarivpatel-ui:fix-stacktrace-leading-newline
Open

draarivpatel-ui wants to merge 1 commit into
pkg:masterfrom
draarivpatel-ui:fix-stacktrace-leading-newline

Conversation

@draarivpatel-ui

@draarivpatel-ui draarivpatel-ui commented Nov 25, 2026 •

Copy link
Copy Markdown

Summary

Formatting a StackTrace directly with %+v prefixed every frame with a newline, including the first one, so the output always began with a stray blank line:

st := err.(interface{ StackTrace() errors.StackTrace }).StackTrace()
fmt.Sprintf("%+v", st) // "\ngithub.com/...\n\t/file.go:1\n..."

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 byte-for-byte unchanged (the "between each frame" option discussed in the issue); only direct formatting of a StackTrace loses the spurious leading newline. Empty and nil stacks still print nothing.

Test plan

  • Added TestStackTraceFormatNoLeadingNewline as a regression test: it failed before the fix and passes after.
  • Updated the existing TestStackTraceFormat expectation, which encoded the buggy leading newline (flagged in the issue as needing care).
  • All other existing format tests (TestFormatNew, TestFormatWrap, TestFormatWithStack, ...) pass unchanged, confirming verbose error output is identical.
  • Note: two pre-existing failures unrelated to this change exist on current Go toolchains (see Various problems when running tests #249): the non-constant-format-string vet errors in errors_test.go and the TestStackTrace line-number drift from inlining. Both fail identically before and after this change.

Fixes #230

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
Copilot AI balanced review requested due to automatic review settings November 25, 2026 22:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@puellanivis

Copy link
Copy Markdown

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 puellanivis left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not saying the code should be accepted or merged. Just providing a good faith review of the code on its face.

Comment thread errors.go
case 'v':
if s.Flag('+') {
io.WriteString(s, f.msg)
if f.stack != nil && len(*f.stack) > 0 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😐 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.

Comment thread errors.go
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", w.Cause())
if w.stack != nil && len(*w.stack) > 0 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, wrong defensive programming pattern.

Comment thread leading_newline_test.go
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
t.Errorf("fmt.Sprintf(\"%%+v\", stackTrace) has leading newline:\n%q", got)
t.Errorf(`fmt.Sprintf("%%+v", stackTrace) has leading newline:\n%q`, got)

Comment thread leading_newline_test.go
t.Errorf("fmt.Sprintf(\"%%+v\", stackTrace) has leading newline:\n%q", got)
}

// Frames must still be separated by newlines.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test as it appears here in code does not actually test this.

Comment thread leading_newline_test.go
// 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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	if len(st) > 1 && !strings.Contains(got, first) {

What?😐 len(st) > 1 here is true by logical necessity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StackTrace.Format with "%+v" includes a leading newline

3 participants