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.

send absolute path to frontend for file_browser (#8668)

Shahmir Varqhaaf05ba0c86da
diff --git a/marimo/_plugins/ui/_impl/file_browser.py b/marimo/_plugins/ui/_impl/file_browser.py
index fc3d243cc0..e99c7bab92 100644
--- a/marimo/_plugins/ui/_impl/file_browser.py
+++ b/marimo/_plugins/ui/_impl/file_browser.py
@@ -211,7 +211,7 @@ class file_browser(
initial_value=[],
label=label,
args={
- "initial-path": str(initial_path),
+ "initial-path": str(self._initial_path),
"selection-mode": selection_mode,
"filetypes": filetypes if filetypes is not None else [],
"multiple": multiple,
@@ -300,8 +300,7 @@ class file_browser(
if self._restrict_navigation and path in self._initial_path.parents:
raise RuntimeError(
- "Navigation is restricted; navigating to a "
- "parent of initial path is not allowed."
+ "Navigation is restricted; navigating to a parent of initial path is not allowed."
)
folders: list[TypedFileBrowserFileInfo] = []
files: list[TypedFileBrowserFileInfo] = []
diff --git a/tests/_plugins/ui/_impl/test_file_browser.py b/tests/_plugins/ui/_impl/test_file_browser.py
index 85af0518b6..23a1e8976e 100644
--- a/tests/_plugins/ui/_impl/test_file_browser.py
+++ b/tests/_plugins/ui/_impl/test_file_browser.py
@@ -951,3 +951,33 @@ def test_file_browser_relative_path_normalization(tmp_path: Path) -> None:
finally:
# Restore original directory
os.chdir(original_cwd)
+
+
+def test_file_browser_relative_path_sent_to_frontend_as_absolute(
+ tmp_path: Path,
+) -> None:
+ """Test that the initial-path arg sent to the frontend is always absolute."""
+ sub_dir = tmp_path / "subdir"
+ sub_dir.mkdir()
+ (sub_dir / "file.txt").touch()
+
+ original_cwd = Path.cwd()
+ try:
+ os.chdir(tmp_path)
+
+ for rel_path in ["subdir", "./subdir", Path("subdir")]:
+ fb = file_browser(initial_path=rel_path)
+ initial_path_arg = str(fb._component_args["initial-path"]) # pyright: ignore[reportPrivateUsage]
+ assert "/" in initial_path_arg, (
+ f"initial-path sent to frontend must contain '/' "
+ f"(be absolute), got: {initial_path_arg!r}"
+ )
+ assert Path(initial_path_arg).is_absolute()
+
+ # Navigating up from the initial path should work
+ parent = str(Path(initial_path_arg).parent)
+ response = fb._list_directory(ListDirectoryArgs(path=parent)) # pyright: ignore[reportPrivateUsage]
+ assert isinstance(response, ListDirectoryResponse)
+
+ finally:
+ os.chdir(original_cwd)