Skip to content

Commit 44a64ad

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 4480df5 commit 44a64ad

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

go/packages/golist.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"golang.org/x/tools/go/internal/packagesdriver"
2626
"golang.org/x/tools/internal/gocommand"
2727
"golang.org/x/tools/internal/packagesinternal"
28+
"golang.org/x/xerrors"
2829
)
2930

3031
// debug controls verbose logging.
@@ -736,7 +737,7 @@ func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer,
736737
if !ok {
737738
// Catastrophic error:
738739
// - context cancellation
739-
return nil, fmt.Errorf("couldn't run 'go': %v", err)
740+
return nil, xerrors.Errorf("couldn't run 'go': %w", err)
740741
}
741742

742743
// Old go version?

go/packages/loadmode_string.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var modeStrings = []string{
3838
func (mod LoadMode) String() string {
3939
m := mod
4040
if m == 0 {
41-
return fmt.Sprintf("LoadMode(0)")
41+
return "LoadMode(0)"
4242
}
4343
var out []string
4444
for i, x := range allModes {

internal/lsp/debug/serve.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"golang.org/x/tools/internal/telemetry/export/metric"
3535
"golang.org/x/tools/internal/telemetry/export/ocagent"
3636
"golang.org/x/tools/internal/telemetry/export/prometheus"
37+
"golang.org/x/xerrors"
3738
)
3839

3940
type instanceKeyType int
@@ -543,8 +544,16 @@ func (i *Instance) writeMemoryDebug(threshold uint64) error {
543544
func makeGlobalExporter(stderr io.Writer) event.Exporter {
544545
return func(ctx context.Context, ev event.Event, tags event.TagMap) context.Context {
545546
i := GetInstance(ctx)
546-
if ev.IsLog() && (event.Err.Get(ev.Map()) != nil || i == nil) {
547-
fmt.Fprintf(stderr, "%v\n", ev)
547+
548+
if ev.IsLog() {
549+
// Don't log context cancellation errors.
550+
if err := event.Err.Get(ev.Map()); xerrors.Is(err, context.Canceled) {
551+
return ctx
552+
}
553+
// Make sure any log messages without an instance go to stderr.
554+
if i == nil {
555+
fmt.Fprintf(stderr, "%v\n", ev)
556+
}
548557
}
549558
ctx = protocol.LogEvent(ctx, ev, tags)
550559
if i == nil {

internal/lsp/generate.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ package lsp
77
import (
88
"context"
99
"io"
10-
"log"
1110
"math/rand"
1211
"strconv"
1312

1413
"golang.org/x/tools/internal/gocommand"
1514
"golang.org/x/tools/internal/lsp/debug/tag"
1615
"golang.org/x/tools/internal/lsp/protocol"
1716
"golang.org/x/tools/internal/telemetry/event"
18-
errors "golang.org/x/xerrors"
17+
"golang.org/x/xerrors"
1918
)
2019

2120
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) {
4342
}
4443
stderr := io.MultiWriter(er, wc)
4544
err := inv.RunPiped(ctx, er, stderr)
46-
if err != nil && !errors.Is(ctx.Err(), context.Canceled) {
47-
log.Printf("generate: command error: %v", err)
48-
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
49-
Type: protocol.Error,
50-
Message: "go generate exited with an error, check gopls logs",
51-
})
45+
if err != nil {
46+
event.Error(ctx, "generate: command error: %v", err, tag.Directory.Of(dir))
47+
if !xerrors.Is(err, context.Canceled) {
48+
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
49+
Type: protocol.Error,
50+
Message: "go generate exited with an error, check gopls logs",
51+
})
52+
}
5253
}
5354
}
5455

internal/lsp/protocol/context.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ func WithClient(ctx context.Context, client Client) context.Context {
1919
}
2020

2121
func LogEvent(ctx context.Context, ev event.Event, tags event.TagMap) context.Context {
22-
if ctx.Err() != nil {
23-
return ctx
24-
}
2522
if !ev.IsLog() {
2623
return ctx
2724
}

internal/lsp/source/references.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package source
66

77
import (
88
"context"
9-
"errors"
109
"go/ast"
1110
"go/token"
1211
"go/types"
1312

1413
"golang.org/x/tools/internal/lsp/protocol"
1514
"golang.org/x/tools/internal/telemetry/event"
15+
"golang.org/x/xerrors"
1616
)
1717

1818
// 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
3333

3434
qualifiedObjs, err := qualifiedObjsAtProtocolPos(ctx, s, f, pp)
3535
// Don't return references for builtin types.
36-
if errors.Is(err, errBuiltin) {
36+
if xerrors.Is(err, errBuiltin) {
3737
return nil, nil
3838
}
3939
if err != nil {

0 commit comments

Comments
 (0)