on main help sign in
fix(lsp): fix multiple bugs in lsp client lifecycle and handlers (#2305)
62b8c0bhuaiyuWangh
internal/lsp/client.go+1−10
@@ -277,10 +277,6 @@ func (c *Client) WaitForServerReady(ctx context.Context) error {
277 // Set initial state
277 // Set initial state
278 c.SetServerState(StateStarting)
278 c.SetServerState(StateStarting)
279
279
280− // Create a context with timeout
281− ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
282− defer cancel()
283−
284 // Try to ping the server with a simple request
280 // Try to ping the server with a simple request
285 ticker := time.NewTicker(500 * time.Millisecond)
281 ticker := time.NewTicker(500 * time.Millisecond)
286 defer ticker.Stop()
282 defer ticker.Stop()
@@ -495,14 +491,9 @@ func (c *Client) RegisterServerRequestHandler(method string, handler transport.H
495
491
496// openKeyConfigFiles opens important configuration files that help initialize the server.
492// openKeyConfigFiles opens important configuration files that help initialize the server.
497func (c *Client) openKeyConfigFiles(ctx context.Context) {
493func (c *Client) openKeyConfigFiles(ctx context.Context) {
498− wd, err := os.Getwd()
499− if err != nil {
500− return
501− }
502−
503 // Try to open each file, ignoring errors if they don't exist
494 // Try to open each file, ignoring errors if they don't exist
504 for _, file := range c.config.RootMarkers {
495 for _, file := range c.config.RootMarkers {
505− file = filepath.Join(wd, file)
496+ file = filepath.Join(c.cwd, file)
506 if _, err := os.Stat(file); err == nil {
497 if _, err := os.Stat(file); err == nil {
507 // File exists, try to open it
498 // File exists, try to open it
508 if err := c.OpenFile(ctx, file); err != nil {
499 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
81func HandleServerMessage(_ context.Context, method string, params json.RawMessage) {
81func HandleServerMessage(_ context.Context, method string, params json.RawMessage) {
82 var msg protocol.ShowMessageParams
82 var msg protocol.ShowMessageParams
83 if err := json.Unmarshal(params, &msg); err != nil {
83 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)
85 return
85 return
86 }
86 }
87
87
internal/lsp/manager.go+1
@@ -229,6 +229,7 @@ func (s *Manager) startServer(ctx context.Context, name, filepath string, server
229 if _, err := client.Initialize(initCtx, s.cfg.WorkingDir()); err != nil {
229 if _, err := client.Initialize(initCtx, s.cfg.WorkingDir()); err != nil {
230 slog.Error("LSP client initialization failed", "name", name, "error", err)
230 slog.Error("LSP client initialization failed", "name", name, "error", err)
231 client.Close(ctx)
231 client.Close(ctx)
232+ s.clients.Del(name)
232 return
233 return
233 }
234 }
234
235