Skip to content

Commit a7c0594

Browse files
committed
internal/lsp: avoid logging context cancellation
This change adds a helper function that checks if the context is canceled, and if so, doesn't log the error. Tried to use it everywhere in internal/lsp where it fits, which resulted in changing a few pieces of error handling throughout. Updates golang/go#37875 Change-Id: I59cbc6f893e3b70cf84524d9944ff7f4b4febd78 Reviewed-on: https://go-review.googlesource.com/c/tools/+/226371 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent a42d6a3 commit a7c0594

File tree

11 files changed

+49
-45
lines changed

11 files changed

+49
-45
lines changed

internal/lsp/cache/analysis.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ func runAnalysis(ctx context.Context, fset *token.FileSet, analyzer *analysis.An
348348
event.Error(ctx, "unable to compute analysis error position", err, tag.Category.Of(diag.Category), tag.Package.Of(pkg.ID()))
349349
continue
350350
}
351+
if ctx.Err() != nil {
352+
data.err = ctx.Err()
353+
return data
354+
}
351355
data.diagnostics = append(data.diagnostics, srcErr)
352356
}
353357
return data

internal/lsp/cache/check.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ func (s *snapshot) buildKey(ctx context.Context, id packageID, mode source.Parse
144144
depHandle, err := s.buildPackageHandle(ctx, depID, mode)
145145
if err != nil {
146146
event.Error(ctx, "no dep handle", err, tag.Package.Of(string(depID)))
147-
147+
if ctx.Err() != nil {
148+
return nil, nil, ctx.Err()
149+
}
148150
// One bad dependency should not prevent us from checking the entire package.
149151
// Add a special key to mark a bad dependency.
150152
depKeys = append(depKeys, packageHandleKey(fmt.Sprintf("%s import not found", id)))

internal/lsp/cache/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func sourceError(ctx context.Context, fset *token.FileSet, pkg *pkg, e interface
6060
kind = source.ParseError
6161
spn, err = scannerErrorRange(fset, pkg, e.Pos)
6262
if err != nil {
63+
if ctx.Err() != nil {
64+
return nil, ctx.Err()
65+
}
6366
event.Error(ctx, "no span for scanner.Error pos", err, tag.Package.Of(pkg.ID()))
6467
spn = span.Parse(e.Pos.String())
6568
}
@@ -73,6 +76,9 @@ func sourceError(ctx context.Context, fset *token.FileSet, pkg *pkg, e interface
7376
kind = source.ParseError
7477
spn, err = scannerErrorRange(fset, pkg, e[0].Pos)
7578
if err != nil {
79+
if ctx.Err() != nil {
80+
return nil, ctx.Err()
81+
}
7682
event.Error(ctx, "no span for scanner.Error pos", err, tag.Package.Of(pkg.ID()))
7783
spn = span.Parse(e[0].Pos.String())
7884
}

internal/lsp/code_action.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"golang.org/x/tools/internal/lsp/protocol"
1717
"golang.org/x/tools/internal/lsp/source"
1818
"golang.org/x/tools/internal/telemetry/event"
19-
errors "golang.org/x/xerrors"
2019
)
2120

2221
func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
@@ -44,7 +43,7 @@ func (s *Server) codeAction(ctx context.Context, params *protocol.CodeActionPara
4443
}
4544
}
4645
if len(wanted) == 0 {
47-
return nil, errors.Errorf("no supported code action to execute for %s, wanted %v", uri, params.Context.Only)
46+
return nil, fmt.Errorf("no supported code action to execute for %s, wanted %v", uri, params.Context.Only)
4847
}
4948

5049
var codeActions []protocol.CodeAction

internal/lsp/completion.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ func (s *Server) completion(ctx context.Context, params *protocol.CompletionPara
2828
case source.Mod:
2929
candidates, surrounding = nil, nil
3030
}
31-
3231
if err != nil {
33-
event.Print(ctx, "no completions found", tag.Position.Of(params.Position), event.Err.Of(err))
32+
event.Error(ctx, "no completions found", err, tag.Position.Of(params.Position))
3433
}
3534
if candidates == nil {
3635
return &protocol.CompletionList{

internal/lsp/diagnostics.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA
5757

5858
// Diagnose the go.mod file.
5959
reports, missingModules, err := mod.Diagnostics(ctx, snapshot)
60-
if ctx.Err() != nil {
61-
return nil
62-
}
6360
if err != nil {
6461
event.Error(ctx, "warning: diagnose go.mod", err, tag.Directory.Of(snapshot.View().Folder().Filename()))
6562
}
66-
// Ensure that the reports returned from mod.Diagnostics are only related to the
67-
// go.mod file for the module.
63+
if ctx.Err() != nil {
64+
return nil
65+
}
66+
// Ensure that the reports returned from mod.Diagnostics are only related
67+
// to the go.mod file for the module.
6868
if len(reports) > 1 {
6969
panic("unexpected reports from mod.Diagnostics")
7070
}
@@ -81,9 +81,6 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA
8181

8282
// Diagnose all of the packages in the workspace.
8383
wsPackages, err := snapshot.WorkspacePackages(ctx)
84-
if ctx.Err() != nil {
85-
return nil
86-
}
8784
if err != nil {
8885
event.Error(ctx, "failed to load workspace packages, skipping diagnostics", err, tag.Snapshot.Of(snapshot.ID()), tag.Directory.Of(snapshot.View().Folder))
8986
return nil
@@ -107,9 +104,6 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA
107104
Message: `You are neither in a module nor in your GOPATH. If you are using modules, please open your editor at the directory containing the go.mod. If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new.`,
108105
})
109106
}
110-
if ctx.Err() != nil {
111-
return
112-
}
113107
if err != nil {
114108
event.Error(ctx, "warning: diagnose package", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(ph.ID()))
115109
return
@@ -185,9 +179,7 @@ func (s *Server) publishReports(ctx context.Context, snapshot source.Snapshot, r
185179
URI: protocol.URIFromSpanURI(key.id.URI),
186180
Version: key.id.Version,
187181
}); err != nil {
188-
if ctx.Err() == nil {
189-
event.Error(ctx, "publishReports: failed to deliver diagnostic", err, tag.URI.Of(key.id.URI))
190-
}
182+
event.Error(ctx, "publishReports: failed to deliver diagnostic", err, tag.URI.Of(key.id.URI))
191183
continue
192184
}
193185
// Update the delivered map.

internal/lsp/link.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ func modLinks(ctx context.Context, snapshot source.Snapshot, fh source.FileHandl
5656
// dependency within the require statement.
5757
start, end := token.Pos(s+i), token.Pos(s+i+len(dep))
5858
target := fmt.Sprintf("https://%s/mod/%s", view.Options().LinkTarget, req.Mod.String())
59-
if l, err := toProtocolLink(view, m, target, start, end, source.Mod); err == nil {
60-
links = append(links, l)
61-
} else {
62-
event.Error(ctx, "failed to create protocol link", err)
59+
l, err := toProtocolLink(view, m, target, start, end, source.Mod)
60+
if err != nil {
61+
return nil, err
6362
}
63+
links = append(links, l)
6464
}
65-
// TODO(ridersofrohan): handle links for replace and exclude directives
65+
// TODO(ridersofrohan): handle links for replace and exclude directives.
6666
if syntax := file.Syntax; syntax == nil {
6767
return links, nil
6868
}
@@ -110,11 +110,12 @@ func goLinks(ctx context.Context, view source.View, fh source.FileHandle) ([]pro
110110
target = fmt.Sprintf("https://%s/%s", view.Options().LinkTarget, target)
111111
// Account for the quotation marks in the positions.
112112
start, end := n.Path.Pos()+1, n.Path.End()-1
113-
if l, err := toProtocolLink(view, m, target, start, end, source.Go); err == nil {
114-
links = append(links, l)
115-
} else {
116-
event.Error(ctx, "failed to create protocol link", err)
113+
l, err := toProtocolLink(view, m, target, start, end, source.Go)
114+
if err != nil {
115+
event.Error(ctx, "failed to create link", err)
116+
return false
117117
}
118+
links = append(links, l)
118119
}
119120
return false
120121
case *ast.BasicLit:

internal/lsp/protocol/context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ 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+
}
2225
if !ev.IsLog() {
2326
return ctx
2427
}

internal/lsp/source/completion_format.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,7 @@ func (c *completer) formatBuiltin(cand candidate) CompletionItem {
247247
item.Kind = protocol.FunctionCompletion
248248
astObj, err := c.snapshot.View().LookupBuiltin(c.ctx, obj.Name())
249249
if err != nil {
250-
if c.ctx.Err() == nil {
251-
event.Error(c.ctx, "no builtin package", err)
252-
}
250+
event.Error(c.ctx, "no builtin package", err)
253251
break
254252
}
255253
decl, ok := astObj.Decl.(*ast.FuncDecl)

internal/lsp/source/diagnostics.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, missi
131131
analyzers = snapshot.View().Options().TypeErrorAnalyzers
132132
}
133133
if err := analyses(ctx, snapshot, reports, ph, analyzers); err != nil {
134+
event.Error(ctx, "analyses failed", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(ph.ID()))
134135
if ctx.Err() != nil {
135136
return nil, warn, ctx.Err()
136137
}
137-
event.Error(ctx, "failed to run analyses", err, tag.Package.Of(ph.ID()))
138138
}
139139
return reports, warn, nil
140140
}
@@ -227,7 +227,6 @@ func missingModulesDiagnostics(ctx context.Context, snapshot Snapshot, reports m
227227
}
228228
file, _, m, _, err := snapshot.View().Session().Cache().ParseGoHandle(fh, ParseHeader).Parse(ctx)
229229
if err != nil {
230-
event.Error(ctx, "could not parse go file when checking for missing modules", err)
231230
return err
232231
}
233232
// Make a dependency->import map to improve performance when finding missing dependencies.

internal/lsp/source/highlight.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Highlight(ctx context.Context, snapshot Snapshot, fh FileHandle, pos protoc
4040
}
4141
path, _ := astutil.PathEnclosingInterval(file, rng.Start, rng.Start)
4242
if len(path) == 0 {
43-
return nil, errors.Errorf("no enclosing position found for %v:%v", int(pos.Line), int(pos.Character))
43+
return nil, fmt.Errorf("no enclosing position found for %v:%v", int(pos.Line), int(pos.Character))
4444
}
4545
// If start==end for astutil.PathEnclosingInterval, the 1-char interval following start is used instead.
4646
// As a result, we might not get an exact match so we should check the 1-char interval to the left of the
@@ -147,10 +147,9 @@ Outer:
147147
if resultsList != nil && -1 < index && index < len(resultsList.List) {
148148
rng, err := nodeToProtocolRange(view, pkg, resultsList.List[index])
149149
if err != nil {
150-
event.Error(ctx, "Error getting range for node", err)
151-
} else {
152-
result[rng] = true
150+
return nil, err
153151
}
152+
result[rng] = true
154153
}
155154
// Add the "func" part of the func declaration.
156155
if highlightAllReturnsAndFunc {
@@ -185,9 +184,9 @@ Outer:
185184
rng, err := nodeToProtocolRange(view, pkg, toAdd)
186185
if err != nil {
187186
event.Error(ctx, "Error getting range for node", err)
188-
} else {
189-
result[rng] = true
187+
return false
190188
}
189+
result[rng] = true
191190
return false
192191
}
193192
}
@@ -268,11 +267,12 @@ func highlightImportUses(ctx context.Context, view View, pkg Package, path []ast
268267
if !strings.Contains(basicLit.Value, obj.Name()) {
269268
return true
270269
}
271-
if rng, err := nodeToProtocolRange(view, pkg, n); err == nil {
272-
result[rng] = true
273-
} else {
270+
rng, err := nodeToProtocolRange(view, pkg, n)
271+
if err != nil {
274272
event.Error(ctx, "Error getting range for node", err)
273+
return false
275274
}
275+
result[rng] = true
276276
return false
277277
})
278278
return rangeMapToSlice(result), nil
@@ -311,11 +311,12 @@ func highlightIdentifiers(ctx context.Context, view View, pkg Package, path []as
311311
if nObj := pkg.GetTypesInfo().ObjectOf(n); nObj != idObj {
312312
return false
313313
}
314-
if rng, err := nodeToProtocolRange(view, pkg, n); err == nil {
315-
result[rng] = true
316-
} else {
314+
rng, err := nodeToProtocolRange(view, pkg, n)
315+
if err != nil {
317316
event.Error(ctx, "Error getting range for node", err)
317+
return false
318318
}
319+
result[rng] = true
319320
return false
320321
})
321322
return rangeMapToSlice(result), nil

0 commit comments

Comments
 (0)