Skip to content

Commit 6254553

Browse files
committed
internal/lsp: use correct file identities when computing diagnostics
CL 214586 switched to using URIs in error messages instead of file identities. It wasn't fully correct because the reports were being allocated for the file identities in the package handle (which may be outdated). Fixes golang/go#36601 Change-Id: I4fcfc02df74d94fff49540c784ef816c357e7232 Reviewed-on: https://go-review.googlesource.com/c/tools/+/215680 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
1 parent bd79bb7 commit 6254553

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

internal/lsp/source/diagnostics.go

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package source
66

77
import (
88
"context"
9-
"fmt"
109
"strings"
1110

1211
"golang.org/x/tools/go/analysis"
@@ -69,7 +68,9 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, withA
6968
// Prepare the reports we will send for the files in this package.
7069
reports := make(map[FileIdentity][]Diagnostic)
7170
for _, fh := range pkg.CompiledGoFiles() {
72-
clearReports(snapshot, reports, fh.File().Identity())
71+
if err := clearReports(snapshot, reports, fh.File().Identity().URI); err != nil {
72+
return nil, warningMsg, err
73+
}
7374
}
7475
// Prepare any additional reports for the errors in this package.
7576
for _, e := range pkg.GetErrors() {
@@ -85,11 +86,9 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, withA
8586
}
8687
}
8788
}
88-
fh, err := snapshot.GetFile(e.URI)
89-
if err != nil {
89+
if err := clearReports(snapshot, reports, e.URI); err != nil {
9090
return nil, warningMsg, err
9191
}
92-
clearReports(snapshot, reports, fh.Identity())
9392
}
9493
// Run diagnostics for the package that this URI belongs to.
9594
hadDiagnostics, err := diagnostics(ctx, snapshot, reports, pkg)
@@ -142,21 +141,17 @@ func diagnostics(ctx context.Context, snapshot Snapshot, reports map[FileIdentit
142141
_ = ctx // circumvent SA4006
143142
defer done()
144143

145-
diagSets := make(map[FileIdentity]*diagnosticSet)
144+
diagSets := make(map[span.URI]*diagnosticSet)
146145
for _, e := range pkg.GetErrors() {
147146
diag := &Diagnostic{
148147
Message: e.Message,
149148
Range: e.Range,
150149
Severity: protocol.SeverityError,
151150
}
152-
fh, err := snapshot.GetFile(e.URI)
153-
if err != nil {
154-
return false, err
155-
}
156-
set, ok := diagSets[fh.Identity()]
151+
set, ok := diagSets[e.URI]
157152
if !ok {
158153
set = &diagnosticSet{}
159-
diagSets[fh.Identity()] = set
154+
diagSets[e.URI] = set
160155
}
161156
switch e.Kind {
162157
case ParseError:
@@ -171,7 +166,7 @@ func diagnostics(ctx context.Context, snapshot Snapshot, reports map[FileIdentit
171166
}
172167
}
173168
var nonEmptyDiagnostics bool // track if we actually send non-empty diagnostics
174-
for fileID, set := range diagSets {
169+
for uri, set := range diagSets {
175170
// Don't report type errors if there are parse errors or list errors.
176171
diags := set.typeErrors
177172
if len(set.parseErrors) > 0 {
@@ -182,7 +177,9 @@ func diagnostics(ctx context.Context, snapshot Snapshot, reports map[FileIdentit
182177
if len(diags) > 0 {
183178
nonEmptyDiagnostics = true
184179
}
185-
addReports(ctx, snapshot, reports, fileID, diags...)
180+
if err := addReports(ctx, snapshot, reports, uri, diags...); err != nil {
181+
return false, err
182+
}
186183
}
187184
return nonEmptyDiagnostics, nil
188185
}
@@ -210,59 +207,53 @@ func analyses(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][
210207
if onlyDeletions(e.SuggestedFixes) {
211208
tags = append(tags, protocol.Unnecessary)
212209
}
213-
fh, err := snapshot.GetFile(e.URI)
214-
if err != nil {
215-
return err
216-
}
217-
addReports(ctx, snapshot, reports, fh.Identity(), &Diagnostic{
210+
if err := addReports(ctx, snapshot, reports, e.URI, &Diagnostic{
218211
Range: e.Range,
219212
Message: e.Message,
220213
Source: e.Category,
221214
Severity: protocol.SeverityWarning,
222215
Tags: tags,
223216
SuggestedFixes: e.SuggestedFixes,
224217
Related: e.Related,
225-
})
218+
}); err != nil {
219+
return err
220+
}
226221
}
227222
return nil
228223
}
229224

230-
func clearReports(snapshot Snapshot, reports map[FileIdentity][]Diagnostic, fileID FileIdentity) {
231-
if snapshot.View().Ignore(fileID.URI) {
232-
return
225+
func clearReports(snapshot Snapshot, reports map[FileIdentity][]Diagnostic, uri span.URI) error {
226+
if snapshot.View().Ignore(uri) {
227+
return nil
233228
}
234-
reports[fileID] = []Diagnostic{}
229+
fh, err := snapshot.GetFile(uri)
230+
if err != nil {
231+
return err
232+
}
233+
reports[fh.Identity()] = []Diagnostic{}
234+
return nil
235235
}
236236

237-
func addReports(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]Diagnostic, fileID FileIdentity, diagnostics ...*Diagnostic) error {
238-
if snapshot.View().Ignore(fileID.URI) {
237+
func addReports(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]Diagnostic, uri span.URI, diagnostics ...*Diagnostic) error {
238+
if snapshot.View().Ignore(uri) {
239239
return nil
240240
}
241-
if _, ok := reports[fileID]; !ok {
242-
return errors.Errorf("diagnostics for unexpected file %s", fileID.URI)
241+
fh, err := snapshot.GetFile(uri)
242+
if err != nil {
243+
return err
244+
}
245+
if _, ok := reports[fh.Identity()]; !ok {
246+
return errors.Errorf("diagnostics for unexpected file %s", uri)
243247
}
244248
for _, diag := range diagnostics {
245249
if diag == nil {
246250
continue
247251
}
248-
reports[fileID] = append(reports[fileID], *diag)
252+
reports[fh.Identity()] = append(reports[fh.Identity()], *diag)
249253
}
250254
return nil
251255
}
252256

253-
func singleDiagnostic(fileID FileIdentity, format string, a ...interface{}) map[FileIdentity][]Diagnostic {
254-
return map[FileIdentity][]Diagnostic{
255-
fileID: []Diagnostic{
256-
{
257-
Source: "gopls",
258-
Range: protocol.Range{},
259-
Message: fmt.Sprintf(format, a...),
260-
Severity: protocol.SeverityError,
261-
},
262-
},
263-
}
264-
}
265-
266257
// onlyDeletions returns true if all of the suggested fixes are deletions.
267258
func onlyDeletions(fixes []SuggestedFix) bool {
268259
for _, fix := range fixes {

0 commit comments

Comments
 (0)