Skip to content

Commit 780cb80

Browse files
committed
internal/lsp: fix go.mod parse error parsing to show diagnostics
This change adds support for showing go.mod parse errors as diagnostics. There was previously a bug in parsing these error messages, and the diagnostic reports were also not being published if the IWL failed. Change-Id: I56197b91c49fb68e35d8886175435d5c38bfb8c2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/272089 Trust: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
1 parent 20be4ac commit 780cb80

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

gopls/internal/regtest/modfile_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,23 @@ func main() {
630630
)
631631
})
632632
}
633+
634+
func TestModTypoDiagnostic(t *testing.T) {
635+
const mod = `
636+
-- go.mod --
637+
module mod.com
638+
639+
go 1.12
640+
-- main.go --
641+
package main
642+
643+
func main() {}
644+
`
645+
run(t, mod, func(t *testing.T, env *Env) {
646+
env.OpenFile("go.mod")
647+
env.RegexpReplace("go.mod", "module", "modul")
648+
env.Await(
649+
env.DiagnosticAtRegexp("go.mod", "modul"),
650+
)
651+
})
652+
}

internal/lsp/cache/mod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func extractModParseErrors(uri span.URI, m *protocol.ColumnMapper, parseErr erro
142142
// The error returned from the modfile package only returns a line number,
143143
// so we assume that the diagnostic should be for the entire line.
144144
endOfLine := len(lines[line-1])
145-
sOffset, err := m.Converter.ToOffset(line, 0)
145+
sOffset, err := m.Converter.ToOffset(line, 1)
146146
if err != nil {
147147
return nil, err
148148
}

internal/lsp/diagnostics.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,10 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA
194194
if err != nil {
195195
// Some error messages can also be displayed as diagnostics.
196196
if errList := (*source.ErrorList)(nil); errors.As(err, &errList) {
197-
if err := errorsToDiagnostic(ctx, snapshot, *errList, reports); err == nil {
198-
return reports, nil
199-
}
197+
_ = errorsToDiagnostic(ctx, snapshot, *errList, reports)
200198
}
201199
event.Error(ctx, "errors diagnosing workspace", err, tag.Snapshot.Of(snapshot.ID()), tag.Directory.Of(snapshot.View().Folder()))
202-
return nil, nil
200+
return reports, nil
203201
}
204202

205203
var (

0 commit comments

Comments
 (0)