Skip to content

fix(lsp): read self.context_state instead of stale local variable in _context_get_or_load#5747

Open
jthurlburt wants to merge 2 commits into
SQLMesh:mainfrom
jthurlburt:fix/lsp-stale-context-state
Open

fix(lsp): read self.context_state instead of stale local variable in _context_get_or_load#5747
jthurlburt wants to merge 2 commits into
SQLMesh:mainfrom
jthurlburt:fix/lsp-stale-context-state

Conversation

@jthurlburt
Copy link
Copy Markdown

@jthurlburt jthurlburt commented Mar 28, 2026

Summary

  • _context_get_or_load captures self.context_state into local variable state at line 1065
  • After _create_lsp_context() or _ensure_context_for_document() successfully updates self.context_state to ContextLoaded, the final check at 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

Impact

The LSP logs "Loaded SQLMesh Context" then immediately crashes on the first textDocument/didOpen. In VS Code, this puts the extension client into a permanent "Client got disposed and can't be restarted" state. Affects both the specified_paths code path (via initializationOptions.project_paths) and the _ensure_context_for_document fallback path.

Fix

Read self.context_state instead of the stale local state variable on lines 1077-1078.

Regression test

Added two tests to tests/lsp/test_context.py that call _context_get_or_load starting from NoContext, covering both affected code paths:

  • test_context_get_or_load_from_no_context_with_specified_paths — the specified_paths path
  • test_context_get_or_load_from_no_context_via_workspace_folder — the _ensure_context_for_document fallback path

Both fail with RuntimeError("Context failed to load") on the unpatched code and pass with the fix.

Test Plan

  • Verified fix locally — LSP hover, go-to-definition, references, and diagnostics all work after patch
  • make style passes (ruff, ruff-format, mypy, valid migrations)
  • Confirmed bug is present on upstream main
  • Added regression tests that fail without the fix and pass with it
  • Rebased onto latest main and re-verified

@jthurlburt jthurlburt force-pushed the fix/lsp-stale-context-state branch from d34d24c to 053b774 Compare March 30, 2026 20:37
@jthurlburt jthurlburt force-pushed the fix/lsp-stale-context-state branch from 053b774 to ef925f8 Compare April 21, 2026 17:08
@StuffbyYuki
Copy link
Copy Markdown
Collaborator

@jthurlburt Thanks for the PR!

Approve with one optional ask question - do you think it'd be beneficial to add a small regression test in tests/lsp/ that calls _context_get_or_load starting from NoContext with specified_paths set? That would prevent this from coming back.

…_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 <jthurlburt818@gmail.com>
@jthurlburt jthurlburt force-pushed the fix/lsp-stale-context-state branch from aea2b0e to 56fe5e5 Compare June 7, 2026 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants