on main help sign in
fix: address `nil` pointer dereference panics on lsp client methods (#2256)

Co-authored-by: andreynering <7011819+andreynering@users.noreply.github.com>

commit9862d3a5552d3246eaff554cca83f2b4eee42ffd authorCopilot <198982749+Copilot@users.noreply.github.com> dateThu 19 Feb 2026, 10:56 (8 months ago) parent84fe27b
2 files changed+33
internal/lsp/client.go+18 internal/lsp/client_test.go+15
internal/lsp/client.go+18
@@ -324,6 +324,9 @@ type OpenFileInfo struct {
324324// HandlesFile checks if this LSP client handles the given file based on its
325325// extension and whether it's within the working directory.
326326func (c *Client) HandlesFile(path string) bool {
327+ if c == nil {
328+ return false
329+ }
327330 if !fsext.HasPrefix(path, c.cwd) {
328331 slog.Debug("File outside workspace", "name", c.name, "file", path, "workDir", c.cwd)
329332 return false
@@ -364,6 +367,9 @@ func (c *Client) OpenFile(ctx context.Context, filepath string) error {
364367
365368// NotifyChange notifies the server about a file change.
366369func (c *Client) NotifyChange(ctx context.Context, filepath string) error {
370+ if c == nil {
371+ return nil
372+ }
367373 uri := string(protocol.URIFromPath(filepath))
368374
369375 content, err := os.ReadFile(filepath)
@@ -420,12 +426,18 @@ func (c *Client) GetFileDiagnostics(uri protocol.DocumentURI) []protocol.Diagnos
420426
421427// GetDiagnostics returns all diagnostics for all files.
422428func (c *Client) GetDiagnostics() map[protocol.DocumentURI][]protocol.Diagnostic {
429+ if c == nil {
430+ return nil
431+ }
423432 return c.diagnostics.Copy()
424433}
425434
426435// GetDiagnosticCounts returns cached diagnostic counts by severity.
427436// Uses the VersionedMap version to avoid recomputing on every call.
428437func (c *Client) GetDiagnosticCounts() DiagnosticCounts {
438+ if c == nil {
439+ return DiagnosticCounts{}
440+ }
429441 currentVersion := c.diagnostics.Version()
430442
431443 c.diagCountsMu.Lock()
@@ -459,6 +471,9 @@ func (c *Client) GetDiagnosticCounts() DiagnosticCounts {
459471
460472// OpenFileOnDemand opens a file only if it's not already open.
461473func (c *Client) OpenFileOnDemand(ctx context.Context, filepath string) error {
474+ if c == nil {
475+ return nil
476+ }
462477 // Check if the file is already open
463478 if c.IsFileOpen(filepath) {
464479 return nil
@@ -501,6 +516,9 @@ func (c *Client) openKeyConfigFiles(ctx context.Context) {
501516
502517// WaitForDiagnostics waits until diagnostics change or the timeout is reached.
503518func (c *Client) WaitForDiagnostics(ctx context.Context, d time.Duration) {
519+ if c == nil {
520+ return
521+ }
504522 ticker := time.NewTicker(200 * time.Millisecond)
505523 defer ticker.Stop()
506524 timeout := time.After(d)
internal/lsp/client_test.go+15
@@ -3,9 +3,11 @@ package lsp
33import (
44 "context"
55 "testing"
6+ "time"
67
78 "github.com/charmbracelet/crush/internal/config"
89 "github.com/charmbracelet/crush/internal/env"
10+ "github.com/stretchr/testify/require"
911)
1012
1113func TestClient(t *testing.T) {
@@ -55,3 +57,16 @@ func TestClient(t *testing.T) {
5557 t.Logf("Close failed as expected with dummy command: %v", err)
5658 }
5759}
60+
61+func TestNilClient(t *testing.T) {
62+ t.Parallel()
63+
64+ var c *Client
65+
66+ require.False(t, c.HandlesFile("/some/file.go"))
67+ require.Equal(t, DiagnosticCounts{}, c.GetDiagnosticCounts())
68+ require.Nil(t, c.GetDiagnostics())
69+ require.Nil(t, c.OpenFileOnDemand(context.Background(), "/some/file.go"))
70+ require.Nil(t, c.NotifyChange(context.Background(), "/some/file.go"))
71+ c.WaitForDiagnostics(context.Background(), time.Second)
72+}