Skip to content

internal/lsp: use x/xerrors to create new errors #108

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.11
require (
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEha
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
28 changes: 14 additions & 14 deletions internal/lsp/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"context"
"fmt"
"go/ast"
"go/scanner"
"go/token"
Expand All @@ -20,6 +19,7 @@ import (
"golang.org/x/tools/internal/lsp/telemetry/log"
"golang.org/x/tools/internal/lsp/telemetry/trace"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

type importer struct {
Expand All @@ -40,7 +40,7 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {
ctx := imp.ctx
id, ok := imp.view.mcache.ids[packagePath(pkgPath)]
if !ok {
return nil, fmt.Errorf("no known ID for %s", pkgPath)
return nil, errors.Errorf("no known ID for %s", pkgPath)
}
pkg, err := imp.getPkg(ctx, id)
if err != nil {
Expand All @@ -51,7 +51,7 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {

func (imp *importer) getPkg(ctx context.Context, id packageID) (*pkg, error) {
if _, ok := imp.seen[id]; ok {
return nil, fmt.Errorf("circular import detected")
return nil, errors.Errorf("circular import detected")
}
imp.view.pcache.mu.Lock()
e, ok := imp.view.pcache.packages[id]
Expand Down Expand Up @@ -99,7 +99,7 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error)
defer done()
meta, ok := imp.view.mcache.packages[id]
if !ok {
return nil, fmt.Errorf("no metadata for %v", id)
return nil, errors.Errorf("no metadata for %v", id)
}
pkg := &pkg{
id: meta.id,
Expand All @@ -123,9 +123,9 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error)
mode = source.ParseExported
}
var (
files = make([]*ast.File, len(meta.files))
errors = make([]error, len(meta.files))
wg sync.WaitGroup
files = make([]*ast.File, len(meta.files))
parseErrors = make([]error, len(meta.files))
wg sync.WaitGroup
)
for _, filename := range meta.files {
uri := span.FileURI(filename)
Expand All @@ -141,7 +141,7 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error)
go func(i int, ph source.ParseGoHandle) {
defer wg.Done()

files[i], errors[i] = ph.Parse(ctx)
files[i], parseErrors[i] = ph.Parse(ctx)
}(i, ph)
}
wg.Wait()
Expand All @@ -153,7 +153,7 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error)
i++
}
}
for _, err := range errors {
for _, err := range parseErrors {
if err == context.Canceled {
return nil, err
}
Expand All @@ -166,7 +166,7 @@ func (imp *importer) typeCheck(ctx context.Context, id packageID) (*pkg, error)
if meta.pkgPath == "unsafe" {
pkg.types = types.Unsafe
} else if len(files) == 0 { // not the unsafe package, no parsed files
return nil, fmt.Errorf("no parsed files for package %s", pkg.pkgPath)
return nil, errors.Errorf("no parsed files for package %s", pkg.pkgPath)
} else {
pkg.types = types.NewPackage(string(meta.pkgPath), meta.name)
}
Expand Down Expand Up @@ -209,14 +209,14 @@ func (imp *importer) cachePackage(ctx context.Context, pkg *pkg, meta *metadata,
uri := ph.File().Identity().URI
f, err := imp.view.getFile(ctx, uri)
if err != nil {
return fmt.Errorf("no such file %s: %v", uri, err)
return errors.Errorf("no such file %s: %v", uri, err)
}
gof, ok := f.(*goFile)
if !ok {
return fmt.Errorf("non Go file %s", uri)
return errors.Errorf("non Go file %s", uri)
}
if err := imp.cachePerFile(gof, ph, pkg); err != nil {
return fmt.Errorf("failed to cache file %s: %v", gof.URI(), err)
return errors.Errorf("failed to cache file %s: %v", gof.URI(), err)
}
}

Expand Down Expand Up @@ -246,7 +246,7 @@ func (imp *importer) cachePerFile(gof *goFile, ph source.ParseGoHandle, p *pkg)

file, err := ph.Parse(imp.ctx)
if file == nil {
return fmt.Errorf("no AST for %s: %v", ph.File().Identity().URI, err)
return errors.Errorf("no AST for %s: %v", ph.File().Identity().URI, err)
}
gof.imports = file.Imports
return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/cache/gofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"context"
"fmt"
"go/ast"
"go/token"
"sync"
Expand All @@ -15,6 +14,7 @@ import (
"golang.org/x/tools/internal/lsp/telemetry"
"golang.org/x/tools/internal/lsp/telemetry/log"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

// goFile holds all of the information we know about a Go file.
Expand Down Expand Up @@ -55,7 +55,7 @@ func (f *goFile) GetToken(ctx context.Context) (*token.File, error) {
}
tok := f.view.session.cache.fset.File(file.Pos())
if tok == nil {
return nil, fmt.Errorf("no token.File for %s", f.URI())
return nil, errors.Errorf("no token.File for %s", f.URI())
}
return tok, nil
}
Expand All @@ -67,7 +67,7 @@ func (f *goFile) GetAST(ctx context.Context, mode source.ParseMode) (*ast.File,

if f.isDirty(ctx) || f.wrongParseMode(ctx, mode) {
if _, err := f.view.loadParseTypecheck(ctx, f); err != nil {
return nil, fmt.Errorf("GetAST: unable to check package for %s: %v", f.URI(), err)
return nil, errors.Errorf("GetAST: unable to check package for %s: %v", f.URI(), err)
}
}
fh := f.Handle(ctx)
Expand Down
13 changes: 7 additions & 6 deletions internal/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"golang.org/x/tools/internal/lsp/telemetry/tag"
"golang.org/x/tools/internal/lsp/telemetry/trace"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Error, error) {
Expand Down Expand Up @@ -55,11 +56,11 @@ func (v *view) loadParseTypecheck(ctx context.Context, f *goFile) ([]packages.Er
return nil, err
}
if pkg == nil || pkg.IsIllTyped() {
return nil, fmt.Errorf("loadParseTypecheck: %s is ill typed", m.pkgPath)
return nil, errors.Errorf("loadParseTypecheck: %s is ill typed", m.pkgPath)
}
}
if len(f.pkgs) == 0 {
return nil, fmt.Errorf("no packages found for %s", f.URI())
return nil, errors.Errorf("no packages found for %s", f.URI())
}
return nil, nil
}
Expand Down Expand Up @@ -93,7 +94,7 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*met
pkgs, err := packages.Load(v.Config(ctx), fmt.Sprintf("file=%s", f.filename()))
if len(pkgs) == 0 {
if err == nil {
err = fmt.Errorf("go/packages.Load: no packages found for %s", f.filename())
err = errors.Errorf("go/packages.Load: no packages found for %s", f.filename())
}
// Return this error as a diagnostic to the user.
return nil, []packages.Error{
Expand All @@ -112,7 +113,7 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*met
// If the package comes back with errors from `go list`,
// don't bother type-checking it.
if len(pkg.Errors) > 0 {
return nil, pkg.Errors, fmt.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
return nil, pkg.Errors, errors.Errorf("package %s has errors, skipping type-checking", pkg.PkgPath)
}
// Build the import graph for this package.
if err := v.link(ctx, packagePath(pkg.PkgPath), pkg, nil, missingImports); err != nil {
Expand All @@ -132,7 +133,7 @@ func validateMetadata(ctx context.Context, missingImports map[packagePath]struct

// If `go list` failed to get data for the file in question (this should never happen).
if len(f.meta) == 0 {
return nil, fmt.Errorf("loadParseTypecheck: no metadata found for %v", f.filename())
return nil, errors.Errorf("loadParseTypecheck: no metadata found for %v", f.filename())
}

// If we have already seen these missing imports before, and we have type information,
Expand Down Expand Up @@ -254,7 +255,7 @@ func (v *view) link(ctx context.Context, pkgPath packagePath, pkg *packages.Pack
for importPath, importPkg := range pkg.Imports {
importPkgPath := packagePath(importPath)
if importPkgPath == pkgPath {
return fmt.Errorf("cycle detected in %s", importPath)
return errors.Errorf("cycle detected in %s", importPath)
}
// Don't remember any imports with significant errors.
if importPkgPath != "unsafe" && len(pkg.CompiledGoFiles) == 0 {
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/cache/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package cache

import (
"context"
"fmt"
"go/token"

errors "golang.org/x/xerrors"
)

// modFile holds all of the information we know about a mod file.
Expand All @@ -16,7 +17,7 @@ type modFile struct {
}

func (*modFile) GetToken(context.Context) (*token.File, error) {
return nil, fmt.Errorf("GetToken: not implemented")
return nil, errors.Errorf("GetToken: not implemented")
}

func (*modFile) setContent(content []byte) {}
Expand Down
14 changes: 7 additions & 7 deletions internal/lsp/cache/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"context"
"fmt"
"go/ast"
"go/parser"
"go/scanner"
Expand All @@ -16,6 +15,7 @@ import (
"golang.org/x/tools/internal/lsp/telemetry"
"golang.org/x/tools/internal/lsp/telemetry/trace"
"golang.org/x/tools/internal/memoize"
errors "golang.org/x/xerrors"
)

// Limits the number of parallel parser calls per process.
Expand Down Expand Up @@ -154,7 +154,7 @@ func fix(ctx context.Context, file *ast.File, tok *token.File, src []byte) error
case *ast.BadStmt:
err = parseDeferOrGoStmt(n, parent, tok, src) // don't shadow err
if err != nil {
err = fmt.Errorf("unable to parse defer or go from *ast.BadStmt: %v", err)
err = errors.Errorf("unable to parse defer or go from *ast.BadStmt: %v", err)
}
return false
default:
Expand Down Expand Up @@ -182,7 +182,7 @@ func parseDeferOrGoStmt(bad *ast.BadStmt, parent ast.Node, tok *token.File, src
var lit string
for {
if tkn == token.EOF {
return fmt.Errorf("reached the end of the file")
return errors.Errorf("reached the end of the file")
}
if pos >= bad.From {
break
Expand All @@ -200,7 +200,7 @@ func parseDeferOrGoStmt(bad *ast.BadStmt, parent ast.Node, tok *token.File, src
Go: pos,
}
default:
return fmt.Errorf("no defer or go statement found")
return errors.Errorf("no defer or go statement found")
}

// The expression after the "defer" or "go" starts at this position.
Expand All @@ -225,15 +225,15 @@ FindTo:
to = curr
}
if !from.IsValid() || tok.Offset(from) >= len(src) {
return fmt.Errorf("invalid from position")
return errors.Errorf("invalid from position")
}
if !to.IsValid() || tok.Offset(to)+1 >= len(src) {
return fmt.Errorf("invalid to position")
return errors.Errorf("invalid to position")
}
exprstr := string(src[tok.Offset(from) : tok.Offset(to)+1])
expr, err := parser.ParseExpr(exprstr)
if expr == nil {
return fmt.Errorf("no expr in %s: %v", exprstr, err)
return errors.Errorf("no expr in %s: %v", exprstr, err)
}
// parser.ParseExpr returns undefined positions.
// Adjust them for the current file.
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cache

import (
"context"
"fmt"
"os"
"sort"
"strconv"
Expand All @@ -21,6 +20,7 @@ import (
"golang.org/x/tools/internal/lsp/telemetry/trace"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/xcontext"
errors "golang.org/x/xerrors"
)

type session struct {
Expand Down Expand Up @@ -178,7 +178,7 @@ func (s *session) removeView(ctx context.Context, view *view) error {
return nil
}
}
return fmt.Errorf("view %s for %v not found", view.Name(), view.Folder())
return errors.Errorf("view %s for %v not found", view.Name(), view.Folder())
}

// TODO: Propagate the language ID through to the view.
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/cache/sumfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package cache

import (
"context"
"fmt"
"go/token"

errors "golang.org/x/xerrors"
)

// sumFile holds all of the information we know about a sum file.
Expand All @@ -16,7 +17,7 @@ type sumFile struct {
}

func (*sumFile) GetToken(context.Context) (*token.File, error) {
return nil, fmt.Errorf("GetToken: not implemented")
return nil, errors.Errorf("GetToken: not implemented")
}

func (*sumFile) setContent(content []byte) {}
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cache/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package cache

import (
"context"
"fmt"
"go/token"

"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/memoize"
errors "golang.org/x/xerrors"
)

type tokenKey struct {
Expand Down Expand Up @@ -87,7 +87,7 @@ func tokenFile(ctx context.Context, c *cache, fh source.FileHandle) (*token.File
}
tok := c.FileSet().AddFile(fh.Identity().URI.Filename(), -1, len(buf))
if tok == nil {
return nil, fmt.Errorf("no token.File for %s", fh.Identity().URI)
return nil, errors.Errorf("no token.File for %s", fh.Identity().URI)
}
tok.SetLinesForContent(buf)
return tok, nil
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)

// check implements the check verb for gopls.
Expand Down Expand Up @@ -60,14 +61,14 @@ func (c *check) Run(ctx context.Context, args ...string) error {
select {
case <-file.hasDiagnostics:
case <-time.After(30 * time.Second):
return fmt.Errorf("timed out waiting for results from %v", file.uri)
return errors.Errorf("timed out waiting for results from %v", file.uri)
}
file.diagnosticsMu.Lock()
defer file.diagnosticsMu.Unlock()
for _, d := range file.diagnostics {
spn, err := file.mapper.RangeSpan(d.Range)
if err != nil {
return fmt.Errorf("Could not convert position %v for %q", d.Range, d.Message)
return errors.Errorf("Could not convert position %v for %q", d.Range, d.Message)
}
fmt.Printf("%v: %v\n", spn, d.Message)
}
Expand Down
Loading