{"record":{"id":"39a529fda69b2f5b","repo":"charmbracelet/crush","slug":"failed-to-initialize-lsp-client-w","errorCode":null,"errorMessage":"failed to initialize lsp client: %w","messagePattern":"failed to initialize lsp client: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lsp/client.go","lineNumber":286,"sourceCode":"\tc.diagCountsVersion = 0\n\n\tif err := c.createPowernapClient(); err != nil {\n\t\treturn err\n\t}\n\n\tinitCtx, cancel := context.WithTimeout(c.ctx, 30*time.Second)\n\tdefer cancel()\n\n\tc.SetServerState(StateStarting)\n\n\t// Register handlers before Initialize so servers that send\n\t// requests during the handshake (e.g. window/workDoneProgress/create)\n\t// don't crash on an unhandled response.\n\tc.registerHandlers()\n\n\tif err := c.client.Initialize(initCtx, false); err != nil {\n\t\tc.SetServerState(StateError)\n\t\treturn fmt.Errorf(\"failed to initialize lsp client: %w\", err)\n\t}\n\n\tif err := c.WaitForServerReady(initCtx); err != nil {\n\t\tslog.Error(\"Server failed to become ready after restart\", \"name\", c.name, \"error\", err)\n\t\tc.SetServerState(StateError)\n\t\treturn err\n\t}\n\n\tfor _, uri := range openFiles {\n\t\tif err := c.OpenFile(initCtx, uri); err != nil {\n\t\t\tslog.Warn(\"Failed to reopen file after restart\", \"file\", uri, \"error\", err)\n\t\t}\n\t}\n\treturn nil\n}\n\n// ServerState represents the state of an LSP server\ntype ServerState int","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/lsp/client.go#L268-L304","documentation":"Client.Restart re-registers handlers and calls c.client.Initialize to perform the LSP initialize handshake. If the initialize request fails (or errors during the exchange), the client transitions to StateError and this wrapped error is returned. It means the server process started but the LSP initialization sequence did not complete.","triggerScenarios":"Calling Restart (or New's startup path) where c.client.Initialize(initCtx, false) returns an error — server crashed on startup, responded with an error, closed the connection during handshake, or the initCtx expired mid-initialization.","commonSituations":"LSP server version incompatible with the request capabilities; server immediately exits due to bad args/env; server binary is a wrapper script that fails; context deadline too short for slow-starting servers.","solutions":["Run the server binary manually with the same args and cwd to see its startup errors.","Check the server logs/verbose output for why initialize failed or crashed.","Increase the initialization context timeout in Restart.","Pin a known-compatible LSP server version if a recent upgrade broke the handshake.","Verify config args/env passed to the server are correct for its version."],"exampleFix":"// before\ninitCtx, cancel := context.WithTimeout(ctx, 5*time.Second)\n\n// after\ninitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)","handlingStrategy":"retry","validationCode":"// Ensure the server can at least start before initializing\nout, err := exec.CommandContext(ctx, cmd, args...).CombinedOutput()\n_ = out // inspect for immediate startup errors if err != nil","typeGuard":"func initState(err error) bool {\n    return client.State() == lsp.StateError // set by Restart on initialize failure\n}","tryCatchPattern":"if err := client.Restart(ctx); err != nil {\n    if strings.Contains(err.Error(), \"failed to initialize lsp client\") {\n        slog.Warn(\"LSP initialize failed; retrying once\", \"error\", err)\n        time.Sleep(2 * time.Second)\n        if retryErr := client.Restart(ctx); retryErr != nil {\n            return retryErr\n        }\n    }\n}","preventionTips":["Use generous initialization timeouts, especially on large workspaces.","Capture server stderr to a log file so initialize failures are diagnosable.","After server upgrades, re-test the handshake before deploying config changes."],"tags":["lsp","initialization","handshake"],"backgroundTag":"lsp-initialization-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}