Skip to content

Commit

Permalink
gopls: remove usage of golang.org/x/xerrors
Browse files Browse the repository at this point in the history
As of golang/go#50827, gopls no longer supports building at 1.12, and so
usage of golang.org/x/xerrors can be replaced with the native support for
error wrapping introduced in Go 1.13.

Remove this usage as a step toward eliminating the xerrors dependency
from x/tools.

For golang/go#52442

Change-Id: Ibf459cc72402a30a6c2735dc620f76ed8a5e2525
Reviewed-on: https://go-review.googlesource.com/c/tools/+/401097
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
findleyr committed Apr 20, 2022
1 parent e854e02 commit 37590b3
Show file tree
Hide file tree
Showing 69 changed files with 239 additions and 272 deletions.
15 changes: 7 additions & 8 deletions internal/lsp/cache/analysis.go
Expand Up @@ -21,7 +21,6 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/memoize"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

func (s *snapshot) Analyze(ctx context.Context, id string, analyzers []*source.Analyzer) ([]*source.Diagnostic, error) {
Expand Down Expand Up @@ -96,7 +95,7 @@ func (s *snapshot) actionHandle(ctx context.Context, id PackageID, a *analysis.A
return act, nil
}
if len(ph.key) == 0 {
return nil, errors.Errorf("actionHandle: no key for package %s", id)
return nil, fmt.Errorf("actionHandle: no key for package %s", id)
}
pkg, err := ph.check(ctx, s)
if err != nil {
Expand Down Expand Up @@ -162,10 +161,10 @@ func (act *actionHandle) analyze(ctx context.Context, snapshot *snapshot) ([]*so
}
data, ok := d.(*actionData)
if !ok {
return nil, nil, errors.Errorf("unexpected type for %s:%s", act.pkg.ID(), act.analyzer.Name)
return nil, nil, fmt.Errorf("unexpected type for %s:%s", act.pkg.ID(), act.analyzer.Name)
}
if data == nil {
return nil, nil, errors.Errorf("unexpected nil analysis for %s:%s", act.pkg.ID(), act.analyzer.Name)
return nil, nil, fmt.Errorf("unexpected nil analysis for %s:%s", act.pkg.ID(), act.analyzer.Name)
}
return data.diagnostics, data.result, data.err
}
Expand All @@ -192,7 +191,7 @@ func execAll(ctx context.Context, snapshot *snapshot, actions []*actionHandle) (
}
data, ok := v.(*actionData)
if !ok {
return errors.Errorf("unexpected type for %s: %T", act, v)
return fmt.Errorf("unexpected type for %s: %T", act, v)
}

mu.Lock()
Expand All @@ -212,7 +211,7 @@ func runAnalysis(ctx context.Context, snapshot *snapshot, analyzer *analysis.Ana
}
defer func() {
if r := recover(); r != nil {
data.err = errors.Errorf("analysis %s for package %s panicked: %v", analyzer.Name, pkg.PkgPath(), r)
data.err = fmt.Errorf("analysis %s for package %s panicked: %v", analyzer.Name, pkg.PkgPath(), r)
}
}()

Expand Down Expand Up @@ -327,7 +326,7 @@ func runAnalysis(ctx context.Context, snapshot *snapshot, analyzer *analysis.Ana
analysisinternal.SetTypeErrors(pass, pkg.typeErrors)

if pkg.IsIllTyped() {
data.err = errors.Errorf("analysis skipped due to errors in package")
data.err = fmt.Errorf("analysis skipped due to errors in package")
return data
}
data.result, data.err = pass.Analyzer.Run(pass)
Expand All @@ -336,7 +335,7 @@ func runAnalysis(ctx context.Context, snapshot *snapshot, analyzer *analysis.Ana
}

if got, want := reflect.TypeOf(data.result), pass.Analyzer.ResultType; got != want {
data.err = errors.Errorf(
data.err = fmt.Errorf(
"internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v",
pass.Pkg.Path(), pass.Analyzer, got, want)
return data
Expand Down
10 changes: 5 additions & 5 deletions internal/lsp/cache/check.go
Expand Up @@ -7,6 +7,7 @@ package cache
import (
"bytes"
"context"
"errors"
"fmt"
"go/ast"
"go/types"
Expand All @@ -29,7 +30,6 @@ import (
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/typeparams"
"golang.org/x/tools/internal/typesinternal"
errors "golang.org/x/xerrors"
)

type packageHandleKey string
Expand Down Expand Up @@ -144,7 +144,7 @@ func (s *snapshot) buildPackageHandle(ctx context.Context, id PackageID, mode so
func (s *snapshot) buildKey(ctx context.Context, id PackageID, mode source.ParseMode) (*packageHandle, map[PackagePath]*packageHandle, error) {
m := s.getMetadata(id)
if m == nil {
return nil, nil, errors.Errorf("no metadata for %s", id)
return nil, nil, fmt.Errorf("no metadata for %s", id)
}
goFiles, err := s.parseGoHandles(ctx, m.GoFiles, mode)
if err != nil {
Expand Down Expand Up @@ -291,7 +291,7 @@ func (ph *packageHandle) ID() string {
func (ph *packageHandle) cached(g *memoize.Generation) (*pkg, error) {
v := ph.handle.Cached(g)
if v == nil {
return nil, errors.Errorf("no cached type information for %s", ph.m.PkgPath)
return nil, fmt.Errorf("no cached type information for %s", ph.m.PkgPath)
}
data := v.(*packageData)
return data.pkg, data.err
Expand Down Expand Up @@ -494,7 +494,7 @@ func doTypeCheck(ctx context.Context, snapshot *snapshot, m *Metadata, mode sour
if found {
return pkg, nil
}
return nil, errors.Errorf("no parsed files for package %s, expected: %v, errors: %v", pkg.m.PkgPath, pkg.compiledGoFiles, m.Errors)
return nil, fmt.Errorf("no parsed files for package %s, expected: %v, errors: %v", pkg.m.PkgPath, pkg.compiledGoFiles, m.Errors)
}

cfg := &types.Config{
Expand All @@ -511,7 +511,7 @@ func doTypeCheck(ctx context.Context, snapshot *snapshot, m *Metadata, mode sour
return nil, snapshot.missingPkgError(ctx, pkgPath)
}
if !source.IsValidImport(string(m.PkgPath), string(dep.m.PkgPath)) {
return nil, errors.Errorf("invalid use of internal package %s", pkgPath)
return nil, fmt.Errorf("invalid use of internal package %s", pkgPath)
}
depPkg, err := dep.check(ctx, snapshot)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/lsp/cache/errors.go
Expand Up @@ -21,7 +21,6 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/typesinternal"
errors "golang.org/x/xerrors"
)

func goPackagesErrorDiagnostics(snapshot *snapshot, pkg *pkg, e packages.Error) ([]*source.Diagnostic, error) {
Expand Down Expand Up @@ -75,7 +74,7 @@ func goPackagesErrorDiagnostics(snapshot *snapshot, pkg *pkg, e packages.Error)
func parseErrorDiagnostics(snapshot *snapshot, pkg *pkg, errList scanner.ErrorList) ([]*source.Diagnostic, error) {
// The first parser error is likely the root cause of the problem.
if errList.Len() <= 0 {
return nil, errors.Errorf("no errors in %v", errList)
return nil, fmt.Errorf("no errors in %v", errList)
}
e := errList[0]
pgf, err := pkg.File(span.URIFromPath(e.Pos.Filename))
Expand Down
12 changes: 6 additions & 6 deletions internal/lsp/cache/load.go
Expand Up @@ -7,6 +7,7 @@ package cache
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -24,7 +25,6 @@ import (
"golang.org/x/tools/internal/memoize"
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

// load calls packages.Load for the given scopes, updating package metadata,
Expand Down Expand Up @@ -135,7 +135,7 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...interf
if err == nil {
err = fmt.Errorf("no packages returned")
}
return errors.Errorf("%v: %w", err, source.PackagesLoadError)
return fmt.Errorf("%v: %w", err, source.PackagesLoadError)
}
for _, pkg := range pkgs {
if !containsDir || s.view.Options().VerboseOutput {
Expand All @@ -152,7 +152,7 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...interf
// Special case for the builtin package, as it has no dependencies.
if pkg.PkgPath == "builtin" {
if len(pkg.GoFiles) != 1 {
return errors.Errorf("only expected 1 file for builtin, got %v", len(pkg.GoFiles))
return fmt.Errorf("only expected 1 file for builtin, got %v", len(pkg.GoFiles))
}
s.setBuiltin(pkg.GoFiles[0])
continue
Expand Down Expand Up @@ -213,7 +213,7 @@ You can work with multiple modules by opening each one as a workspace folder.
Improvements to this workflow will be coming soon, and you can learn more here:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`
return &source.CriticalError{
MainError: errors.Errorf(msg),
MainError: fmt.Errorf(msg),
DiagList: s.applyCriticalErrorToFiles(ctx, msg, openFiles),
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ You can learn more here: https://github.com/golang/tools/blob/master/gopls/doc/w
}
if len(srcDiags) != 0 {
return &source.CriticalError{
MainError: errors.Errorf(`You are working in a nested module.
MainError: fmt.Errorf(`You are working in a nested module.
Please open it as a separate workspace folder. Learn more:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.`),
DiagList: srcDiags,
Expand Down Expand Up @@ -374,7 +374,7 @@ func (s *snapshot) setMetadataLocked(ctx context.Context, pkgPath PackagePath, p
pkgPath = PackagePath(string(pkgPath) + suffix)
}
if _, ok := seen[id]; ok {
return nil, errors.Errorf("import cycle detected: %q", id)
return nil, fmt.Errorf("import cycle detected: %q", id)
}
// Recreate the metadata rather than reusing it to avoid locking.
m := &Metadata{
Expand Down
13 changes: 6 additions & 7 deletions internal/lsp/cache/parse.go
Expand Up @@ -26,7 +26,6 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/memoize"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

// parseKey uniquely identifies a parsed Go file.
Expand Down Expand Up @@ -249,7 +248,7 @@ func parseGo(ctx context.Context, fset *token.FileSet, fh source.FileHandle, mod

ext := filepath.Ext(fh.URI().Filename())
if ext != ".go" && ext != "" { // files generated by cgo have no extension
return &parseGoData{err: errors.Errorf("cannot parse non-Go file %s", fh.URI())}
return &parseGoData{err: fmt.Errorf("cannot parse non-Go file %s", fh.URI())}
}
src, err := fh.Read()
if err != nil {
Expand Down Expand Up @@ -686,7 +685,7 @@ func fixAST(ctx context.Context, n ast.Node, tok *token.File, src []byte) (fixed
// Recursively fix in our fixed node.
_ = fixAST(ctx, parent, tok, src)
} else {
err = errors.Errorf("unable to parse defer or go from *ast.BadStmt: %v", err)
err = fmt.Errorf("unable to parse defer or go from *ast.BadStmt: %v", err)
}
return false
case *ast.BadExpr:
Expand Down Expand Up @@ -1339,17 +1338,17 @@ func parseStmt(pos token.Pos, src []byte) (ast.Stmt, error) {
// best-effort behavior, whereas ParseExpr fails hard on any error.
fakeFile, err := parser.ParseFile(token.NewFileSet(), "", fileSrc, 0)
if fakeFile == nil {
return nil, errors.Errorf("error reading fake file source: %v", err)
return nil, fmt.Errorf("error reading fake file source: %v", err)
}

// Extract our expression node from inside the fake file.
if len(fakeFile.Decls) == 0 {
return nil, errors.Errorf("error parsing fake file: %v", err)
return nil, fmt.Errorf("error parsing fake file: %v", err)
}

fakeDecl, _ := fakeFile.Decls[0].(*ast.FuncDecl)
if fakeDecl == nil || len(fakeDecl.Body.List) == 0 {
return nil, errors.Errorf("no statement in %s: %v", src, err)
return nil, fmt.Errorf("no statement in %s: %v", src, err)
}

stmt := fakeDecl.Body.List[0]
Expand All @@ -1371,7 +1370,7 @@ func parseExpr(pos token.Pos, src []byte) (ast.Expr, error) {

exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
return nil, errors.Errorf("no expr in %s: %v", src, err)
return nil, fmt.Errorf("no expr in %s: %v", src, err)
}

return exprStmt.X, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/cache/pkg.go
Expand Up @@ -5,14 +5,14 @@
package cache

import (
"fmt"
"go/ast"
"go/scanner"
"go/types"

"golang.org/x/mod/module"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

// pkg contains the type information needed by the source package.
Expand Down Expand Up @@ -70,7 +70,7 @@ func (p *pkg) File(uri span.URI) (*source.ParsedGoFile, error) {
return gf, nil
}
}
return nil, errors.Errorf("no parsed file for %s in %v", uri, p.m.ID)
return nil, fmt.Errorf("no parsed file for %s in %v", uri, p.m.ID)
}

func (p *pkg) GetSyntax() []*ast.File {
Expand Down Expand Up @@ -106,7 +106,7 @@ func (p *pkg) GetImport(pkgPath string) (source.Package, error) {
return imp, nil
}
// Don't return a nil pointer because that still satisfies the interface.
return nil, errors.Errorf("no imported package for %s", pkgPath)
return nil, fmt.Errorf("no imported package for %s", pkgPath)
}

func (p *pkg) MissingDependencies() []string {
Expand Down
13 changes: 6 additions & 7 deletions internal/lsp/cache/session.go
Expand Up @@ -18,7 +18,6 @@ import (
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/xcontext"
errors "golang.org/x/xerrors"
)

type Session struct {
Expand Down Expand Up @@ -404,7 +403,7 @@ func (s *Session) dropView(ctx context.Context, v *View) (int, error) {
return i, nil
}
}
return -1, errors.Errorf("view %s for %v not found", v.Name(), v.Folder())
return -1, fmt.Errorf("view %s for %v not found", v.Name(), v.Folder())
}

func (s *Session) ModifyFiles(ctx context.Context, changes []source.FileModification) error {
Expand Down Expand Up @@ -614,7 +613,7 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif
kind = source.FileKindForLang(c.LanguageID)
default:
if !ok {
return nil, errors.Errorf("updateOverlays: modifying unopened overlay %v", c.URI)
return nil, fmt.Errorf("updateOverlays: modifying unopened overlay %v", c.URI)
}
kind = o.kind
}
Expand Down Expand Up @@ -648,10 +647,10 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif
case source.Save:
// Make sure the version and content (if present) is the same.
if false && o.version != version { // Client no longer sends the version
return nil, errors.Errorf("updateOverlays: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
return nil, fmt.Errorf("updateOverlays: saving %s at version %v, currently at %v", c.URI, c.Version, o.version)
}
if c.Text != nil && o.hash != hash {
return nil, errors.Errorf("updateOverlays: overlay %s changed on save", c.URI)
return nil, fmt.Errorf("updateOverlays: overlay %s changed on save", c.URI)
}
sameContentOnDisk = true
default:
Expand All @@ -676,10 +675,10 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif
if c.Action == source.Open {
view, err := s.ViewOf(o.uri)
if err != nil {
return nil, errors.Errorf("updateOverlays: finding view for %s: %v", o.uri, err)
return nil, fmt.Errorf("updateOverlays: finding view for %s: %v", o.uri, err)
}
if kind := view.FileKind(o); kind == source.UnknownKind {
return nil, errors.Errorf("updateOverlays: unknown file kind for %s", o.uri)
return nil, fmt.Errorf("updateOverlays: unknown file kind for %s", o.uri)
}
}

Expand Down
10 changes: 5 additions & 5 deletions internal/lsp/cache/snapshot.go
Expand Up @@ -7,6 +7,7 @@ package cache
import (
"bytes"
"context"
"errors"
"fmt"
"go/ast"
"go/token"
Expand Down Expand Up @@ -35,7 +36,6 @@ import (
"golang.org/x/tools/internal/packagesinternal"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/typesinternal"
errors "golang.org/x/xerrors"
)

type snapshot struct {
Expand Down Expand Up @@ -511,7 +511,7 @@ func (s *snapshot) PackageForFile(ctx context.Context, uri span.URI, mode source
}

if len(phs) < 1 {
return nil, errors.Errorf("no packages")
return nil, fmt.Errorf("no packages")
}

ph := phs[0]
Expand All @@ -528,7 +528,7 @@ func (s *snapshot) PackageForFile(ctx context.Context, uri span.URI, mode source
}
}
if ph == nil {
return nil, errors.Errorf("no packages in input")
return nil, fmt.Errorf("no packages in input")
}

return ph.check(ctx, s)
Expand Down Expand Up @@ -2268,7 +2268,7 @@ func (s *snapshot) BuiltinFile(ctx context.Context) (*source.ParsedGoFile, error
s.mu.Unlock()

if builtin == "" {
return nil, errors.Errorf("no builtin package for view %s", s.view.name)
return nil, fmt.Errorf("no builtin package for view %s", s.view.name)
}

fh, err := s.GetFile(ctx, builtin)
Expand Down Expand Up @@ -2425,7 +2425,7 @@ func buildWorkspaceSumFile(ctx context.Context, modFiles map[span.URI]struct{},
continue
}
if err != nil {
return nil, errors.Errorf("reading go sum: %w", err)
return nil, fmt.Errorf("reading go sum: %w", err)
}
if err := readGoSum(allSums, sumURI.Filename(), data); err != nil {
return nil, err
Expand Down

0 comments on commit 37590b3

Please sign in to comment.