on main help sign in
fix(lsp): fix multiple bugs in lsp client lifecycle and handlers (#2305)

- Remove dead client from map when initialization fails to allow retry - Use client's cwd field instead of os.Getwd() in openKeyConfigFiles - Fix slog key-value pair in HandleServerMessage error logging - Remove redundant hardcoded timeout in WaitForServerReady

commit62b8c0b3def656ebbdb4f2e39e5ea4e97dfbd975 authorhuaiyuWangh <34158348+huaiyuWangh@users.noreply.github.com> dateWed 25 Feb 2026, 21:19 (8 months ago) parent643919d
3 files changed+3−11
internal/lsp/client.go+1−10 internal/lsp/handlers.go+1−1 internal/lsp/manager.go+1
internal/lsp/client.go+1−10
@@ -277,10 +277,6 @@ func (c *Client) WaitForServerReady(ctx context.Context) error {
277277 // Set initial state
278278 c.SetServerState(StateStarting)
279279
280− // Create a context with timeout
281− ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
282− defer cancel()
283−
284280 // Try to ping the server with a simple request
285281 ticker := time.NewTicker(500 * time.Millisecond)
286282 defer ticker.Stop()
@@ -495,14 +491,9 @@ func (c *Client) RegisterServerRequestHandler(method string, handler transport.H
495491
496492// openKeyConfigFiles opens important configuration files that help initialize the server.
497493func (c *Client) openKeyConfigFiles(ctx context.Context) {
498− wd, err := os.Getwd()
499− if err != nil {
500− return
501− }
502−
503494 // Try to open each file, ignoring errors if they don't exist
504495 for _, file := range c.config.RootMarkers {
505− file = filepath.Join(wd, file)
496+ file = filepath.Join(c.cwd, file)
506497 if _, err := os.Stat(file); err == nil {
507498 // File exists, try to open it
508499 if err := c.OpenFile(ctx, file); err != nil {
internal/lsp/handlers.go+1−1
@@ -81,7 +81,7 @@ func notifyFileWatchRegistration(id string, watchers []protocol.FileSystemWatche
8181func HandleServerMessage(_ context.Context, method string, params json.RawMessage) {
8282 var msg protocol.ShowMessageParams
8383 if err := json.Unmarshal(params, &msg); err != nil {
84− slog.Debug("Server message", "type", msg.Type, "message", msg.Message)
84+ slog.Debug("Error unmarshal server message", "error", err)
8585 return
8686 }
8787
internal/lsp/manager.go+1
@@ -229,6 +229,7 @@ func (s *Manager) startServer(ctx context.Context, name, filepath string, server
229229 if _, err := client.Initialize(initCtx, s.cfg.WorkingDir()); err != nil {
230230 slog.Error("LSP client initialization failed", "name", name, "error", err)
231231 client.Close(ctx)
232+ s.clients.Del(name)
232233 return
233234 }
234235