From d34d24c8c0703fe10ec2f5ee1d3fec9e808a26f2 Mon Sep 17 00:00:00 2001 From: jthurlburt Date: Sat, 28 Mar 2026 14:05:43 -0500 Subject: [PATCH] fix(lsp): read self.context_state instead of stale local variable in _context_get_or_load _context_get_or_load captures self.context_state into a local variable `state` at the top of the method. After _create_lsp_context() or _ensure_context_for_document() successfully updates self.context_state to ContextLoaded, the final check on line 1077 reads the stale local `state` (still NoContext) instead of self.context_state. This causes the method to always raise RuntimeError("Context failed to load") on the first didOpen, even when context loading succeeds. The LSP logs "Loaded SQLMesh Context" then immediately crashes. In VS Code, this puts the client into a permanent "Client got disposed and can't be restarted" state. Signed-off-by: jthurlburt --- sqlmesh/lsp/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlmesh/lsp/main.py b/sqlmesh/lsp/main.py index 71dc5e1e2b..b5623f3ff8 100755 --- a/sqlmesh/lsp/main.py +++ b/sqlmesh/lsp/main.py @@ -1074,8 +1074,8 @@ def _context_get_or_load(self, document_uri: t.Optional[URI] = None) -> LSPConte loaded_sqlmesh_message(self.server) else: self._ensure_context_for_document(document_uri) - if isinstance(state, ContextLoaded): - return state.lsp_context + if isinstance(self.context_state, ContextLoaded): + return self.context_state.lsp_context raise RuntimeError("Context failed to load") def _ensure_context_for_document(