Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: disable LSP servers in run-mode (#4514)

Myles Scolnick219d01afb49c
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index 9d0d495ba5..411edb54bd 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -229,21 +229,18 @@ class CompositeLspServer(LspServer):
self.config_reader = config_reader
self.min_port = min_port
+ last_free_port = find_free_port(min_port)
+
# NOTE: we construct all servers up front regardless of whether they are enabled
# in order to properly mount them as Starlette routes with their own ports
# With 2 servers, this is OK, but if we want to support more, we should
# lazily construct servers, routes, and ports.
# We still lazily start servers as they are enabled.
# We also need to ensure that the ports are unique
- self.servers: dict[str, LspServer] = {
- # We offset the ports by 2 to ensure they are unique
- server_name: server_constructor(
- find_free_port(self.min_port + i * 5)
- )
- for i, (server_name, server_constructor) in enumerate(
- self.LANGUAGE_SERVERS.items()
- )
- }
+ self.servers: dict[str, LspServer] = {}
+ for server_name, server_constructor in self.LANGUAGE_SERVERS.items():
+ last_free_port = find_free_port(last_free_port + 1)
+ self.servers[server_name] = server_constructor(last_free_port)
def _is_enabled(self, server_name: str) -> bool:
# .get_config() is not cached
diff --git a/marimo/_server/main.py b/marimo/_server/main.py
index 7fcfa5365a..9b2f86c045 100644
--- a/marimo/_server/main.py
+++ b/marimo/_server/main.py
@@ -2,7 +2,7 @@
from __future__ import annotations
from dataclasses import dataclass
-from typing import TYPE_CHECKING, Optional
+from typing import TYPE_CHECKING, Callable, Optional
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
@@ -134,16 +134,19 @@ def _create_lsps_proxy_middleware(
) -> list[Middleware]:
middlewares: list[Middleware] = []
for server in servers:
- to_replace = "/lsp" if server.id == "copilot" else f"/lsp/{server.id}"
+
+ def path_rewrite(server_id: str) -> Callable[[str], str]:
+ to_replace = (
+ "/copilot" if server_id == "copilot" else f"/lsp/{server_id}"
+ )
+ return lambda _: to_replace
+
middlewares.append(
Middleware(
ProxyMiddleware,
proxy_path=f"/lsp/{server.id}",
target_url=f"http://localhost:{server.port}",
- path_rewrite=lambda path: path.replace(
- to_replace, # noqa: B023
- "",
- ),
+ path_rewrite=path_rewrite(server.id),
)
)
return middlewares
diff --git a/marimo/_server/start.py b/marimo/_server/start.py
index 04d026842f..0d9b454d00 100644
--- a/marimo/_server/start.py
+++ b/marimo/_server/start.py
@@ -12,7 +12,7 @@ from marimo._config.manager import get_default_config_manager
from marimo._config.settings import GLOBAL_SETTINGS
from marimo._runtime.requests import SerializedCLIArgs
from marimo._server.file_router import AppFileRouter
-from marimo._server.lsp import CompositeLspServer
+from marimo._server.lsp import CompositeLspServer, NoopLspServer
from marimo._server.main import create_starlette_app
from marimo._server.model import SessionMode
from marimo._server.sessions import SessionManager
@@ -106,10 +106,12 @@ def start(
config_reader = get_default_config_manager(current_path=start_path)
- lsp_composite_server = CompositeLspServer(
- config_reader=config_reader,
- min_port=DEFAULT_PORT + 400,
- )
+ lsp_composite_server: Optional[CompositeLspServer] = None
+ if mode == SessionMode.EDIT:
+ lsp_composite_server = CompositeLspServer(
+ config_reader=config_reader,
+ min_port=DEFAULT_PORT + 400,
+ )
# If watch is true, disable auto-save and format-on-save,
# watch is enabled when they are editing in another editor
@@ -144,7 +146,7 @@ def start(
quiet=quiet,
include_code=include_code,
ttl_seconds=ttl_seconds,
- lsp_server=lsp_composite_server,
+ lsp_server=lsp_composite_server or NoopLspServer(),
config_manager=config_reader,
cli_args=cli_args,
argv=argv,
@@ -170,7 +172,9 @@ def start(
),
allow_origins=allow_origins,
enable_auth=not AuthToken.is_empty(session_manager.auth_token),
- lsp_servers=list(lsp_composite_server.servers.values()),
+ lsp_servers=list(lsp_composite_server.servers.values())
+ if lsp_composite_server is not None
+ else None,
)
app.state.port = external_port