From 6c6fd98e388a1d3623a0aa4357335fed5d3c2222 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Fri, 19 Jun 2020 16:31:22 -0400 Subject: [PATCH] internal/lsp: fix panic in computing diagnostics for module We were returning empty diagnostics for an empty file. Fixes golang/go#39696. Change-Id: Idfd50980f4bf771f76ad7b7fb6180a5f712a92bf Reviewed-on: https://go-review.googlesource.com/c/tools/+/239040 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick TryBot-Result: Gobot Gobot --- internal/lsp/diagnostics.go | 4 ++++ internal/lsp/mod/diagnostics.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index 6dc9aeddc5..4c9fe2556a 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -74,6 +74,10 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA } modURI := snapshot.View().ModFile() for id, diags := range reports { + if id.URI == "" { + event.Error(ctx, "missing URI for module diagnostics", fmt.Errorf("empty URI"), tag.Directory.Of(snapshot.View().Folder().Filename())) + continue + } if id.URI != modURI { panic(fmt.Sprintf("expected module diagnostics report for %q, got %q", modURI, id.URI)) } diff --git a/internal/lsp/mod/diagnostics.go b/internal/lsp/mod/diagnostics.go index bb2d9fdc7c..84320dce87 100644 --- a/internal/lsp/mod/diagnostics.go +++ b/internal/lsp/mod/diagnostics.go @@ -18,6 +18,10 @@ import ( func Diagnostics(ctx context.Context, snapshot source.Snapshot) (map[source.FileIdentity][]*source.Diagnostic, map[string]*modfile.Require, error) { uri := snapshot.View().ModFile() + if uri == "" { + return nil, nil, nil + } + ctx, done := event.Start(ctx, "mod.Diagnostics", tag.URI.Of(uri)) defer done()