From 4911b5da93e52b1fb28ab8328135ac3fa0a6c9a0 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 12 Dec 2023 21:14:56 +0000 Subject: [PATCH] fix: diagnostics set to nil, which causes issues in some LSP clients (and is against spec) --- .version | 2 +- cmd/templ/lspcmd/proxy/diagnosticcache.go | 22 +++++++++++++++++++++- cmd/templ/lspcmd/proxy/server.go | 6 ++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/.version b/.version index 894b1133a..9993225f7 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.480 \ No newline at end of file +0.2.485 \ No newline at end of file diff --git a/cmd/templ/lspcmd/proxy/diagnosticcache.go b/cmd/templ/lspcmd/proxy/diagnosticcache.go index 3a33979c9..00ec5c21d 100644 --- a/cmd/templ/lspcmd/proxy/diagnosticcache.go +++ b/cmd/templ/lspcmd/proxy/diagnosticcache.go @@ -1,8 +1,9 @@ package proxy import ( - lsp "github.com/a-h/protocol" "sync" + + lsp "github.com/a-h/protocol" ) func NewDiagnosticCache() *DiagnosticCache { @@ -22,20 +23,39 @@ type DiagnosticCache struct { cache map[string]fileDiagnostic } +func zeroLengthSliceIfNil(diags []lsp.Diagnostic) []lsp.Diagnostic { + if diags == nil { + return make([]lsp.Diagnostic, 0) + } + return diags +} + func (dc *DiagnosticCache) AddTemplDiagnostics(uri string, goDiagnostics []lsp.Diagnostic) []lsp.Diagnostic { + goDiagnostics = zeroLengthSliceIfNil(goDiagnostics) dc.m.Lock() defer dc.m.Unlock() diag := dc.cache[uri] diag.goplsDiagnostics = goDiagnostics + diag.templDiagnostics = zeroLengthSliceIfNil(diag.templDiagnostics) dc.cache[uri] = diag return append(diag.templDiagnostics, goDiagnostics...) } +func (dc *DiagnosticCache) ClearTemplDiagnostics(uri string) { + dc.m.Lock() + defer dc.m.Unlock() + diag := dc.cache[uri] + diag.templDiagnostics = make([]lsp.Diagnostic, 0) + dc.cache[uri] = diag +} + func (dc *DiagnosticCache) AddGoDiagnostics(uri string, templDiagnostics []lsp.Diagnostic) []lsp.Diagnostic { + templDiagnostics = zeroLengthSliceIfNil(templDiagnostics) dc.m.Lock() defer dc.m.Unlock() diag := dc.cache[uri] diag.templDiagnostics = templDiagnostics + diag.goplsDiagnostics = zeroLengthSliceIfNil(diag.goplsDiagnostics) dc.cache[uri] = diag return append(diag.goplsDiagnostics, templDiagnostics...) } diff --git a/cmd/templ/lspcmd/proxy/server.go b/cmd/templ/lspcmd/proxy/server.go index 978fe0c75..524ab8acd 100644 --- a/cmd/templ/lspcmd/proxy/server.go +++ b/cmd/templ/lspcmd/proxy/server.go @@ -182,9 +182,11 @@ func (p *Server) parseTemplate(ctx context.Context, uri uri.URI, templateText st return } // Clear templ diagnostics. + p.DiagnosticCache.ClearTemplDiagnostics(string(uri)) err = p.Client.PublishDiagnostics(ctx, &lsp.PublishDiagnosticsParams{ - URI: uri, - Diagnostics: p.DiagnosticCache.AddGoDiagnostics(string(uri), nil), + URI: uri, + // Cannot be nil as per https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#publishDiagnosticsParams + Diagnostics: []lsp.Diagnostic{}, }) if err != nil { p.Log.Error("failed to publish diagnostics", zap.Error(err))