1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:04:44 -07:00

internal/lsp: return an error from {Narrowest,Widest}CheckPackageHandle

This will prevent us from panicking in cases with errors.

Fixes golang/go#34824

Change-Id: I02c20655f6926ec00c1591a905ff5a107cc44192
Reviewed-on: https://go-review.googlesource.com/c/tools/+/200300
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-10-10 15:22:30 -04:00
parent 5889748991
commit 638914d249
9 changed files with 56 additions and 17 deletions

View File

@ -110,7 +110,10 @@ func (v *view) GetActiveReverseDeps(ctx context.Context, f source.File) (results
if err != nil {
continue
}
cph := source.WidestCheckPackageHandle(cphs)
cph, err := source.WidestCheckPackageHandle(cphs)
if err != nil {
continue
}
for _, ph := range cph.Files() {
seen[ph.File().Identity().URI] = struct{}{}
}

View File

@ -213,7 +213,10 @@ func quickFixes(ctx context.Context, view source.View, f source.File, diagnostic
}
// We get the package that source.Diagnostics would've used. This is hack.
// TODO(golang/go#32443): The correct solution will be to cache diagnostics per-file per-snapshot.
cph := source.WidestCheckPackageHandle(cphs)
cph, err := source.WidestCheckPackageHandle(cphs)
if err != nil {
return nil, err
}
pkg, err := cph.Cached(ctx)
if err != nil {
return nil, err

View File

@ -393,7 +393,10 @@ func Completion(ctx context.Context, view View, f File, pos protocol.Position, o
if err != nil {
return nil, nil, err
}
cph := NarrowestCheckPackageHandle(cphs)
cph, err := NarrowestCheckPackageHandle(cphs)
if err != nil {
return nil, nil, err
}
pkg, err := cph.Check(ctx)
if err != nil {
return nil, nil, err

View File

@ -45,7 +45,10 @@ func Diagnostics(ctx context.Context, view View, f File, disabledAnalyses map[st
if err != nil {
return nil, "", err
}
cph := WidestCheckPackageHandle(cphs)
cph, err := WidestCheckPackageHandle(cphs)
if err != nil {
return nil, "", err
}
// If we are missing dependencies, it may because the user's workspace is
// not correctly configured. Report errors, if possible.
@ -220,7 +223,10 @@ func toDiagnostic(ctx context.Context, view View, diag analysis.Diagnostic, cate
if err != nil {
return Diagnostic{}, err
}
cph := NarrowestCheckPackageHandle(cphs)
cph, err := NarrowestCheckPackageHandle(cphs)
if err != nil {
return Diagnostic{}, err
}
pkg, err := cph.Cached(ctx)
if err != nil {
return Diagnostic{}, err

View File

@ -28,7 +28,10 @@ func Format(ctx context.Context, view View, f File) ([]protocol.TextEdit, error)
if err != nil {
return nil, err
}
cph := NarrowestCheckPackageHandle(cphs)
cph, err := NarrowestCheckPackageHandle(cphs)
if err != nil {
return nil, err
}
pkg, err := cph.Check(ctx)
if err != nil {
return nil, err
@ -91,7 +94,10 @@ func Imports(ctx context.Context, view View, f File) ([]protocol.TextEdit, error
if err != nil {
return nil, err
}
cph := NarrowestCheckPackageHandle(cphs)
cph, err := NarrowestCheckPackageHandle(cphs)
if err != nil {
return nil, err
}
pkg, err := cph.Check(ctx)
if err != nil {
return nil, err
@ -154,7 +160,10 @@ func AllImportsFixes(ctx context.Context, view View, f File) (edits []protocol.T
if err != nil {
return nil, nil, err
}
cph := NarrowestCheckPackageHandle(cphs)
cph, err := NarrowestCheckPackageHandle(cphs)
if err != nil {
return nil, nil, err
}
pkg, err := cph.Check(ctx)
if err != nil {
return nil, nil, err

View File

@ -53,7 +53,10 @@ func Identifier(ctx context.Context, view View, f File, pos protocol.Position) (
if err != nil {
return nil, err
}
cph := WidestCheckPackageHandle(cphs)
cph, err := WidestCheckPackageHandle(cphs)
if err != nil {
return nil, err
}
pkg, err := cph.Check(ctx)
if err != nil {
return nil, err

View File

@ -35,7 +35,10 @@ func SignatureHelp(ctx context.Context, view View, f File, pos protocol.Position
if err != nil {
return nil, err
}
cph := NarrowestCheckPackageHandle(cphs)
cph, err := NarrowestCheckPackageHandle(cphs)
if err != nil {
return nil, err
}
pkg, err := cph.Check(ctx)
if err != nil {
return nil, err

View File

@ -22,7 +22,10 @@ func DocumentSymbols(ctx context.Context, view View, f File) ([]protocol.Documen
if err != nil {
return nil, err
}
cph := NarrowestCheckPackageHandle(cphs)
cph, err := NarrowestCheckPackageHandle(cphs)
if err != nil {
return nil, err
}
pkg, err := cph.Check(ctx)
if err != nil {
return nil, err

View File

@ -56,9 +56,9 @@ func (s mappedRange) URI() span.URI {
// By "narrowest" package, we mean the package with the fewest number of files
// that includes the given file. This solves the problem of test variants,
// as the test will have more files than the non-test package.
func NarrowestCheckPackageHandle(handles []CheckPackageHandle) CheckPackageHandle {
func NarrowestCheckPackageHandle(handles []CheckPackageHandle) (CheckPackageHandle, error) {
if len(handles) < 1 {
return nil
return nil, errors.Errorf("no CheckPackageHandles")
}
result := handles[0]
for _, handle := range handles[1:] {
@ -66,16 +66,19 @@ func NarrowestCheckPackageHandle(handles []CheckPackageHandle) CheckPackageHandl
result = handle
}
}
return result
if result == nil {
return nil, errors.Errorf("nil CheckPackageHandles have been returned")
}
return result, nil
}
// WidestCheckPackageHandle returns the CheckPackageHandle containing the most files.
//
// This is useful for something like diagnostics, where we'd prefer to offer diagnostics
// for as many files as possible.
func WidestCheckPackageHandle(handles []CheckPackageHandle) CheckPackageHandle {
func WidestCheckPackageHandle(handles []CheckPackageHandle) (CheckPackageHandle, error) {
if len(handles) < 1 {
return nil
return nil, errors.Errorf("no CheckPackageHandles")
}
result := handles[0]
for _, handle := range handles[1:] {
@ -83,7 +86,10 @@ func WidestCheckPackageHandle(handles []CheckPackageHandle) CheckPackageHandle {
result = handle
}
}
return result
if result == nil {
return nil, errors.Errorf("nil CheckPackageHandles have been returned")
}
return result, nil
}
func IsGenerated(ctx context.Context, view View, uri span.URI) bool {