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>
diff --git a/gopls/internal/regtest/modfile_test.go b/gopls/internal/regtest/modfile_test.go
index 22d780a..f71acec 100644
--- a/gopls/internal/regtest/modfile_test.go
+++ b/gopls/internal/regtest/modfile_test.go
@@ -630,3 +630,23 @@
)
})
}
+
+func TestModTypoDiagnostic(t *testing.T) {
+ const mod = `
+-- go.mod --
+module mod.com
+
+go 1.12
+-- main.go --
+package main
+
+func main() {}
+`
+ run(t, mod, func(t *testing.T, env *Env) {
+ env.OpenFile("go.mod")
+ env.RegexpReplace("go.mod", "module", "modul")
+ env.Await(
+ env.DiagnosticAtRegexp("go.mod", "modul"),
+ )
+ })
+}
diff --git a/internal/lsp/cache/mod.go b/internal/lsp/cache/mod.go
index 0504799..ef01864 100644
--- a/internal/lsp/cache/mod.go
+++ b/internal/lsp/cache/mod.go
@@ -142,7 +142,7 @@
// The error returned from the modfile package only returns a line number,
// so we assume that the diagnostic should be for the entire line.
endOfLine := len(lines[line-1])
- sOffset, err := m.Converter.ToOffset(line, 0)
+ sOffset, err := m.Converter.ToOffset(line, 1)
if err != nil {
return nil, err
}
diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go
index f821361..e8e2868 100644
--- a/internal/lsp/diagnostics.go
+++ b/internal/lsp/diagnostics.go
@@ -194,12 +194,10 @@
if err != nil {
// Some error messages can also be displayed as diagnostics.
if errList := (*source.ErrorList)(nil); errors.As(err, &errList) {
- if err := errorsToDiagnostic(ctx, snapshot, *errList, reports); err == nil {
- return reports, nil
- }
+ _ = errorsToDiagnostic(ctx, snapshot, *errList, reports)
}
event.Error(ctx, "errors diagnosing workspace", err, tag.Snapshot.Of(snapshot.ID()), tag.Directory.Of(snapshot.View().Folder()))
- return nil, nil
+ return reports, nil
}
var (