Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: make Show in folder work on Linux, and report when it can't (#16198)

Debojyoti Banerjeef07747326ad1
diff --git a/studio/backend/tests/test_library_routes.py b/studio/backend/tests/test_library_routes.py
index 940fe3f1212..b4410dc0c79 100644
--- a/studio/backend/tests/test_library_routes.py
+++ b/studio/backend/tests/test_library_routes.py
@@ -2497,6 +2497,110 @@ def test_explorer_gets_the_documented_select_command(tmp_path, monkeypatch):
assert calls == [["explorer.exe", "/select,", windows_path]]
+class _Launcher:
+ def __init__(self, status):
+ self.status = status
+
+ def wait(self, timeout = None):
+ if self.status is None:
+ import subprocess
+ raise subprocess.TimeoutExpired("xdg-open", timeout)
+ return self.status
+
+
+def _linux_desktop(
+ monkeypatch,
+ tmp_path,
+ answers,
+ xdg_status = 0,
+):
+ """D-Bus clients exit with their code in *answers* (absent: not installed), xdg-open with *xdg_status* (None: still running)."""
+ import subprocess
+ from types import SimpleNamespace
+
+ import utils.paths.path_utils as path_utils
+
+ target = tmp_path / "it's, here" / "cat.png"
+ target.parent.mkdir(parents = True)
+ target.write_bytes(b"png")
+ commands = []
+
+ def run(command, **_kwargs):
+ commands.append(command)
+ code = answers.get(command[0])
+ if code is None:
+ raise FileNotFoundError(2, "No such file or directory", command[0])
+ return SimpleNamespace(returncode = code)
+
+ def popen(command, **_kwargs):
+ commands.append(command)
+ return _Launcher(xdg_status)
+
+ monkeypatch.setattr(path_utils.sys, "platform", "linux")
+ monkeypatch.setattr(path_utils, "os", _os_named("posix"))
+ monkeypatch.setattr(path_utils, "_IS_WSL", False)
+ monkeypatch.setattr(subprocess, "run", run)
+ monkeypatch.setattr(subprocess, "Popen", popen)
+ return target, commands
+
+
+def test_linux_reveal_asks_the_file_manager_to_select_the_file(tmp_path, monkeypatch):
+ import utils.paths.path_utils as path_utils
+
+ target, commands = _linux_desktop(monkeypatch, tmp_path, {"gdbus": 0})
+ path_utils.reveal_in_file_manager(target)
+ [command] = commands
+ assert command[:2] == ["gdbus", "call"]
+ assert "org.freedesktop.FileManager1.ShowItems" in command
+ # The quote and comma in the folder name are percent-encoded, so the array literal holds.
+ assert f"['{target.as_uri()}']" in command
+ assert "'" not in target.as_uri() and "," not in target.as_uri()
+
+ commands.clear()
+ path_utils.reveal_in_file_manager(target.parent)
+ assert "org.freedesktop.FileManager1.ShowFolders" in commands[0]
+ assert f"['{target.parent.as_uri()}']" in commands[0]
+
+
+def test_linux_reveal_tries_dbus_send_then_xdg_open_on_the_folder(tmp_path, monkeypatch):
+ import utils.paths.path_utils as path_utils
+
+ target, commands = _linux_desktop(monkeypatch, tmp_path, {"dbus-send": 0})
+ path_utils.reveal_in_file_manager(target)
+ assert [command[0] for command in commands] == ["gdbus", "dbus-send"]
+ assert f"array:string:{target.as_uri()}" in commands[1]
+
+ target, commands = _linux_desktop(monkeypatch, tmp_path / "b", {"gdbus": 1, "dbus-send": 1})
+ path_utils.reveal_in_file_manager(target)
+ assert commands[-1] == ["xdg-open", str(target.parent)]
+
+
+def test_linux_reveal_reports_an_xdg_open_that_fails(tmp_path, monkeypatch):
+ """No FileManager1 and no handler for folders used to answer 200 with nothing on screen."""
+ import utils.paths.path_utils as path_utils
+
+ target, commands = _linux_desktop(monkeypatch, tmp_path, {}, xdg_status = 3)
+ with pytest.raises(RuntimeError, match = "status 3"):
+ path_utils.reveal_in_file_manager(target)
+
+ # Still running after the wait: a fallback that holds the window open, not a failure.
+ target, commands = _linux_desktop(monkeypatch, tmp_path / "b", {}, xdg_status = None)
+ path_utils.reveal_in_file_manager(target)
+ assert commands[-1] == ["xdg-open", str(target.parent)]
+
+
+def test_a_failed_linux_reveal_answers_with_an_error(client, monkeypatch, tmp_path):
+ import utils.paths.file_manager as file_manager
+ import utils.paths.path_utils as path_utils
+
+ [note] = _upload(client, ("plan.md", b"# plan", "text/markdown"))
+ monkeypatch.setattr(file_manager, "file_manager_kind", lambda: "files")
+ _linux_desktop(monkeypatch, tmp_path, {}, xdg_status = 4)
+ response = _post(client, "items/reveal", id = note)
+ assert response.status_code == 500
+ assert response.json()["detail"] == "Failed to open file manager"
+
+
@pytest.mark.parametrize(
"path, root, inside",
[
diff --git a/studio/backend/utils/paths/path_utils.py b/studio/backend/utils/paths/path_utils.py
index e2c949ecc93..25dfdede744 100644
--- a/studio/backend/utils/paths/path_utils.py
+++ b/studio/backend/utils/paths/path_utils.py
@@ -371,6 +371,58 @@ def _wsl_reveal_in_explorer(path: Path, is_file: bool) -> bool:
return False
+_FILE_MANAGER1 = "org.freedesktop.FileManager1"
+
+
+def _linux_show_items(path: Path, is_file: bool) -> bool:
+ """FileManager1 over D-Bus (Nautilus, Dolphin, Nemo, Caja, Thunar) shows *path*, a file
+ selected in its folder; False when nothing answers."""
+ import subprocess
+
+ # as_uri percent-encodes quotes and commas, so the URI is safe in both argument syntaxes.
+ uri = Path(os.path.abspath(path)).as_uri()
+ method = f"{_FILE_MANAGER1}.{'ShowItems' if is_file else 'ShowFolders'}"
+ object_path = "/org/freedesktop/FileManager1"
+ commands = (
+ ["gdbus", "call", "--session", "--dest", _FILE_MANAGER1, "--object-path", object_path]
+ + ["--method", method, f"['{uri}']", "''"],
+ ["dbus-send", "--session", "--print-reply", f"--dest={_FILE_MANAGER1}", object_path]
+ + [method, f"array:string:{uri}", "string:"],
+ )
+ for command in commands:
+ try:
+ result = subprocess.run(
+ command,
+ stdin = subprocess.DEVNULL,
+ stdout = subprocess.DEVNULL,
+ stderr = subprocess.DEVNULL,
+ timeout = 10,
+ )
+ except (OSError, subprocess.SubprocessError):
+ continue
+ if result.returncode == 0:
+ return True
+ return False
+
+
+def _linux_xdg_open(folder: str) -> None:
+ """Raises when xdg-open exits non-zero (no handler for folders) instead of doing nothing."""
+ import subprocess
+
+ launcher = subprocess.Popen(
+ ["xdg-open", folder],
+ stdin = subprocess.DEVNULL,
+ stdout = subprocess.DEVNULL,
+ stderr = subprocess.DEVNULL,
+ )
+ try:
+ status = launcher.wait(timeout = 5)
+ except subprocess.TimeoutExpired:
+ return # Generic fallbacks run the file manager in the foreground.
+ if status != 0:
+ raise RuntimeError(f"xdg-open exited with status {status}")
+
+
def reveal_in_file_manager(path: Path, expect_dir: bool = False) -> None:
"""Raises FileNotFoundError when gone, rather than revealing the parent folder; expect_dir refuses symlinks and non-dirs, and is off by default."""
import stat as stat_module
@@ -404,9 +456,9 @@ def reveal_in_file_manager(path: Path, expect_dir: bool = False) -> None:
os.startfile(str(path.parent)) # noqa: S606 - local user's own file manager
else:
os.startfile(target) # noqa: S606 - local user's own file manager
- elif not _wsl_reveal_in_explorer(path, is_file):
- # No cross-desktop "select file" standard on Linux; open the directory.
- subprocess.Popen(["xdg-open", str(path.parent) if is_file else target])
+ elif not _wsl_reveal_in_explorer(path, is_file) and not _linux_show_items(path, is_file):
+ # No FileManager1 on this desktop: open the folder without selecting the file.
+ _linux_xdg_open(str(path.parent) if is_file else target)
# Viewer-opened types only: scripts or installers would execute, and these files are model-written.
diff --git a/tests/studio/test_reveal_file_manager.py b/tests/studio/test_reveal_file_manager.py
index bebf7b2ff81..0a327f3b7bf 100644
--- a/tests/studio/test_reveal_file_manager.py
+++ b/tests/studio/test_reveal_file_manager.py
@@ -64,11 +64,11 @@ def spawned(monkeypatch):
calls.run.append(list(cmd))
if calls.run_error is not None:
raise calls.run_error
- return types.SimpleNamespace(stdout = _WINDOWS_PATH + "\n")
+ return types.SimpleNamespace(stdout = _WINDOWS_PATH + "\n", returncode = 1)
def fake_popen(cmd, **kwargs):
calls.popen.append(list(cmd))
- return types.SimpleNamespace()
+ return types.SimpleNamespace(wait = lambda timeout = None: 0)
monkeypatch.setattr(subprocess, "run", fake_run)
monkeypatch.setattr(subprocess, "Popen", fake_popen)
@@ -103,7 +103,7 @@ def test_wsl_empty_conversion_falls_back_to_xdg_open(linux_host, spawned, monkey
monkeypatch.setattr(path_utils, "_IS_WSL", True)
def empty_run(cmd, **kwargs):
- return types.SimpleNamespace(stdout = "\n")
+ return types.SimpleNamespace(stdout = "\n", returncode = 1)
monkeypatch.setattr(subprocess, "run", empty_run)
path_utils.reveal_in_file_manager(tmp_path)
@@ -117,5 +117,5 @@ def test_native_linux_keeps_xdg_open_on_parent_directory(
target = tmp_path / "model.gguf"
target.write_bytes(b"gguf")
path_utils.reveal_in_file_manager(target)
- assert spawned.run == []
+ assert [cmd[0] for cmd in spawned.run] == ["gdbus", "dbus-send"]
assert spawned.popen == [["xdg-open", str(tmp_path)]]
diff --git a/tests/studio/test_reveal_file_manager_matrix.py b/tests/studio/test_reveal_file_manager_matrix.py
index db16a907f88..558abd69e36 100644
--- a/tests/studio/test_reveal_file_manager_matrix.py
+++ b/tests/studio/test_reveal_file_manager_matrix.py
@@ -59,13 +59,14 @@ def spawned(monkeypatch):
def fake_run(cmd, **kwargs):
calls.run.append(list(cmd))
- return types.SimpleNamespace(stdout = "C:\\converted\n")
+ # Non-zero: no FileManager1 on the bus, so native Linux falls back to xdg-open.
+ return types.SimpleNamespace(stdout = "C:\\converted\n", returncode = 1)
def fake_popen(cmd, **kwargs):
if calls.popen_error is not None:
raise calls.popen_error
calls.popen.append(cmd if isinstance(cmd, str) else list(cmd))
- return types.SimpleNamespace()
+ return types.SimpleNamespace(wait = lambda timeout = None: 0)
monkeypatch.setattr(subprocess, "run", fake_run)
monkeypatch.setattr(subprocess, "Popen", fake_popen)
@@ -178,7 +179,7 @@ def test_wsl_falls_back_to_xdg_open_when_wslpath_fails(monkeypatch, spawned, tmp
def test_native_linux_opens_a_directory_directly(native_linux, spawned, tmp_path):
path_utils.reveal_in_file_manager(tmp_path)
- assert spawned.run == []
+ assert [cmd[0] for cmd in spawned.run] == ["gdbus", "dbus-send"]
assert spawned.popen == [["xdg-open", str(tmp_path)]]