Skip to content

dev: replace pkg/errors with native error wrapping #3604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ require (
github.com/nishanths/exhaustive v0.9.5
github.com/nishanths/predeclared v0.2.2
github.com/nunnatsa/ginkgolinter v0.8.1
github.com/pkg/errors v0.9.1
github.com/polyfloyd/go-errorlint v1.1.0
github.com/quasilyte/go-ruleguard/dsl v0.3.22
github.com/ryancurrah/gomodguard v1.3.0
Expand Down Expand Up @@ -154,6 +153,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
Expand Down
21 changes: 10 additions & 11 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
Expand All @@ -20,8 +21,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"

"github.com/golangci/golangci-lint/internal/renameio"
"github.com/golangci/golangci-lint/internal/robustio"
)
Expand Down Expand Up @@ -80,7 +79,7 @@ func (c *Cache) fileName(id [HashSize]byte, key string) string {
var errMissing = errors.New("cache entry not found")

func IsErrMissing(err error) bool {
return errors.Cause(err) == errMissing
return errors.Is(err, errMissing)
}

const (
Expand Down Expand Up @@ -169,10 +168,10 @@ func (c *Cache) get(id ActionID) (Entry, error) {
etime := entry[1 : 1+20]
var buf [HashSize]byte
if _, err = hex.Decode(buf[:], eid); err != nil || buf != id {
return failed(errors.Wrapf(err, "failed to hex decode eid data in %s", fileName))
return failed(fmt.Errorf("failed to hex decode eid data in %s: %w", fileName, err))
}
if _, err = hex.Decode(buf[:], eout); err != nil {
return failed(errors.Wrapf(err, "failed to hex decode eout data in %s", fileName))
return failed(fmt.Errorf("failed to hex decode eout data in %s: %w", fileName, err))
}
i := 0
for i < len(esize) && esize[i] == ' ' {
Expand All @@ -192,7 +191,7 @@ func (c *Cache) get(id ActionID) (Entry, error) {
}

if err = c.used(fileName); err != nil {
return failed(errors.Wrapf(err, "failed to mark %s as used", fileName))
return failed(fmt.Errorf("failed to mark %s as used: %w", fileName, err))
}

return Entry{buf, size, time.Unix(0, tm)}, nil
Expand Down Expand Up @@ -264,15 +263,15 @@ func (c *Cache) used(file string) error {
if os.IsNotExist(err) {
return errMissing
}
return errors.Wrapf(err, "failed to stat file %s", file)
return fmt.Errorf("failed to stat file %s: %w", file, err)
}

if c.now().Sub(info.ModTime()) < mtimeInterval {
return nil
}

if err := os.Chtimes(file, c.now(), c.now()); err != nil {
return errors.Wrapf(err, "failed to change time of file %s", file)
return fmt.Errorf("failed to change time of file %s: %w", file, err)
}

return nil
Expand Down Expand Up @@ -385,7 +384,7 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
return err
}
if err = os.Chtimes(file, c.now(), c.now()); err != nil { // mainly for tests
return errors.Wrapf(err, "failed to change time of file %s", file)
return fmt.Errorf("failed to change time of file %s: %w", file, err)
}

return nil
Expand Down Expand Up @@ -443,7 +442,7 @@ func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error {
if f, openErr := os.Open(name); openErr == nil {
h := sha256.New()
if _, copyErr := io.Copy(h, f); copyErr != nil {
return errors.Wrap(copyErr, "failed to copy to sha256")
return fmt.Errorf("failed to copy to sha256: %w", copyErr)
}

f.Close()
Expand Down Expand Up @@ -519,7 +518,7 @@ func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error {
return err
}
if err = os.Chtimes(name, c.now(), c.now()); err != nil { // mainly for tests
return errors.Wrapf(err, "failed to change time of file %s", name)
return fmt.Errorf("failed to change time of file %s: %w", name, err)
}

return nil
Expand Down
28 changes: 14 additions & 14 deletions internal/pkgcache/pkgcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bytes"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"runtime"
"sort"
"sync"

"github.com/pkg/errors"
"golang.org/x/tools/go/packages"

"github.com/golangci/golangci-lint/internal/cache"
Expand Down Expand Up @@ -61,7 +61,7 @@ func (c *Cache) Put(pkg *packages.Package, mode HashMode, key string, data inter
err = gob.NewEncoder(buf).Encode(data)
})
if err != nil {
return errors.Wrap(err, "failed to gob encode")
return fmt.Errorf("failed to gob encode: %w", err)
}

var aID cache.ActionID
Expand All @@ -71,21 +71,21 @@ func (c *Cache) Put(pkg *packages.Package, mode HashMode, key string, data inter
if err == nil {
subkey, subkeyErr := cache.Subkey(aID, key)
if subkeyErr != nil {
err = errors.Wrap(subkeyErr, "failed to build subkey")
err = fmt.Errorf("failed to build subkey: %w", subkeyErr)
}
aID = subkey
}
})
if err != nil {
return errors.Wrapf(err, "failed to calculate package %s action id", pkg.Name)
return fmt.Errorf("failed to calculate package %s action id: %w", pkg.Name, err)
}
c.ioSem <- struct{}{}
c.sw.TrackStage("cache io", func() {
err = c.lowLevelCache.PutBytes(aID, buf.Bytes())
})
<-c.ioSem
if err != nil {
return errors.Wrapf(err, "failed to save data to low-level cache by key %s for package %s", key, pkg.Name)
return fmt.Errorf("failed to save data to low-level cache by key %s for package %s: %w", key, pkg.Name, err)
}

return nil
Expand All @@ -101,13 +101,13 @@ func (c *Cache) Get(pkg *packages.Package, mode HashMode, key string, data inter
if err == nil {
subkey, subkeyErr := cache.Subkey(aID, key)
if subkeyErr != nil {
err = errors.Wrap(subkeyErr, "failed to build subkey")
err = fmt.Errorf("failed to build subkey: %w", subkeyErr)
}
aID = subkey
}
})
if err != nil {
return errors.Wrapf(err, "failed to calculate package %s action id", pkg.Name)
return fmt.Errorf("failed to calculate package %s action id: %w", pkg.Name, err)
}

var b []byte
Expand All @@ -120,14 +120,14 @@ func (c *Cache) Get(pkg *packages.Package, mode HashMode, key string, data inter
if cache.IsErrMissing(err) {
return ErrMissing
}
return errors.Wrapf(err, "failed to get data from low-level cache by key %s for package %s", key, pkg.Name)
return fmt.Errorf("failed to get data from low-level cache by key %s for package %s: %w", key, pkg.Name, err)
}

c.sw.TrackStage("gob", func() {
err = gob.NewDecoder(bytes.NewReader(b)).Decode(data)
})
if err != nil {
return errors.Wrap(err, "failed to gob decode")
return fmt.Errorf("failed to gob decode: %w", err)
}

return nil
Expand All @@ -136,12 +136,12 @@ func (c *Cache) Get(pkg *packages.Package, mode HashMode, key string, data inter
func (c *Cache) pkgActionID(pkg *packages.Package, mode HashMode) (cache.ActionID, error) {
hash, err := c.packageHash(pkg, mode)
if err != nil {
return cache.ActionID{}, errors.Wrap(err, "failed to get package hash")
return cache.ActionID{}, fmt.Errorf("failed to get package hash: %w", err)
}

key, err := cache.NewHash("action ID")
if err != nil {
return cache.ActionID{}, errors.Wrap(err, "failed to make a hash")
return cache.ActionID{}, fmt.Errorf("failed to make a hash: %w", err)
}
fmt.Fprintf(key, "pkgpath %s\n", pkg.PkgPath)
fmt.Fprintf(key, "pkghash %s\n", hash)
Expand All @@ -167,7 +167,7 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error

key, err := cache.NewHash("package hash")
if err != nil {
return "", errors.Wrap(err, "failed to make a hash")
return "", fmt.Errorf("failed to make a hash: %w", err)
}

fmt.Fprintf(key, "pkgpath %s\n", pkg.PkgPath)
Expand All @@ -176,7 +176,7 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error
h, fErr := cache.FileHash(f)
<-c.ioSem
if fErr != nil {
return "", errors.Wrapf(fErr, "failed to calculate file %s hash", f)
return "", fmt.Errorf("failed to calculate file %s hash: %w", f, fErr)
}
fmt.Fprintf(key, "file %s %x\n", f, h)
}
Expand All @@ -199,7 +199,7 @@ func (c *Cache) packageHash(pkg *packages.Package, mode HashMode) (string, error

depHash, depErr := c.packageHash(dep, depMode)
if depErr != nil {
return errors.Wrapf(depErr, "failed to calculate hash for dependency %s with mode %d", dep.Name, depMode)
return fmt.Errorf("failed to calculate hash for dependency %s with mode %d: %w", dep.Name, depMode, depErr)
}

fmt.Fprintf(key, "import %s %s\n", dep.PkgPath, depHash)
Expand Down
8 changes: 4 additions & 4 deletions pkg/commands/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -12,7 +13,6 @@ import (

"github.com/fatih/color"
"github.com/gofrs/flock"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -149,12 +149,12 @@ func (e *Executor) Execute() error {
func (e *Executor) initHashSalt(version string) error {
binSalt, err := computeBinarySalt(version)
if err != nil {
return errors.Wrap(err, "failed to calculate binary salt")
return fmt.Errorf("failed to calculate binary salt: %w", err)
}

configSalt, err := computeConfigSalt(e.cfg)
if err != nil {
return errors.Wrap(err, "failed to calculate config salt")
return fmt.Errorf("failed to calculate config salt: %w", err)
}

var b bytes.Buffer
Expand Down Expand Up @@ -195,7 +195,7 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {

lintersSettingsBytes, err := yaml.Marshal(cfg.LintersSettings)
if err != nil {
return nil, errors.Wrap(err, "failed to json marshal config linter settings")
return nil, fmt.Errorf("failed to json marshal config linter settings: %w", err)
}

var configData bytes.Buffer
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand All @@ -11,7 +12,6 @@ import (
"time"

"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -355,7 +355,7 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss

lintCtx, err := e.contextLoader.Load(ctx, lintersToRun)
if err != nil {
return nil, errors.Wrap(err, "context loading failed")
return nil, fmt.Errorf("context loading failed: %w", err)
}
lintCtx.Log = e.log.Child(logutils.DebugKeyLintersContext)

Expand Down Expand Up @@ -522,7 +522,8 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) {
if err := e.runAndPrint(ctx, args); err != nil {
e.log.Errorf("Running error: %s", err)
if e.exitCode == exitcodes.Success {
if exitErr, ok := errors.Cause(err).(*exitcodes.ExitError); ok {
var exitErr *exitcodes.ExitError
if errors.As(err, &exitErr) {
e.exitCode = exitErr.Code
} else {
e.exitCode = exitcodes.Failure
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package config

import (
"errors"
"runtime"

"github.com/pkg/errors"
)

var defaultLintersSettings = LintersSettings{
Expand Down
4 changes: 1 addition & 3 deletions pkg/fsutils/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"os"
"sync"

"github.com/pkg/errors"

"github.com/golangci/golangci-lint/pkg/logutils"
)

Expand All @@ -26,7 +24,7 @@ func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {

fileBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, errors.Wrapf(err, "can't read file %s", filePath)
return nil, fmt.Errorf("can't read file %s: %w", filePath, err)
}

fc.files.Store(filePath, fileBytes)
Expand Down
6 changes: 2 additions & 4 deletions pkg/fsutils/linecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"fmt"
"sync"

"github.com/pkg/errors"
)

type fileLinesCache [][]byte
Expand Down Expand Up @@ -39,7 +37,7 @@ func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) {
func (lc *LineCache) getRawLine(filePath string, index0 int) ([]byte, error) {
fc, err := lc.getFileCache(filePath)
if err != nil {
return nil, errors.Wrapf(err, "failed to get file %s lines cache", filePath)
return nil, fmt.Errorf("failed to get file %s lines cache: %w", filePath, err)
}

if index0 < 0 {
Expand All @@ -61,7 +59,7 @@ func (lc *LineCache) getFileCache(filePath string) (fileLinesCache, error) {

fileBytes, err := lc.fileCache.GetFileBytes(filePath)
if err != nil {
return nil, errors.Wrapf(err, "can't get file %s bytes from cache", filePath)
return nil, fmt.Errorf("can't get file %s bytes from cache: %w", filePath, err)
}

fc := bytes.Split(fileBytes, []byte("\n"))
Expand Down
3 changes: 1 addition & 2 deletions pkg/golinters/dupl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"

duplAPI "github.com/golangci/dupl"
"github.com/pkg/errors"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
Expand Down Expand Up @@ -71,7 +70,7 @@ func runDupl(pass *analysis.Pass, settings *config.DuplSettings) ([]goanalysis.I
for _, i := range issues {
toFilename, err := fsutils.ShortestRelPath(i.To.Filename(), "")
if err != nil {
return nil, errors.Wrapf(err, "failed to get shortest rel path for %q", i.To.Filename())
return nil, fmt.Errorf("failed to get shortest rel path for %q: %w", i.To.Filename(), err)
}

dupl := fmt.Sprintf("%s:%d-%d", toFilename, i.To.LineStart(), i.To.LineEnd())
Expand Down
Loading