From 96a89f3a36896cb183be306a9c0b817a03e3d8e9 Mon Sep 17 00:00:00 2001 From: Saga4 Date: Wed, 2 Jul 2025 03:03:59 +0530 Subject: [PATCH 1/3] Create dedicated server entry point to prevent crashes --- codeflash/lsp/server.py | 23 ++++++++++++++++++- codeflash/lsp/server_entry.py | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 codeflash/lsp/server_entry.py diff --git a/codeflash/lsp/server.py b/codeflash/lsp/server.py index dff1085ac..1a303c13c 100644 --- a/codeflash/lsp/server.py +++ b/codeflash/lsp/server.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import TYPE_CHECKING, Any -from lsprotocol.types import INITIALIZE +from lsprotocol.types import INITIALIZE, MessageType, LogMessageParams from pygls import uris from pygls.protocol import LanguageServerProtocol, lsp_method from pygls.server import LanguageServer @@ -55,3 +55,24 @@ def initialize_optimizer(self, config_file: Path) -> None: args.no_pr = True # LSP server should not create PRs args = process_pyproject_config(args) self.optimizer = Optimizer(args) + + def show_message_log(self, message: str, message_type: str) -> None: + """Send a log message to the client's output channel. + + Args: + message: The message to log + message_type: String type - "Info", "Warning", "Error", or "Log" + """ + # Convert string message type to LSP MessageType enum + type_mapping = { + "Info": MessageType.Info, + "Warning": MessageType.Warning, + "Error": MessageType.Error, + "Log": MessageType.Log + } + + lsp_message_type = type_mapping.get(message_type, MessageType.Info) + + # Send log message to client (appears in output channel) + log_params = LogMessageParams(type=lsp_message_type, message=message) + self.lsp.notify("window/logMessage", log_params) diff --git a/codeflash/lsp/server_entry.py b/codeflash/lsp/server_entry.py new file mode 100644 index 000000000..89c22bf15 --- /dev/null +++ b/codeflash/lsp/server_entry.py @@ -0,0 +1,42 @@ +""" +This script is the dedicated entry point for the Codeflash Language Server. +It initializes the server and redirects its logs to stderr so that the +VS Code client can display them in the output channel. + +This script is run by the VS Code extension and is not intended to be +executed directly by users. +""" +import logging +import sys +import os +from codeflash.lsp.beta import server + +# Configure logging to stderr for VS Code output channel +def setup_logging(): + # Clear any existing handlers to prevent conflicts + root_logger = logging.getLogger() + root_logger.handlers.clear() + + # Set up stderr handler for VS Code output channel with [LSP-Server] prefix + handler = logging.StreamHandler(sys.stderr) + handler.setFormatter(logging.Formatter("[LSP-Server] %(asctime)s [%(levelname)s]: %(message)s")) + + # Configure root logger + root_logger.addHandler(handler) + root_logger.setLevel(logging.INFO) + + # Also configure the pygls logger specifically + pygls_logger = logging.getLogger('pygls') + pygls_logger.setLevel(logging.INFO) + + return root_logger + +# Set up logging +log = setup_logging() + +# Silence the console module to prevent stdout pollution +from codeflash.cli_cmds.console import console +console.quiet = True + +log.info("Starting Codeflash Language Server...") +server.start_io() From b920b92b1b3716300349932b7118504c65c80548 Mon Sep 17 00:00:00 2001 From: Saga4 Date: Wed, 2 Jul 2025 03:46:26 +0530 Subject: [PATCH 2/3] wrap in main block --- codeflash/lsp/server_entry.py | 36 ++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/codeflash/lsp/server_entry.py b/codeflash/lsp/server_entry.py index 89c22bf15..841d18f84 100644 --- a/codeflash/lsp/server_entry.py +++ b/codeflash/lsp/server_entry.py @@ -1,42 +1,48 @@ -""" -This script is the dedicated entry point for the Codeflash Language Server. +"""This script is the dedicated entry point for the Codeflash Language Server. It initializes the server and redirects its logs to stderr so that the VS Code client can display them in the output channel. This script is run by the VS Code extension and is not intended to be executed directly by users. """ + import logging import sys -import os + from codeflash.lsp.beta import server + # Configure logging to stderr for VS Code output channel def setup_logging(): # Clear any existing handlers to prevent conflicts root_logger = logging.getLogger() root_logger.handlers.clear() - + # Set up stderr handler for VS Code output channel with [LSP-Server] prefix handler = logging.StreamHandler(sys.stderr) handler.setFormatter(logging.Formatter("[LSP-Server] %(asctime)s [%(levelname)s]: %(message)s")) - + # Configure root logger root_logger.addHandler(handler) root_logger.setLevel(logging.INFO) - + # Also configure the pygls logger specifically - pygls_logger = logging.getLogger('pygls') + pygls_logger = logging.getLogger("pygls") pygls_logger.setLevel(logging.INFO) - + return root_logger -# Set up logging -log = setup_logging() -# Silence the console module to prevent stdout pollution -from codeflash.cli_cmds.console import console -console.quiet = True +if __name__ == "__main__": + # Set up logging + log = setup_logging() + log.info("Starting Codeflash Language Server...") + + # Silence the console module to prevent stdout pollution + from codeflash.cli_cmds.console import console + + console.quiet = True + # console.enable() -log.info("Starting Codeflash Language Server...") -server.start_io() + # Start the language server + server.start_io() From 77e24f93c292ddc6fbb2b2c0391429c2593b7f95 Mon Sep 17 00:00:00 2001 From: Saga4 Date: Wed, 2 Jul 2025 03:49:05 +0530 Subject: [PATCH 3/3] clean up beta.py --- codeflash/lsp/beta.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/codeflash/lsp/beta.py b/codeflash/lsp/beta.py index 63f811cf9..21c10fecc 100644 --- a/codeflash/lsp/beta.py +++ b/codeflash/lsp/beta.py @@ -292,10 +292,3 @@ def perform_function_optimization( # noqa: PLR0911 "extra": f"Speedup: {speedup:.2f}x faster", "optimization": optimized_source, } - - -if __name__ == "__main__": - from codeflash.cli_cmds.console import console - - console.quiet = True - server.start_io()