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

internal/lsp, go/packages: don't log context cancellation errors

Instead of checking the context, check the error. This may expose some
errors that are not wrapped correctly. Replaced all uses of errors
with golang.org/x/xerrors.

Change-Id: Ia40160f8ea352e02618765f2a9415a4ece0dcd94
Reviewed-on: https://go-review.googlesource.com/c/tools/+/227036
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Rebecca Stambler 2020-04-02 17:27:48 -04:00
parent 4480df5f16
commit 44a64ad78b
6 changed files with 25 additions and 17 deletions

View File

@ -25,6 +25,7 @@ import (
"golang.org/x/tools/go/internal/packagesdriver" "golang.org/x/tools/go/internal/packagesdriver"
"golang.org/x/tools/internal/gocommand" "golang.org/x/tools/internal/gocommand"
"golang.org/x/tools/internal/packagesinternal" "golang.org/x/tools/internal/packagesinternal"
"golang.org/x/xerrors"
) )
// debug controls verbose logging. // debug controls verbose logging.
@ -736,7 +737,7 @@ func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer,
if !ok { if !ok {
// Catastrophic error: // Catastrophic error:
// - context cancellation // - context cancellation
return nil, fmt.Errorf("couldn't run 'go': %v", err) return nil, xerrors.Errorf("couldn't run 'go': %w", err)
} }
// Old go version? // Old go version?

View File

@ -38,7 +38,7 @@ var modeStrings = []string{
func (mod LoadMode) String() string { func (mod LoadMode) String() string {
m := mod m := mod
if m == 0 { if m == 0 {
return fmt.Sprintf("LoadMode(0)") return "LoadMode(0)"
} }
var out []string var out []string
for i, x := range allModes { for i, x := range allModes {

View File

@ -34,6 +34,7 @@ import (
"golang.org/x/tools/internal/telemetry/export/metric" "golang.org/x/tools/internal/telemetry/export/metric"
"golang.org/x/tools/internal/telemetry/export/ocagent" "golang.org/x/tools/internal/telemetry/export/ocagent"
"golang.org/x/tools/internal/telemetry/export/prometheus" "golang.org/x/tools/internal/telemetry/export/prometheus"
"golang.org/x/xerrors"
) )
type instanceKeyType int type instanceKeyType int
@ -543,8 +544,16 @@ func (i *Instance) writeMemoryDebug(threshold uint64) error {
func makeGlobalExporter(stderr io.Writer) event.Exporter { func makeGlobalExporter(stderr io.Writer) event.Exporter {
return func(ctx context.Context, ev event.Event, tags event.TagMap) context.Context { return func(ctx context.Context, ev event.Event, tags event.TagMap) context.Context {
i := GetInstance(ctx) i := GetInstance(ctx)
if ev.IsLog() && (event.Err.Get(ev.Map()) != nil || i == nil) {
fmt.Fprintf(stderr, "%v\n", ev) if ev.IsLog() {
// Don't log context cancellation errors.
if err := event.Err.Get(ev.Map()); xerrors.Is(err, context.Canceled) {
return ctx
}
// Make sure any log messages without an instance go to stderr.
if i == nil {
fmt.Fprintf(stderr, "%v\n", ev)
}
} }
ctx = protocol.LogEvent(ctx, ev, tags) ctx = protocol.LogEvent(ctx, ev, tags)
if i == nil { if i == nil {

View File

@ -7,7 +7,6 @@ package lsp
import ( import (
"context" "context"
"io" "io"
"log"
"math/rand" "math/rand"
"strconv" "strconv"
@ -15,7 +14,7 @@ import (
"golang.org/x/tools/internal/lsp/debug/tag" "golang.org/x/tools/internal/lsp/debug/tag"
"golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/telemetry/event" "golang.org/x/tools/internal/telemetry/event"
errors "golang.org/x/xerrors" "golang.org/x/xerrors"
) )
func (s *Server) runGenerate(ctx context.Context, dir string, recursive bool) { func (s *Server) runGenerate(ctx context.Context, dir string, recursive bool) {
@ -43,12 +42,14 @@ func (s *Server) runGenerate(ctx context.Context, dir string, recursive bool) {
} }
stderr := io.MultiWriter(er, wc) stderr := io.MultiWriter(er, wc)
err := inv.RunPiped(ctx, er, stderr) err := inv.RunPiped(ctx, er, stderr)
if err != nil && !errors.Is(ctx.Err(), context.Canceled) { if err != nil {
log.Printf("generate: command error: %v", err) event.Error(ctx, "generate: command error: %v", err, tag.Directory.Of(dir))
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{ if !xerrors.Is(err, context.Canceled) {
Type: protocol.Error, s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
Message: "go generate exited with an error, check gopls logs", Type: protocol.Error,
}) Message: "go generate exited with an error, check gopls logs",
})
}
} }
} }

View File

@ -19,9 +19,6 @@ func WithClient(ctx context.Context, client Client) context.Context {
} }
func LogEvent(ctx context.Context, ev event.Event, tags event.TagMap) context.Context { func LogEvent(ctx context.Context, ev event.Event, tags event.TagMap) context.Context {
if ctx.Err() != nil {
return ctx
}
if !ev.IsLog() { if !ev.IsLog() {
return ctx return ctx
} }

View File

@ -6,13 +6,13 @@ package source
import ( import (
"context" "context"
"errors"
"go/ast" "go/ast"
"go/token" "go/token"
"go/types" "go/types"
"golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/telemetry/event" "golang.org/x/tools/internal/telemetry/event"
"golang.org/x/xerrors"
) )
// ReferenceInfo holds information about reference to an identifier in Go source. // ReferenceInfo holds information about reference to an identifier in Go source.
@ -33,7 +33,7 @@ func References(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Posit
qualifiedObjs, err := qualifiedObjsAtProtocolPos(ctx, s, f, pp) qualifiedObjs, err := qualifiedObjsAtProtocolPos(ctx, s, f, pp)
// Don't return references for builtin types. // Don't return references for builtin types.
if errors.Is(err, errBuiltin) { if xerrors.Is(err, errBuiltin) {
return nil, nil return nil, nil
} }
if err != nil { if err != nil {