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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix[lsp]: allow binary locations with spaces (#6569)
Dylan Madisetti13a0ad210694
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.pyindex b583af3d90..07ffb67dea 100644--- a/marimo/_server/lsp.py+++ b/marimo/_server/lsp.py@@ -16,7 +16,6 @@ from marimo._messaging.ops import Alertfrom marimo._server.utils import find_free_portfrom marimo._tracer import server_tracerfrom marimo._utils.paths import marimo_package_path-from marimo._utils.strings import cmd_quoteLOGGER = _loggers.marimo_logger()@@ -200,8 +199,8 @@ class CopilotLspServer(BaseLspServer):copilot_bin = self._lsp_dir() / "copilot" / "language-server.js"log_file = _loggers.get_log_directory() / "github-copilot-lsp.log"- # Properly quote the copilot binary path to handle spaces and special characters- copilot_command = f"node {cmd_quote(str(copilot_bin))} --stdio"+ # Use typed format to avoid quoting issues: copilot:<binary_path>+ copilot_command = f"copilot:{copilot_bin}"return ["node",@@ -292,7 +291,7 @@ class BasedpyrightServer(BaseLspServer):"--port",str(self.port),"--lsp",- "basedpyright-langserver --stdio",+ "basedpyright:basedpyright-langserver","--log-file",str(log_file),]@@ -330,8 +329,8 @@ class TyServer(BaseLspServer):lsp_bin = marimo_package_path() / "_lsp" / "index.cjs"log_file = _loggers.get_log_directory() / "ty-lsp.log"- # Properly quote the ty binary path to handle spaces and special characters- ty_command = f"{cmd_quote(find_ty_bin())} server"+ # Use typed format to avoid quoting issues: ty:<binary_path>+ ty_command = f"ty:{find_ty_bin()}"return ["node",diff --git a/packages/lsp/index.test.ts b/packages/lsp/index.test.tsindex 3f2e97b0bc..b5e87eddf6 100644--- a/packages/lsp/index.test.ts+++ b/packages/lsp/index.test.ts@@ -4,7 +4,71 @@ import * as path from "node:path";import { describe, expect, it } from "vitest";-describe("LSP Server", () => {+// Import the parseTypedCommand function - we need to export it first+import { parseTypedCommand } from "./index";++describe("parseTypedCommand", () => {+ it("should parse copilot commands correctly", () => {+ const result = parseTypedCommand(+ "copilot:/path/to/copilot/language-server.js",+ );+ expect(result).toEqual([+ "node",+ "/path/to/copilot/language-server.js",+ "--stdio",+ ]);+ });++ it("should parse copilot commands with spaces in path", () => {+ const result = parseTypedCommand(+ "copilot:/path/with spaces/copilot/language-server.js",+ );+ expect(result).toEqual([+ "node",+ "/path/with spaces/copilot/language-server.js",+ "--stdio",+ ]);+ });++ it("should parse basedpyright commands correctly", () => {+ const result = parseTypedCommand("basedpyright:basedpyright-langserver");+ expect(result).toEqual(["basedpyright-langserver", "--stdio"]);+ });++ it("should parse ty commands correctly", () => {+ const result = parseTypedCommand("ty:/path/to/ty");+ expect(result).toEqual(["/path/to/ty", "server"]);+ });++ it("should parse ty commands with spaces in path", () => {+ const result = parseTypedCommand("ty:/path/with spaces/ty");+ expect(result).toEqual(["/path/with spaces/ty", "server"]);+ });++ it("should fallback to old format for commands without colon", () => {+ const result = parseTypedCommand("node /path/to/binary --stdio");+ expect(result).toEqual(["node", "/path/to/binary", "--stdio"]);+ });++ it("should throw error for unknown server types", () => {+ expect(() => parseTypedCommand("unknown:/path/to/binary")).toThrow(+ "Unknown LSP server type: unknown",+ );+ });++ it("should handle edge cases with multiple colons", () => {+ const result = parseTypedCommand(+ "copilot:C:/Program Files/Node.js/language-server.js",+ );+ expect(result).toEqual([+ "node",+ "C:/Program Files/Node.js/language-server.js",+ "--stdio",+ ]);+ });+});++describe("LSP Server Integration", () => {it("should start and be killable", async () => {const process = childProcess.spawn("node", ["./dist/index.cjs",@@ -27,4 +91,27 @@ describe("LSP Server", () => {expect(process.killed).toBe(true);});++ it("should start with typed copilot command", async () => {+ const process = childProcess.spawn("node", [+ "./dist/index.cjs",+ "--lsp",+ "copilot:echo copilot-test",+ "--log-file",+ path.join(os.tmpdir(), "lsp-server-copilot-test.log"),+ ]);++ // Give it a moment to start+ await new Promise((resolve) => setTimeout(resolve, 1000));++ // Kill the process+ process.kill();++ // Wait for process to die+ await new Promise<void>((resolve) => {+ process.on("exit", () => resolve());+ });++ expect(process.killed).toBe(true);+ });});diff --git a/packages/lsp/index.ts b/packages/lsp/index.tsindex 68ea95c67a..384c2e5a73 100644--- a/packages/lsp/index.ts+++ b/packages/lsp/index.ts@@ -106,6 +106,28 @@ class WebSocketAdapter implements IWebSocket {}}+export function parseTypedCommand(typedCommand: string): string[] {+ if (!typedCommand.includes(":")) {+ // Fallback for old format - simple split by spaces+ return typedCommand.split(" ");+ }++ const colonIndex = typedCommand.indexOf(":");+ const serverType = typedCommand.substring(0, colonIndex);+ const binaryPath = typedCommand.substring(colonIndex + 1);++ switch (serverType) {+ case "copilot":+ return ["node", binaryPath, "--stdio"];+ case "basedpyright":+ return [binaryPath, "--stdio"];+ case "ty":+ return [binaryPath, "server"];+ default:+ throw new Error(`Unknown LSP server type: ${serverType}`);+ }+}+function handleWebSocketConnection(languageServerCommand: string[],logger: Logger,@@ -184,10 +206,12 @@ async function main(): Promise<void> {return;}- const logger = await Logger.create(argv["log-file"]);+ const logFile = argv["log-file"] || "/tmp/lsp-server.log";+ const logger = await Logger.create(logFile);const serverPort = Number.parseInt(argv.port) || 3000;- const languageServerCommand = argv.lsp.split(" ");+ const languageServerCommand = parseTypedCommand(argv.lsp || "echo test");+ logger.log(`Parsed LSP command: ${languageServerCommand.join(" ")}`);startWebSocketServer(serverPort, languageServerCommand, logger);}diff --git a/tests/_server/test_lsp.py b/tests/_server/test_lsp.pyindex 0697b93dca..43696c6a12 100644--- a/tests/_server/test_lsp.py+++ b/tests/_server/test_lsp.py@@ -18,10 +18,12 @@ from marimo._config.manager import (from marimo._loggers import get_log_directoryfrom marimo._messaging.ops import Alertfrom marimo._server.lsp import (+ BasedpyrightServer,BaseLspServer,CompositeLspServer,CopilotLspServer,PyLspServer,+ TyServer,any_lsp_server_running,)@@ -132,8 +134,8 @@ def test_copilot_server():assert "GitHub Copilot" in alert.title-def test_copilot_server_command_quotes_path(tmp_path: Path) -> None:- """Test that paths with spaces are properly quoted in the copilot command."""+def test_copilot_server_uses_typed_format(tmp_path: Path) -> None:+ """Test that copilot uses the new typed format instead of shell commands."""from unittest import mockserver = CopilotLspServer(port=8000)@@ -161,14 +163,12 @@ def test_copilot_server_command_quotes_path(tmp_path: Path) -> None:lsp_arg_index = command.index("--lsp")lsp_command = command[lsp_arg_index + 1]- # The command should contain the quoted path- assert "node" in lsp_command- assert (- str(copilot_bin) in lsp_command- or f"'{copilot_bin}'" in lsp_command- or f'"{copilot_bin}"' in lsp_command- )- assert "--stdio" in lsp_command+ # The command should use the new typed format+ assert lsp_command.startswith("copilot:")+ assert str(copilot_bin) in lsp_command+ # Should NOT contain shell commands like "node" or "--stdio"+ assert "node" not in lsp_command+ assert "--stdio" not in lsp_commanddef test_copilot_server_node_version_validation():@@ -346,6 +346,70 @@ def test_composite_server():assert server._is_enabled(config, "ty") is False+def test_basedpyright_server_uses_typed_format():+ """Test that basedpyright uses the new typed format."""+ server = BasedpyrightServer(port=8000)+ command = server.get_command()++ # Find the --lsp argument+ lsp_arg_index = command.index("--lsp")+ lsp_command = command[lsp_arg_index + 1]++ # Should use typed format+ assert lsp_command == "basedpyright:basedpyright-langserver"+ assert lsp_command.startswith("basedpyright:")+ # Should NOT contain shell arguments+ assert "--stdio" not in lsp_command+++def test_ty_server_uses_typed_format():+ """Test that ty server uses the new typed format."""+ from unittest import mock++ # Mock the entire ty module and its find_ty_bin function+ mock_ty_module = mock.MagicMock()+ mock_ty_module.find_ty_bin.return_value = "/path/to/ty/binary"++ with mock.patch.dict(+ "sys.modules", {"ty": mock_ty_module, "ty.__main__": mock_ty_module}+ ):+ server = TyServer(port=8000)+ command = server.get_command()++ # Find the --lsp argument+ lsp_arg_index = command.index("--lsp")+ lsp_command = command[lsp_arg_index + 1]++ # Should use typed format+ assert lsp_command == "ty:/path/to/ty/binary"+ assert lsp_command.startswith("ty:")+ # Should NOT contain shell arguments+ assert "server" not in lsp_command+++def test_ty_server_handles_spaces_in_path():+ """Test that ty server handles paths with spaces correctly."""+ from unittest import mock++ # Mock the entire ty module and its find_ty_bin function+ mock_ty_module = mock.MagicMock()+ mock_ty_module.find_ty_bin.return_value = "/path/with spaces/ty"++ with mock.patch.dict(+ "sys.modules", {"ty": mock_ty_module, "ty.__main__": mock_ty_module}+ ):+ server = TyServer(port=8000)+ command = server.get_command()++ # Find the --lsp argument+ lsp_arg_index = command.index("--lsp")+ lsp_command = command[lsp_arg_index + 1]++ # Should use typed format and preserve the path with spaces+ assert lsp_command == "ty:/path/with spaces/ty"+ assert "/path/with spaces/ty" in lsp_command++def test_any_lsp_server_running():# Test any_lsp_server_running functionconfig: MarimoConfig = merge_default_config(