1
0
Fork 0
mirror of https://github.com/imjasonh/pkgerrors-analyzer synced 2026-07-07 00:32:34 +00:00
No description
Find a file
Jason Hall 925783f971 initial commit
Signed-off-by: Jason Hall <imjasonh@gmail.com>
2026-03-24 16:19:23 -04:00
cmd/pkgerrors-analyzer initial commit 2026-03-24 16:19:23 -04:00
testdata/src initial commit 2026-03-24 16:19:23 -04:00
analyzer.go initial commit 2026-03-24 16:19:23 -04:00
analyzer_test.go initial commit 2026-03-24 16:19:23 -04:00
fixes.go initial commit 2026-03-24 16:19:23 -04:00
go.mod initial commit 2026-03-24 16:19:23 -04:00
go.sum initial commit 2026-03-24 16:19:23 -04:00
imports.go initial commit 2026-03-24 16:19:23 -04:00
LICENSE initial commit 2026-03-24 16:19:23 -04:00
README.md initial commit 2026-03-24 16:19:23 -04:00

pkgerrors-analyzer

github.com/pkg/errors was archived in December 2021, after the Go stdlib added built-in support for wrapping, unwrapping and joining errors. Even so, there are still 300k+ imports of pkg/errors in the wild.

This is a Go anaylzer that finds uses of pkg/errors and replaces them with errors and fmt.Errorf instead.

Replacements

Before After
errors.New(msg) errors.New(msg) (import swap)
errors.Errorf(fmt, args...) fmt.Errorf(fmt, args...)
errors.Wrap(err, "msg") fmt.Errorf("msg: %w", err)
errors.Wrapf(err, "fmt", args...) fmt.Errorf("fmt: %w", args..., err)
errors.WithMessage(err, "msg") fmt.Errorf("msg: %w", err)
errors.WithMessagef(err, "fmt", args...) fmt.Errorf("fmt: %w", args..., err)
errors.WithStack(err) reported, no auto-fix (stack trace lost)
errors.Cause(err) == target errors.Is(err, target)
errors.Cause(err) != target !errors.Is(err, target)
errors.Cause(err) (other) reported, no auto-fix
errors.Unwrap(err) errors.Unwrap(err) (import swap)
errors.Is(err, target) errors.Is(err, target) (import swap)
errors.As(err, target) errors.As(err, target) (import swap)

Some methods in pkg/errors are not automatically replaceable, and will just report a diagnostic instead of automatically fixing with -fix.

Behavioral differences

These replacements are not always perfectly equivalent. Review changes before committing.

  • Stack traces: pkg/errors.New, Errorf, Wrap, and Wrapf all capture a stack trace. The stdlib replacements do not. If you depend on stack traces in error output (e.g. %+v formatting), those will be lost.
  • Nil wrapping: pkg/errors.Wrap(nil, "msg") returns nil. fmt.Errorf("msg: %w", nil) returns a non-nil error. Code that checks if err != nil after wrapping may behave differently.
  • Cause vs Is: errors.Cause unwraps to the root error only. errors.Is matches anywhere in the chain. This is usually more correct, but could match unexpectedly if the same sentinel appears as both a wrapper and a root cause.

Usage

pkgerrors-analyzer ./...        # report diagnostics
pkgerrors-analyzer -fix ./...   # apply fixes