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: detect ty language server on PATH (#11321)

Myles Scolnick41234934e244
diff --git a/marimo/_messaging/notification.py b/marimo/_messaging/notification.py
index 665fd76ea4..5f31cb422a 100644
--- a/marimo/_messaging/notification.py
+++ b/marimo/_messaging/notification.py
@@ -383,7 +383,7 @@ class KernelCapabilitiesNotification(msgspec.Struct):
self.terminal = not is_windows() and not is_pyodide()
self.pylsp = DependencyManager.pylsp.has()
self.basedpyright = DependencyManager.basedpyright.has()
- self.ty = DependencyManager.ty.has()
+ self.ty = DependencyManager.ty.has() or DependencyManager.which("ty")
self.pyrefly = DependencyManager.pyrefly.has()
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index a9dd3e9ea9..c9fb0e8e20 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio
import os
import secrets
+import shutil
import signal
import socket
import subprocess
@@ -696,13 +697,13 @@ class TyServer(BaseLspServer):
async def start(self) -> AlertNotification | None:
# ty is not required, so we don't want to alert or fail if it is not installed
- if not DependencyManager.ty.has():
+ if not (DependencyManager.ty.has() or DependencyManager.which("ty")):
LOGGER.debug("ty is not installed. Skipping LSP server.")
return None
return await super().start()
def validate_requirements(self) -> str | Literal[True]:
- if not DependencyManager.ty.has():
+ if not (DependencyManager.ty.has() or DependencyManager.which("ty")):
return "ty is missing. Install it with `pip install ty`."
if not DependencyManager.which("node"):
@@ -711,12 +712,21 @@ class TyServer(BaseLspServer):
return True
def get_command(self) -> list[str]:
- from ty.__main__ import find_ty_bin # type: ignore
-
lsp_bin = marimo_package_path() / "_lsp" / "index.cjs"
# Use typed format to avoid quoting issues: ty:<binary_path>
- ty_command = f"ty:{find_ty_bin()}"
+ try:
+ from ty.__main__ import find_ty_bin # type: ignore
+
+ ty_command = f"ty:{find_ty_bin()}"
+ except ImportError:
+ # Tools installed with `uv tool install` are only on PATH.
+ ty_bin = shutil.which("ty")
+ if ty_bin is None:
+ raise FileNotFoundError(
+ "ty executable not found on PATH"
+ ) from None
+ ty_command = f"ty:{ty_bin}"
return [
"node",
diff --git a/tests/_messaging/test_notifications.py b/tests/_messaging/test_notifications.py
index 1f1780e982..01e6f0b301 100644
--- a/tests/_messaging/test_notifications.py
+++ b/tests/_messaging/test_notifications.py
@@ -1,7 +1,7 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
-from unittest.mock import MagicMock
+from unittest.mock import MagicMock, patch
import msgspec
import pytest
@@ -11,6 +11,7 @@ from marimo._messaging.notification import (
CellNotification,
EnvironmentOperationNotification,
EnvironmentOperationStatus,
+ KernelCapabilitiesNotification,
ModelLifecycleNotification,
ModelOpen,
OperationCancelled,
@@ -38,6 +39,19 @@ from marimo._utils.parse_dataclass import parse_raw
from tests._messaging.mocks import MockStream
[email protected]("installed", [False, True])
[email protected]("on_path", [False, True])
+def test_ty_capability(installed: bool, on_path: bool) -> None:
+ with (
+ patch(
+ "marimo._messaging.notification.DependencyManager.ty.has",
+ return_value=installed,
+ ),
+ patch("shutil.which", return_value="/bin/ty" if on_path else None),
+ ):
+ assert KernelCapabilitiesNotification().ty is (installed or on_path)
+
+
def test_value_ui_element() -> None:
variable_value = create_variable_value(
name="s", value=slider(1, 10, value=5)
diff --git a/tests/_server/test_lsp.py b/tests/_server/test_lsp.py
index 1344ceb6c9..a10cb07d16 100644
--- a/tests/_server/test_lsp.py
+++ b/tests/_server/test_lsp.py
@@ -618,8 +618,12 @@ def test_ty_server_uses_typed_format():
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}
+ with (
+ mock.patch.dict(
+ "sys.modules",
+ {"ty": mock_ty_module, "ty.__main__": mock_ty_module},
+ ),
+ mock.patch("shutil.which", return_value="/global/bin/ty"),
):
server = TyServer(port=8000)
command = server.get_command()
@@ -658,6 +662,89 @@ def test_ty_server_handles_spaces_in_path():
assert "/path/with spaces/ty" in lsp_command
[email protected]("installed", [False, True])
[email protected]("on_path", [False, True])
[email protected]("node_available", [False, True])
+async def test_ty_server_availability(
+ installed: bool, on_path: bool, node_available: bool
+) -> None:
+ server = TyServer(port=8000)
+ with (
+ mock.patch(
+ "marimo._server.lsp.DependencyManager.ty.has",
+ return_value=installed,
+ ),
+ mock.patch(
+ "shutil.which",
+ side_effect={
+ "ty": "/global/bin/ty" if on_path else None,
+ "node": "/bin/node" if node_available else None,
+ }.get,
+ ),
+ mock.patch.object(
+ BaseLspServer, "start", new_callable=mock.AsyncMock
+ ) as start,
+ ):
+ validation = server.validate_requirements()
+ if not (installed or on_path):
+ assert (
+ validation
+ == "ty is missing. Install it with `pip install ty`."
+ )
+ elif not node_available:
+ assert (
+ validation
+ == "node.js binary is missing. Install node at https://nodejs.org/."
+ )
+ else:
+ assert validation is True
+
+ await server.start()
+ assert start.await_count == int(installed or on_path)
+
+
+def test_ty_server_missing_binary() -> None:
+ server = TyServer(port=8000)
+ with (
+ mock.patch.dict("sys.modules", {"ty": None, "ty.__main__": None}),
+ mock.patch("shutil.which", return_value=None),
+ pytest.raises(
+ FileNotFoundError, match="ty executable not found on PATH"
+ ),
+ ):
+ server.get_command()
+
+
+async def test_ty_server_starts_with_binary_on_path(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ mock_popen: mock.MagicMock,
+):
+ bin_dir = tmp_path / "global tools"
+ bin_dir.mkdir()
+ binary = bin_dir / ("ty.exe" if is_windows() else "ty")
+ binary.touch(mode=0o755)
+ monkeypatch.setenv("PATH", str(bin_dir))
+ server = TyServer(port=8000)
+
+ with (
+ mock.patch.dict("sys.modules", {"ty": None, "ty.__main__": None}),
+ mock.patch(
+ "marimo._server.lsp.DependencyManager.which",
+ return_value=True,
+ ),
+ mock.patch.object(server, "_wait_until_ready", return_value=True),
+ mock.patch.object(server, "_signal_process_tree"),
+ ):
+ try:
+ assert await server.start() is None
+ mock_popen.assert_called_once()
+ command = mock_popen.call_args.args[0]
+ assert command[command.index("--lsp") + 1] == f"ty:{binary}"
+ finally:
+ server.stop()
+
+
def test_any_lsp_server_running():
# Test any_lsp_server_running function
config: MarimoConfig = merge_default_config(