Skip to content

Commit b673c5c

Browse files
alexandearldez
andauthored
dev: replace pkg/errors with native error wrapping (#3604)
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
1 parent 6c21f04 commit b673c5c

36 files changed

+106
-126
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ require (
7575
github.com/nishanths/exhaustive v0.9.5
7676
github.com/nishanths/predeclared v0.2.2
7777
github.com/nunnatsa/ginkgolinter v0.8.1
78-
github.com/pkg/errors v0.9.1
7978
github.com/polyfloyd/go-errorlint v1.1.0
8079
github.com/quasilyte/go-ruleguard/dsl v0.3.22
8180
github.com/ryancurrah/gomodguard v1.3.0
@@ -154,6 +153,7 @@ require (
154153
github.com/olekukonko/tablewriter v0.0.5 // indirect
155154
github.com/pelletier/go-toml v1.9.5 // indirect
156155
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
156+
github.com/pkg/errors v0.9.1 // indirect
157157
github.com/pmezard/go-difflib v1.0.0 // indirect
158158
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
159159
github.com/prometheus/client_golang v1.12.1 // indirect

internal/cache/cache.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"bytes"
1313
"crypto/sha256"
1414
"encoding/hex"
15+
"errors"
1516
"fmt"
1617
"io"
1718
"os"
@@ -20,8 +21,6 @@ import (
2021
"strings"
2122
"time"
2223

23-
"github.com/pkg/errors"
24-
2524
"github.com/golangci/golangci-lint/internal/renameio"
2625
"github.com/golangci/golangci-lint/internal/robustio"
2726
)
@@ -80,7 +79,7 @@ func (c *Cache) fileName(id [HashSize]byte, key string) string {
8079
var errMissing = errors.New("cache entry not found")
8180

8281
func IsErrMissing(err error) bool {
83-
return errors.Cause(err) == errMissing
82+
return errors.Is(err, errMissing)
8483
}
8584

8685
const (
@@ -169,10 +168,10 @@ func (c *Cache) get(id ActionID) (Entry, error) {
169168
etime := entry[1 : 1+20]
170169
var buf [HashSize]byte
171170
if _, err = hex.Decode(buf[:], eid); err != nil || buf != id {
172-
return failed(errors.Wrapf(err, "failed to hex decode eid data in %s", fileName))
171+
return failed(fmt.Errorf("failed to hex decode eid data in %s: %w", fileName, err))
173172
}
174173
if _, err = hex.Decode(buf[:], eout); err != nil {
175-
return failed(errors.Wrapf(err, "failed to hex decode eout data in %s", fileName))
174+
return failed(fmt.Errorf("failed to hex decode eout data in %s: %w", fileName, err))
176175
}
177176
i := 0
178177
for i < len(esize) && esize[i] == ' ' {
@@ -192,7 +191,7 @@ func (c *Cache) get(id ActionID) (Entry, error) {
192191
}
193192

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

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

270269
if c.now().Sub(info.ModTime()) < mtimeInterval {
271270
return nil
272271
}
273272

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

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

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

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

525524
return nil

internal/pkgcache/pkgcache.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"bytes"
55
"encoding/gob"
66
"encoding/hex"
7+
"errors"
78
"fmt"
89
"runtime"
910
"sort"
1011
"sync"
1112

12-
"github.com/pkg/errors"
1313
"golang.org/x/tools/go/packages"
1414

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

6767
var aID cache.ActionID
@@ -71,21 +71,21 @@ func (c *Cache) Put(pkg *packages.Package, mode HashMode, key string, data inter
7171
if err == nil {
7272
subkey, subkeyErr := cache.Subkey(aID, key)
7373
if subkeyErr != nil {
74-
err = errors.Wrap(subkeyErr, "failed to build subkey")
74+
err = fmt.Errorf("failed to build subkey: %w", subkeyErr)
7575
}
7676
aID = subkey
7777
}
7878
})
7979
if err != nil {
80-
return errors.Wrapf(err, "failed to calculate package %s action id", pkg.Name)
80+
return fmt.Errorf("failed to calculate package %s action id: %w", pkg.Name, err)
8181
}
8282
c.ioSem <- struct{}{}
8383
c.sw.TrackStage("cache io", func() {
8484
err = c.lowLevelCache.PutBytes(aID, buf.Bytes())
8585
})
8686
<-c.ioSem
8787
if err != nil {
88-
return errors.Wrapf(err, "failed to save data to low-level cache by key %s for package %s", key, pkg.Name)
88+
return fmt.Errorf("failed to save data to low-level cache by key %s for package %s: %w", key, pkg.Name, err)
8989
}
9090

9191
return nil
@@ -101,13 +101,13 @@ func (c *Cache) Get(pkg *packages.Package, mode HashMode, key string, data inter
101101
if err == nil {
102102
subkey, subkeyErr := cache.Subkey(aID, key)
103103
if subkeyErr != nil {
104-
err = errors.Wrap(subkeyErr, "failed to build subkey")
104+
err = fmt.Errorf("failed to build subkey: %w", subkeyErr)
105105
}
106106
aID = subkey
107107
}
108108
})
109109
if err != nil {
110-
return errors.Wrapf(err, "failed to calculate package %s action id", pkg.Name)
110+
return fmt.Errorf("failed to calculate package %s action id: %w", pkg.Name, err)
111111
}
112112

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

126126
c.sw.TrackStage("gob", func() {
127127
err = gob.NewDecoder(bytes.NewReader(b)).Decode(data)
128128
})
129129
if err != nil {
130-
return errors.Wrap(err, "failed to gob decode")
130+
return fmt.Errorf("failed to gob decode: %w", err)
131131
}
132132

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

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

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

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

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

205205
fmt.Fprintf(key, "import %s %s\n", dep.PkgPath, depHash)

pkg/commands/executor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"crypto/sha256"
7+
"fmt"
78
"io"
89
"os"
910
"path/filepath"
@@ -12,7 +13,6 @@ import (
1213

1314
"github.com/fatih/color"
1415
"github.com/gofrs/flock"
15-
"github.com/pkg/errors"
1616
"github.com/spf13/cobra"
1717
"github.com/spf13/pflag"
1818
"gopkg.in/yaml.v3"
@@ -149,12 +149,12 @@ func (e *Executor) Execute() error {
149149
func (e *Executor) initHashSalt(version string) error {
150150
binSalt, err := computeBinarySalt(version)
151151
if err != nil {
152-
return errors.Wrap(err, "failed to calculate binary salt")
152+
return fmt.Errorf("failed to calculate binary salt: %w", err)
153153
}
154154

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

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

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

201201
var configData bytes.Buffer

pkg/commands/run.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"log"
@@ -11,7 +12,6 @@ import (
1112
"time"
1213

1314
"github.com/fatih/color"
14-
"github.com/pkg/errors"
1515
"github.com/spf13/cobra"
1616
"github.com/spf13/pflag"
1717

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

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

@@ -522,7 +522,8 @@ func (e *Executor) executeRun(_ *cobra.Command, args []string) {
522522
if err := e.runAndPrint(ctx, args); err != nil {
523523
e.log.Errorf("Running error: %s", err)
524524
if e.exitCode == exitcodes.Success {
525-
if exitErr, ok := errors.Cause(err).(*exitcodes.ExitError); ok {
525+
var exitErr *exitcodes.ExitError
526+
if errors.As(err, &exitErr) {
526527
e.exitCode = exitErr.Code
527528
} else {
528529
e.exitCode = exitcodes.Failure

pkg/config/linters_settings.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package config
22

33
import (
4+
"errors"
45
"runtime"
5-
6-
"github.com/pkg/errors"
76
)
87

98
var defaultLintersSettings = LintersSettings{

pkg/fsutils/filecache.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"os"
66
"sync"
77

8-
"github.com/pkg/errors"
9-
108
"github.com/golangci/golangci-lint/pkg/logutils"
119
)
1210

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

2725
fileBytes, err := os.ReadFile(filePath)
2826
if err != nil {
29-
return nil, errors.Wrapf(err, "can't read file %s", filePath)
27+
return nil, fmt.Errorf("can't read file %s: %w", filePath, err)
3028
}
3129

3230
fc.files.Store(filePath, fileBytes)

pkg/fsutils/linecache.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"sync"
7-
8-
"github.com/pkg/errors"
97
)
108

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

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

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

6765
fc := bytes.Split(fileBytes, []byte("\n"))

pkg/golinters/dupl.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"sync"
77

88
duplAPI "github.com/golangci/dupl"
9-
"github.com/pkg/errors"
109
"golang.org/x/tools/go/analysis"
1110

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

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

0 commit comments

Comments
 (0)