mirror of
https://github.com/golang/go
synced 2024-11-05 11:46:12 -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:
parent
4480df5f16
commit
44a64ad78b
@ -25,6 +25,7 @@ import (
|
||||
"golang.org/x/tools/go/internal/packagesdriver"
|
||||
"golang.org/x/tools/internal/gocommand"
|
||||
"golang.org/x/tools/internal/packagesinternal"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// debug controls verbose logging.
|
||||
@ -736,7 +737,7 @@ func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer,
|
||||
if !ok {
|
||||
// Catastrophic error:
|
||||
// - 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?
|
||||
|
@ -38,7 +38,7 @@ var modeStrings = []string{
|
||||
func (mod LoadMode) String() string {
|
||||
m := mod
|
||||
if m == 0 {
|
||||
return fmt.Sprintf("LoadMode(0)")
|
||||
return "LoadMode(0)"
|
||||
}
|
||||
var out []string
|
||||
for i, x := range allModes {
|
||||
|
@ -34,6 +34,7 @@ import (
|
||||
"golang.org/x/tools/internal/telemetry/export/metric"
|
||||
"golang.org/x/tools/internal/telemetry/export/ocagent"
|
||||
"golang.org/x/tools/internal/telemetry/export/prometheus"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
type instanceKeyType int
|
||||
@ -543,9 +544,17 @@ func (i *Instance) writeMemoryDebug(threshold uint64) error {
|
||||
func makeGlobalExporter(stderr io.Writer) event.Exporter {
|
||||
return func(ctx context.Context, ev event.Event, tags event.TagMap) context.Context {
|
||||
i := GetInstance(ctx)
|
||||
if ev.IsLog() && (event.Err.Get(ev.Map()) != nil || i == nil) {
|
||||
|
||||
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)
|
||||
if i == nil {
|
||||
return ctx
|
||||
|
@ -7,7 +7,6 @@ package lsp
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
|
||||
@ -15,7 +14,7 @@ import (
|
||||
"golang.org/x/tools/internal/lsp/debug/tag"
|
||||
"golang.org/x/tools/internal/lsp/protocol"
|
||||
"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) {
|
||||
@ -43,13 +42,15 @@ func (s *Server) runGenerate(ctx context.Context, dir string, recursive bool) {
|
||||
}
|
||||
stderr := io.MultiWriter(er, wc)
|
||||
err := inv.RunPiped(ctx, er, stderr)
|
||||
if err != nil && !errors.Is(ctx.Err(), context.Canceled) {
|
||||
log.Printf("generate: command error: %v", err)
|
||||
if err != nil {
|
||||
event.Error(ctx, "generate: command error: %v", err, tag.Directory.Of(dir))
|
||||
if !xerrors.Is(err, context.Canceled) {
|
||||
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
||||
Type: protocol.Error,
|
||||
Message: "go generate exited with an error, check gopls logs",
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// eventWriter writes every incoming []byte to
|
||||
|
@ -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 {
|
||||
if ctx.Err() != nil {
|
||||
return ctx
|
||||
}
|
||||
if !ev.IsLog() {
|
||||
return ctx
|
||||
}
|
||||
|
@ -6,13 +6,13 @@ package source
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"go/types"
|
||||
|
||||
"golang.org/x/tools/internal/lsp/protocol"
|
||||
"golang.org/x/tools/internal/telemetry/event"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// 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)
|
||||
// Don't return references for builtin types.
|
||||
if errors.Is(err, errBuiltin) {
|
||||
if xerrors.Is(err, errBuiltin) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user