@@ -85,7 +85,7 @@ class ContextFailed:
8585 """State when context failed to load with an error message."""
8686
8787 error : ContextFailedError
88- context : t . Optional [ Context ] = None
88+ context : Context
8989 version_id : str = field (default_factory = lambda : str (uuid .uuid4 ()))
9090
9191
@@ -227,24 +227,26 @@ def _reload_context_and_publish_diagnostics(
227227 self , ls : LanguageServer , uri : URI , document_uri : str
228228 ) -> None :
229229 """Helper method to reload context and publish diagnostics."""
230+ context : t .Optional [Context ] = None
230231 if isinstance (self .context_state , NoContext ):
231232 pass
232233 elif isinstance (self .context_state , ContextFailed ):
233234 if self .context_state .context :
235+ context = self .context_state .context
234236 try :
235- self . context_state . context .load ()
237+ context .load ()
236238 # Creating a new LSPContext will naturally create fresh caches
237- self .context_state = ContextLoaded (
238- lsp_context = LSPContext (self .context_state .context )
239- )
239+ self .context_state = ContextLoaded (lsp_context = LSPContext (context ))
240240 except Exception as e :
241241 ls .log_trace (f"Error loading context: { e } " )
242- context = (
242+ error_context = (
243243 self .context_state .context
244244 if hasattr (self .context_state , "context" )
245245 else None
246246 )
247- self .context_state = ContextFailed (error = e , context = context )
247+ if error_context is None :
248+ raise RuntimeError (f"Context should always be set." )
249+ self .context_state = ContextFailed (error = e , context = error_context )
248250 else :
249251 # If there's no context, reset to NoContext and try to create one from scratch
250252 ls .log_trace ("No partial context available, attempting fresh creation" )
@@ -860,17 +862,20 @@ def _create_lsp_context(self, paths: t.List[Path]) -> t.Optional[LSPContext]:
860862 Returns:
861863 A new LSPContext instance wrapping the created context, or None if creation fails
862864 """
865+ context = None
863866 try :
864867 if isinstance (self .context_state , NoContext ):
865- context = self .context_class (paths = paths )
868+ context = self .context_class (paths = paths , load = False )
869+ context .load ()
866870 loaded_sqlmesh_message (self .server , paths [0 ])
867871 elif isinstance (self .context_state , ContextFailed ):
868872 if self .context_state .context :
869873 context = self .context_state .context
870874 context .load ()
871875 else :
872876 # If there's no context (initial creation failed), try creating again
873- context = self .context_class (paths = paths )
877+ context = self .context_class (paths = paths , load = False )
878+ context .load ()
874879 loaded_sqlmesh_message (self .server , paths [0 ])
875880 else :
876881 context = self .context_state .lsp_context .context
@@ -889,11 +894,12 @@ def _create_lsp_context(self, paths: t.List[Path]) -> t.Optional[LSPContext]:
889894 self .server .log_trace (f"Error creating context: { e } " )
890895 # Store the error in context state so subsequent requests show the actual error
891896 # Try to preserve any partially loaded context if it exists
892- context = None
893897 if isinstance (self .context_state , ContextLoaded ):
894898 context = self .context_state .lsp_context .context
895899 elif isinstance (self .context_state , ContextFailed ) and self .context_state .context :
896900 context = self .context_state .context
901+ if context is None :
902+ raise RuntimeError ("Context should always be set." )
897903 self .context_state = ContextFailed (error = e , context = context )
898904 return None
899905
0 commit comments