From de9f36f276cf294346b793d64bbb926a5c74c519 Mon Sep 17 00:00:00 2001 From: Charlie Egan Date: Tue, 24 Feb 2026 16:52:27 +0000 Subject: [PATCH] lsp/testing: Ensure test locations sent at boot This was missed in https://github.com/open-policy-agent/regal/pull/1888 This makes test locations more similar to diagnostics. Signed-off-by: Charlie Egan --- internal/lsp/server.go | 19 ++++++++++++++++++- internal/lsp/testing.go | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/lsp/server.go b/internal/lsp/server.go index f2d0db02..2992fda0 100644 --- a/internal/lsp/server.go +++ b/internal/lsp/server.go @@ -2150,12 +2150,23 @@ func (l *LanguageServer) loadWorkspaceContents(ctx context.Context, newOnly bool return nil } - if _, err = updateParse(ctx, l.parseOpts(fileURI, l.builtinsForCurrentCapabilities())); err != nil { + parseSuccess, err := updateParse(ctx, l.parseOpts(fileURI, l.builtinsForCurrentCapabilities())) + if err != nil { failed = append(failed, fileLoadFailure{URI: fileURI, Error: fmt.Errorf("failed to update parse: %w", err)}) return nil // continue processing other files } + if parseSuccess { + l.testLocationJobs <- lintFileJob{Reason: "server initialized", URI: fileURI} + } else { + // this is covering the case where the client starts and the state + // is different (the client remembers test locations and state). + if err := l.sendTestLocations(ctx, fileURI, []any{}); err != nil { + l.log.Message("failed to send empty test locations after parse failure: %s", err) + } + } + changedOrNewURIs = append(changedOrNewURIs, fileURI) return nil @@ -2221,6 +2232,12 @@ func (l *LanguageServer) handleWorkspaceDidChangeWatchedFiles( } func (l *LanguageServer) sendFileDiagnostics(ctx context.Context, fileURI string) { + if l.conn == nil { + l.log.Debug("sendFileDiagnostics called with no connection: %s", fileURI) + + return + } + // first, set the diagnostics for the file to the current parse errors fileDiags, _ := l.cache.GetParseErrors(fileURI) if len(fileDiags) == 0 { diff --git a/internal/lsp/testing.go b/internal/lsp/testing.go index 1fc0d6b6..389e37c4 100644 --- a/internal/lsp/testing.go +++ b/internal/lsp/testing.go @@ -78,6 +78,12 @@ func (l *LanguageServer) processTestLocationsUpdate(ctx context.Context, fileURI } func (l *LanguageServer) sendTestLocations(ctx context.Context, fileURI string, locations any) error { + if l.conn == nil { + l.log.Debug("sendTestLocations called with no connection: %s", fileURI) + + return nil + } + params := map[string]any{ "uri": fileURI, "locations": locations,