Skip to content

Commit 7a2a8a0

Browse files
committed
internal/lsp: propagate errors through source.DocumentSymbols
We had previously been ignoring many errors in textDocument/documentSymbols, which led to errors appearing in the VS Code extension logs (see microsoft/vscode-go#2919 for more context). We should return errors so that we can more easily debug these issues in gopls directly. Change-Id: Ieef7c9f0bc8296f7e12d8c84e60d8b978d311651 Reviewed-on: https://go-review.googlesource.com/c/tools/+/209858 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent ac41720 commit 7a2a8a0

File tree

2 files changed

+63
-32
lines changed

2 files changed

+63
-32
lines changed

internal/lsp/source/symbols.go

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, f File) ([]protocol
3737
switch decl := decl.(type) {
3838
case *ast.FuncDecl:
3939
if obj := info.ObjectOf(decl.Name); obj != nil {
40-
if fs := funcSymbol(ctx, snapshot.View(), m, decl, obj, q); fs.Kind == protocol.Method {
41-
// Store methods separately, as we want them to appear as children
42-
// of the corresponding type (which we may not have seen yet).
40+
fs, err := funcSymbol(ctx, snapshot.View(), m, decl, obj, q)
41+
if err != nil {
42+
return nil, err
43+
}
44+
// Store methods separately, as we want them to appear as children
45+
// of the corresponding type (which we may not have seen yet).
46+
if fs.Kind == protocol.Method {
4347
rtype := obj.Type().(*types.Signature).Recv().Type()
4448
methodsToReceiver[rtype] = append(methodsToReceiver[rtype], fs)
4549
} else {
@@ -51,14 +55,21 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, f File) ([]protocol
5155
switch spec := spec.(type) {
5256
case *ast.TypeSpec:
5357
if obj := info.ObjectOf(spec.Name); obj != nil {
54-
ts := typeSymbol(ctx, snapshot.View(), m, info, spec, obj, q)
58+
ts, err := typeSymbol(ctx, snapshot.View(), m, info, spec, obj, q)
59+
if err != nil {
60+
return nil, err
61+
}
5562
symbols = append(symbols, ts)
5663
symbolsToReceiver[obj.Type()] = len(symbols) - 1
5764
}
5865
case *ast.ValueSpec:
5966
for _, name := range spec.Names {
6067
if obj := info.ObjectOf(name); obj != nil {
61-
symbols = append(symbols, varSymbol(ctx, snapshot.View(), m, decl, name, obj, q))
68+
vs, err := varSymbol(ctx, snapshot.View(), m, decl, name, obj, q)
69+
if err != nil {
70+
return nil, err
71+
}
72+
symbols = append(symbols, vs)
6273
}
6374
}
6475
}
@@ -82,16 +93,19 @@ func DocumentSymbols(ctx context.Context, snapshot Snapshot, f File) ([]protocol
8293
return symbols, nil
8394
}
8495

85-
func funcSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl *ast.FuncDecl, obj types.Object, q types.Qualifier) protocol.DocumentSymbol {
96+
func funcSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl *ast.FuncDecl, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
8697
s := protocol.DocumentSymbol{
8798
Name: obj.Name(),
8899
Kind: protocol.Function,
89100
}
90-
if span, err := nodeToProtocolRange(ctx, view, m, decl); err == nil {
91-
s.Range = span
101+
var err error
102+
s.Range, err = nodeToProtocolRange(ctx, view, m, decl)
103+
if err != nil {
104+
return protocol.DocumentSymbol{}, err
92105
}
93-
if span, err := nodeToProtocolRange(ctx, view, m, decl.Name); err == nil {
94-
s.SelectionRange = span
106+
s.SelectionRange, err = nodeToProtocolRange(ctx, view, m, decl.Name)
107+
if err != nil {
108+
return protocol.DocumentSymbol{}, err
95109
}
96110
sig, _ := obj.Type().(*types.Signature)
97111
if sig != nil {
@@ -112,7 +126,7 @@ func funcSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl *
112126
}
113127
s.Detail += ")"
114128
}
115-
return s
129+
return s, nil
116130
}
117131

118132
func setKind(s *protocol.DocumentSymbol, typ types.Type, q types.Qualifier) {
@@ -143,18 +157,21 @@ func setKind(s *protocol.DocumentSymbol, typ types.Type, q types.Qualifier) {
143157
}
144158
}
145159

146-
func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *types.Info, spec *ast.TypeSpec, obj types.Object, q types.Qualifier) protocol.DocumentSymbol {
160+
func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *types.Info, spec *ast.TypeSpec, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
147161
s := protocol.DocumentSymbol{
148162
Name: obj.Name(),
149163
}
150164
s.Detail, _ = formatType(obj.Type(), q)
151165
setKind(&s, obj.Type(), q)
152166

153-
if span, err := nodeToProtocolRange(ctx, view, m, spec); err == nil {
154-
s.Range = span
167+
var err error
168+
s.Range, err = nodeToProtocolRange(ctx, view, m, spec)
169+
if err != nil {
170+
return protocol.DocumentSymbol{}, err
155171
}
156-
if span, err := nodeToProtocolRange(ctx, view, m, spec.Name); err == nil {
157-
s.SelectionRange = span
172+
s.SelectionRange, err = nodeToProtocolRange(ctx, view, m, spec.Name)
173+
if err != nil {
174+
return protocol.DocumentSymbol{}, err
158175
}
159176
t, objIsStruct := obj.Type().Underlying().(*types.Struct)
160177
st, specIsStruct := spec.Type.(*ast.StructType)
@@ -198,11 +215,13 @@ func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *
198215
}
199216
}
200217
}
201-
if span, err := nodeToProtocolRange(ctx, view, m, spanNode); err == nil {
202-
child.Range = span
218+
child.Range, err = nodeToProtocolRange(ctx, view, m, spanNode)
219+
if err != nil {
220+
return protocol.DocumentSymbol{}, err
203221
}
204-
if span, err := nodeToProtocolRange(ctx, view, m, selectionNode); err == nil {
205-
child.SelectionRange = span
222+
child.SelectionRange, err = nodeToProtocolRange(ctx, view, m, selectionNode)
223+
if err != nil {
224+
return protocol.DocumentSymbol{}, err
206225
}
207226
s.Children = append(s.Children, child)
208227
}
@@ -230,16 +249,18 @@ func typeSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, info *
230249
break Embeddeds
231250
}
232251
}
233-
if rng, err := nodeToProtocolRange(ctx, view, m, spanNode); err == nil {
234-
child.Range = rng
252+
child.Range, err = nodeToProtocolRange(ctx, view, m, spanNode)
253+
if err != nil {
254+
return protocol.DocumentSymbol{}, err
235255
}
236-
if span, err := nodeToProtocolRange(ctx, view, m, selectionNode); err == nil {
237-
child.SelectionRange = span
256+
child.SelectionRange, err = nodeToProtocolRange(ctx, view, m, selectionNode)
257+
if err != nil {
258+
return protocol.DocumentSymbol{}, err
238259
}
239260
s.Children = append(s.Children, child)
240261
}
241262
}
242-
return s
263+
return s, nil
243264
}
244265

245266
func nodesForStructField(i int, st *ast.StructType) (span, selection ast.Node) {
@@ -262,20 +283,23 @@ func nodesForStructField(i int, st *ast.StructType) (span, selection ast.Node) {
262283
return nil, nil
263284
}
264285

265-
func varSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl ast.Node, name *ast.Ident, obj types.Object, q types.Qualifier) protocol.DocumentSymbol {
286+
func varSymbol(ctx context.Context, view View, m *protocol.ColumnMapper, decl ast.Node, name *ast.Ident, obj types.Object, q types.Qualifier) (protocol.DocumentSymbol, error) {
266287
s := protocol.DocumentSymbol{
267288
Name: obj.Name(),
268289
Kind: protocol.Variable,
269290
}
270291
if _, ok := obj.(*types.Const); ok {
271292
s.Kind = protocol.Constant
272293
}
273-
if rng, err := nodeToProtocolRange(ctx, view, m, decl); err == nil {
274-
s.Range = rng
294+
var err error
295+
s.Range, err = nodeToProtocolRange(ctx, view, m, decl)
296+
if err != nil {
297+
return protocol.DocumentSymbol{}, err
275298
}
276-
if span, err := nodeToProtocolRange(ctx, view, m, name); err == nil {
277-
s.SelectionRange = span
299+
s.SelectionRange, err = nodeToProtocolRange(ctx, view, m, name)
300+
if err != nil {
301+
return protocol.DocumentSymbol{}, err
278302
}
279303
s.Detail = types.TypeString(obj.Type(), q)
280-
return s
304+
return s, nil
281305
}

internal/lsp/symbols.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99

1010
"golang.org/x/tools/internal/lsp/protocol"
1111
"golang.org/x/tools/internal/lsp/source"
12+
"golang.org/x/tools/internal/lsp/telemetry"
1213
"golang.org/x/tools/internal/span"
14+
"golang.org/x/tools/internal/telemetry/log"
1315
"golang.org/x/tools/internal/telemetry/trace"
1416
)
1517

@@ -27,5 +29,10 @@ func (s *Server) documentSymbol(ctx context.Context, params *protocol.DocumentSy
2729
if err != nil {
2830
return nil, err
2931
}
30-
return source.DocumentSymbols(ctx, snapshot, f)
32+
symbols, err := source.DocumentSymbols(ctx, snapshot, f)
33+
if err != nil {
34+
log.Error(ctx, "DocumentSymbols failed", err, telemetry.URI.Of(uri))
35+
return []protocol.DocumentSymbol{}, nil
36+
}
37+
return symbols, nil
3138
}

0 commit comments

Comments
 (0)