@@ -321,12 +321,13 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
321321 diagnostics = getattr (params .capabilities .text_document , "diagnostic" , None )
322322 if diagnostics :
323323 self .client_supports_pull_diagnostics = True
324- ls .log_trace ("Client supports pull diagnostics" )
324+ ls .show_message ("Client supports pull diagnostics" , types . MessageType . Info )
325325 else :
326326 self .client_supports_pull_diagnostics = False
327- ls .log_trace ("Client does not support pull diagnostics" )
327+ ls .show_message ("Client does not support pull diagnostics" , types . MessageType . Info )
328328 else :
329329 self .client_supports_pull_diagnostics = False
330+ ls .show_message ("Client capabilities not available" , types .MessageType .Info )
330331
331332 if params .workspace_folders :
332333 # Store all workspace folders for later use
@@ -354,15 +355,22 @@ def initialize(ls: LanguageServer, params: types.InitializeParams) -> None:
354355 @self .server .feature (types .TEXT_DOCUMENT_DID_OPEN )
355356 def did_open (ls : LanguageServer , params : types .DidOpenTextDocumentParams ) -> None :
356357 uri = URI (params .text_document .uri )
357- context = self ._context_get_or_load (ls , uri )
358-
359- # Only publish diagnostics if client doesn't support pull diagnostics
360- if not self .client_supports_pull_diagnostics :
361- diagnostics = context .lint_model (uri )
362- ls .publish_diagnostics (
363- params .text_document .uri ,
364- LSPContext .diagnostics_to_lsp_diagnostics (diagnostics ),
365- )
358+ try :
359+ context = self ._context_get_or_load (ls , uri )
360+
361+ # Only publish diagnostics if client doesn't support pull diagnostics
362+ if not self .client_supports_pull_diagnostics :
363+ diagnostics = context .lint_model (uri )
364+ ls .publish_diagnostics (
365+ params .text_document .uri ,
366+ LSPContext .diagnostics_to_lsp_diagnostics (diagnostics ),
367+ )
368+ except Exception as e :
369+ # Context loading failed, but don't crash the LSP
370+ ls .log_trace (f"Failed to load context in did_open: { e } " )
371+ # Optionally publish empty diagnostics to clear any existing ones
372+ if not self .client_supports_pull_diagnostics :
373+ ls .publish_diagnostics (params .text_document .uri , [])
366374
367375 @self .server .feature (types .TEXT_DOCUMENT_DID_SAVE )
368376 def did_save (ls : LanguageServer , params : types .DidSaveTextDocumentParams ) -> None :
@@ -769,7 +777,25 @@ def _get_diagnostics_for_uri(
769777 context = self ._context_get_or_load (ls , uri )
770778 diagnostics = context .lint_model (uri )
771779 return LSPContext .diagnostics_to_lsp_diagnostics (diagnostics ), 0
772- except Exception :
780+ except Exception as e :
781+ # Check if there's a failed context state with configuration errors for this URI
782+ if isinstance (self .context_state , ContextFailed ):
783+ error_message = self .context_state .error_message
784+ if isinstance (error_message , ConfigError ) and error_message .location is not None :
785+ error_uri = URI .from_path (error_message .location )
786+ if error_uri .value == uri .value :
787+ # Return the configuration error as a diagnostic
788+ return [
789+ types .Diagnostic (
790+ range = types .Range (
791+ start = types .Position (line = 0 , character = 0 ),
792+ end = types .Position (line = 0 , character = 0 ),
793+ ),
794+ message = str (error_message ),
795+ severity = types .DiagnosticSeverity .Error ,
796+ source = "SQLMesh" ,
797+ )
798+ ], 0
773799 return [], 0
774800
775801 def _context_get_or_load (
@@ -869,16 +895,17 @@ def _create_lsp_context(
869895 except Exception as e :
870896 # Only show the error message once
871897 if not self .has_raised_loading_error :
898+ location_info = f" at { e .location } " if isinstance (e , ConfigError ) and e .location else ""
872899 self .server .show_message (
873- f"Error creating context error type { type (e )} : { e . location } { e } " ,
900+ f"Error creating context error type { type (e )} : { e } { location_info } " ,
874901 types .MessageType .Error ,
875902 )
876903 self .has_raised_loading_error = True
877904 error_message = e if isinstance (e , ConfigError ) else str (e )
878- if isinstance (e , ConfigError ) and error_message .location is not None :
879- self . server . show_message ( "hello" , types . MessageType . Error )
880- uri = URI . from_path ( error_message . location )
881- self . server .publish_diagnostics (
905+ if isinstance (e , ConfigError ) and e .location is not None :
906+ uri = URI . from_path ( e . location )
907+ ls . show_message ( f"Publishing diagnostic to URI: { uri . value } " , types . MessageType . Info )
908+ ls .publish_diagnostics (
882909 uri .value ,
883910 [
884911 types .Diagnostic (
@@ -890,7 +917,6 @@ def _create_lsp_context(
890917 severity = types .DiagnosticSeverity .Error ,
891918 )
892919 ],
893- str (1 ),
894920 )
895921
896922 # Store the error in context state such that later requests can
0 commit comments