Skip to content

Format Errorf arguments only once - #118

Merged
k0kubun merged 2 commits into
k0kubun:masterfrom
abo3losh1:fix/errorf-double-format
Oct 9, 2026
Merged

k0kubun merged 2 commits into
k0kubun:masterfrom
abo3losh1:fix/errorf-double-format

Conversation

@abo3losh1

Copy link
Copy Markdown
Contributor

Errorf formats its arguments twice. It runs them through formatAllWithLineFlag and then passes the result to pp.Sprintf, which pretty prints the already formatted strings again.

p := pp.New()
p.SetColoringEnabled(false)
p.Sprintf("val=%v", 42)        // val=42
p.Errorf("val=%v", 42).Error() // val="42"

With colors on, the error text has the escape codes quoted and colored again (val=\x1b[31m\x1b[1m"\x1b[0m...), and with WithLineInfo it gets two line info headers, the outer one pointing at pp.go instead of the caller.

This came in with #106. Before that Errorf was errors.New(pp.Sprintf(format, a...)), which was fine because the arguments were not formatted first. The fix uses fmt.Sprintf here, same as Sprintf and Fprintf do.

Added TestErrorf, which checks the plain output and that the colored output matches Sprintf. Both fail before the fix. go test and go vet pass.

Errorf formatted its arguments and then passed them to pp.Sprintf, which
formatted them again. Plain values came back quoted, colors showed up as
escaped text, and WithLineInfo added a second header. Use fmt.Sprintf
like Sprintf does.
Copilot AI balanced review requested due to automatic review settings October 6, 2026 22:06

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 reached their quota limit.

@k0kubun
k0kubun merged commit 75766ff into k0kubun:master Oct 9, 2026
5 checks passed
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.

3 participants