Skip to content

Commit 68abd43

Browse files
committed
internal/lsp: fix swallowed package errors
If a package has an error that makes it completely unparsable, such as containing a .go file with no "package" statement, the error was previously unreported. Such errors would manifest as other errors. Fixes golang/go#31712
1 parent 4f9510c commit 68abd43

File tree

7 files changed

+115
-8
lines changed

7 files changed

+115
-8
lines changed

internal/lsp/diagnostics.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package lsp
66

77
import (
88
"context"
9+
"strings"
910

1011
"golang.org/x/tools/internal/lsp/protocol"
1112
"golang.org/x/tools/internal/lsp/source"
@@ -40,8 +41,10 @@ func (s *Server) Diagnostics(ctx context.Context, view source.View, uri span.URI
4041
// Anytime we compute diagnostics, make sure to also send along any
4142
// undelivered ones (only for remaining URIs).
4243
for uri, diagnostics := range s.undelivered {
43-
s.publishDiagnostics(ctx, view, uri, diagnostics)
44-
44+
err := s.publishDiagnostics(ctx, view, uri, diagnostics)
45+
if err != nil {
46+
s.log.Errorf(ctx, "failed to deliver diagnostic for %s: %v", uri, err)
47+
}
4548
// If we fail to deliver the same diagnostics twice, just give up.
4649
delete(s.undelivered, uri)
4750
}
@@ -78,7 +81,7 @@ func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []sou
7881
return nil, err
7982
}
8083
reports = append(reports, protocol.Diagnostic{
81-
Message: diag.Message,
84+
Message: strings.TrimSpace(diag.Message), // go list returns errors prefixed by newline
8285
Range: rng,
8386
Severity: severity,
8487
Source: diag.Source,

internal/lsp/link.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package lsp
66

77
import (
88
"context"
9+
"fmt"
910
"strconv"
1011

1112
"golang.org/x/tools/internal/lsp/protocol"
@@ -21,6 +22,10 @@ func (s *Server) documentLink(ctx context.Context, params *protocol.DocumentLink
2122
}
2223
// find the import block
2324
ast := f.GetAST(ctx)
25+
if ast == nil {
26+
return nil, fmt.Errorf("no AST for %v", uri)
27+
}
28+
2429
var result []protocol.DocumentLink
2530
for _, imp := range ast.Imports {
2631
spn, err := span.NewRange(f.GetFileSet(ctx), imp.Pos(), imp.End()).Span()

internal/lsp/protocol/span.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ func NewColumnMapper(uri span.URI, fset *token.FileSet, f *token.File, content [
3131
}
3232
}
3333

34+
func NewContentColumnMapper(uri span.URI, fn string, content []byte) *ColumnMapper {
35+
return &ColumnMapper{
36+
URI: uri,
37+
Converter: span.NewContentConverter(fn, content),
38+
Content: content,
39+
}
40+
}
41+
3442
func (m *ColumnMapper) Location(s span.Span) (Location, error) {
3543
rng, err := m.Range(s)
3644
if err != nil {

internal/lsp/source/diagnostics.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ func Diagnostics(ctx context.Context, v View, uri span.URI) (map[span.URI][]Diag
6868
for _, filename := range pkg.GetFilenames() {
6969
reports[span.FileURI(filename)] = []Diagnostic{}
7070
}
71+
72+
// Prepare reports for package errors
73+
for _, pkgErr := range pkg.GetErrors() {
74+
reports[packageErrorSpan(pkgErr).URI()] = []Diagnostic{}
75+
}
76+
7177
// Run diagnostics for the package that this URI belongs to.
7278
if !diagnostics(ctx, v, pkg, reports) {
7379
// If we don't have any list, parse, or type errors, run analyses.
@@ -109,7 +115,7 @@ func diagnostics(ctx context.Context, v View, pkg Package, reports map[span.URI]
109115
diags = listErrors
110116
}
111117
for _, diag := range diags {
112-
spn := span.Parse(diag.Pos)
118+
spn := packageErrorSpan(diag)
113119
if spn.IsPoint() && diag.Kind == packages.TypeError {
114120
spn = pointToSpan(ctx, v, spn)
115121
}
@@ -153,6 +159,14 @@ func analyses(ctx context.Context, v View, pkg Package, reports map[span.URI][]D
153159
return nil
154160
}
155161

162+
func packageErrorSpan(pkgErr packages.Error) span.Span {
163+
if pkgErr.Pos == "" {
164+
return span.ParseErrorMessage(pkgErr.Msg)
165+
}
166+
167+
return span.Parse(pkgErr.Pos)
168+
}
169+
156170
func pointToSpan(ctx context.Context, v View, spn span.Span) span.Span {
157171
// Don't set a range if it's anything other than a type error.
158172
f, err := v.GetFile(ctx, spn.URI())

internal/lsp/util.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,18 @@ func getSourceFile(ctx context.Context, v source.View, uri span.URI) (source.Fil
5151
if err != nil {
5252
return nil, nil, err
5353
}
54-
tok := f.GetToken(ctx)
55-
if tok == nil {
56-
return nil, nil, fmt.Errorf("no file information for %v", f.URI())
54+
55+
var m *protocol.ColumnMapper
56+
if tok := f.GetToken(ctx); tok == nil {
57+
fn, err := f.URI().Filename()
58+
if err != nil {
59+
return nil, nil, err
60+
}
61+
m = protocol.NewContentColumnMapper(uri, fn, f.GetContent(ctx))
62+
} else {
63+
m = protocol.NewColumnMapper(f.URI(), f.GetFileSet(ctx), tok, f.GetContent(ctx))
5764
}
58-
m := protocol.NewColumnMapper(f.URI(), f.GetFileSet(ctx), tok, f.GetContent(ctx))
65+
5966
return f, m, nil
6067
}
6168

internal/span/parse.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ import (
1010
"unicode/utf8"
1111
)
1212

13+
// ParseErrorMessage attempts to parse a standard error message by stripping off the trailing error message.
14+
// Works only on errors where the message is prefixed by ": ".
15+
// e.g.:
16+
// attributes.go:13:1: expected 'package', found 'type'
17+
func ParseErrorMessage(input string) Span {
18+
input = strings.TrimSpace(input)
19+
20+
msgIndex := strings.Index(input, ": ")
21+
if msgIndex < 0 {
22+
return Parse(input)
23+
}
24+
25+
return Parse(input[:msgIndex])
26+
}
27+
1328
// Parse returns the location represented by the input.
1429
// All inputs are valid locations, as they can always be a pure filename.
1530
// The returned span will be normalized, and thus if printed may produce a

internal/span/parse_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package span
6+
7+
import (
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestParseErrorMessage(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
in string
16+
expectedFileName string
17+
expectedLine int
18+
expectedColumn int
19+
}{
20+
{
21+
name: "from go list output",
22+
in: "\nattributes.go:13:1: expected 'package', found 'type'",
23+
expectedFileName: "attributes.go",
24+
expectedLine: 13,
25+
expectedColumn: 1,
26+
},
27+
}
28+
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
spn := ParseErrorMessage(tt.in)
32+
fn, err := spn.URI().Filename()
33+
if err != nil {
34+
t.Fatalf("unexpected error: %v", err)
35+
}
36+
37+
if !strings.HasSuffix(fn, tt.expectedFileName) {
38+
t.Errorf("expected filename with suffix %v but got %v", tt.expectedFileName, fn)
39+
}
40+
41+
if !spn.HasPosition() {
42+
t.Fatalf("expected span to have position")
43+
}
44+
45+
pos := spn.Start()
46+
if pos.Line() != tt.expectedLine {
47+
t.Errorf("expected line %v but got %v", tt.expectedLine, pos.Line())
48+
}
49+
50+
if pos.Column() != tt.expectedColumn {
51+
t.Errorf("expected line %v but got %v", tt.expectedLine, pos.Line())
52+
}
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)