From 464a8874ca3c65ed47a35e2c0f997b57f2ccdc9d Mon Sep 17 00:00:00 2001 From: pjw Date: Wed, 15 Sep 2021 12:41:12 -0400 Subject: [PATCH] internal/lsp/templates: replace panic by logging There were two panics that cannot happen (meaning they are inconsistent woith my understanding of the documentation). These are being replaced by logging, as if they happen, they will happen deterministically again if the user restarts gopls and does the same thing. Change-Id: Ia405ad2883cedcf3de6cb1376eedb0f9250c61e0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/350110 Trust: Peter Weinberger Run-TryBot: Peter Weinberger gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Rebecca Stambler --- internal/lsp/template/symbols.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/lsp/template/symbols.go b/internal/lsp/template/symbols.go index 856f6e335f..2730ebffba 100644 --- a/internal/lsp/template/symbols.go +++ b/internal/lsp/template/symbols.go @@ -6,10 +6,12 @@ package template import ( "bytes" + "context" "fmt" "text/template/parse" "unicode/utf8" + "golang.org/x/tools/internal/event" "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/source" ) @@ -49,10 +51,16 @@ func (p *Parsed) fields(flds []string, x parse.Node) []symbol { lookfor += "." + f // quadratic, but probably ok } default: - panic(fmt.Sprintf("%T unexpected in fields()", x)) + // If these happen they will happen even if gopls is restarted + // and the users does the same thing, so it is better not to panic. + // context.Background() is used because we don't have access + // to any other context. [we could, but it would be complicated] + event.Log(context.Background(), fmt.Sprintf("%T unexpected in fields()", x)) + return nil } if len(lookfor) == 0 { - panic(fmt.Sprintf("no strings in fields() %#v", x)) + event.Log(context.Background(), fmt.Sprintf("no strings in fields() %#v", x)) + return nil } startsAt := int(x.Position()) ix := bytes.Index(p.buf[startsAt:], []byte(lookfor)) // HasPrefix? PJW?