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.

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

2 participants