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(file_browser): reject an empty-string value (#10613)

Kiran Gadhave4c33fb904b5a
diff --git a/marimo/_plugins/ui/_impl/file_browser.py b/marimo/_plugins/ui/_impl/file_browser.py
index b350cdd587..5a61d101f9 100644
--- a/marimo/_plugins/ui/_impl/file_browser.py
+++ b/marimo/_plugins/ui/_impl/file_browser.py
@@ -66,6 +66,19 @@ def _normalize_selection_mode(
)
+def _reject_empty_value(value: str | Path, key: str) -> None:
+ """Reject an empty string before it reaches path normalization.
+
+ `Path("")` is `Path(".")`, which normalization resolves to the current
+ working directory. That hides the real input from every later error.
+ """
+ if isinstance(value, str) and not value:
+ raise ValueError(
+ f'Invalid {key}="". Pass a file or directory path, '
+ f"or None for no default value."
+ )
+
+
def _normalize_values(
value: str | Path | Sequence[str | Path] | None,
*,
@@ -75,8 +88,11 @@ def _normalize_values(
if value is None:
values: Sequence[str | Path] = ()
elif isinstance(value, Sequence) and not isinstance(value, str):
+ for index, entry in enumerate(value):
+ _reject_empty_value(value=entry, key=f"value[{index}]")
values = value
else:
+ _reject_empty_value(value, "value")
values = (value,)
if not multiple and len(values) > 1:
@@ -268,6 +284,8 @@ class file_browser(
to browse any path readable by the server process.
value (str | Path | Sequence[str | Path], optional): File or directory
path, or sequence of paths, selected by default. Defaults to None.
+ An empty string is not a valid path. Pass None for no default
+ value.
ignore_empty_dirs (bool, optional): If True, hide directories that contain
no files (recursively). Directories are scanned up to 100 levels deep
to prevent stack overflow from deeply nested structures. Directory
diff --git a/tests/_plugins/ui/_impl/test_file_browser.py b/tests/_plugins/ui/_impl/test_file_browser.py
index 8663162436..331dc9d0d1 100644
--- a/tests/_plugins/ui/_impl/test_file_browser.py
+++ b/tests/_plugins/ui/_impl/test_file_browser.py
@@ -42,6 +42,21 @@ def test_normalize_values_rejects_multiple_values() -> None:
_normalize_values(["first.txt", "second.txt"], multiple=False)
+def test_normalize_values_rejects_empty_string() -> None:
+ with pytest.raises(ValueError, match='Invalid value=""'):
+ _normalize_values("", multiple=True)
+
+
+def test_normalize_values_rejects_empty_string_in_sequence() -> None:
+ with pytest.raises(ValueError, match=r'Invalid value\[1\]=""'):
+ _normalize_values(["first.txt", ""], multiple=True)
+
+
+def test_normalize_values_reports_index_for_single_element_sequence() -> None:
+ with pytest.raises(ValueError, match=r'Invalid value\[0\]=""'):
+ _normalize_values([""], multiple=True)
+
+
def test_is_path_within_cases(tmp_path: Path) -> None:
root = tmp_path.resolve()
jail = root / "jail"
@@ -458,6 +473,42 @@ def test_file_browser_rejects_multiple_default_values(
)
+def test_file_browser_rejects_empty_string_value(tmp_path: Path) -> None:
+ with pytest.raises(ValueError, match='Invalid value=""') as error:
+ file_browser(initial_path=tmp_path, value="")
+
+ assert str(tmp_path) not in str(error.value)
+
+
+def test_file_browser_rejects_empty_string_value_for_any_selection_mode(
+ tmp_path: Path,
+) -> None:
+ with pytest.raises(ValueError, match='Invalid value=""'):
+ file_browser(initial_path=tmp_path, value="", selection_mode="all")
+
+
+def test_file_browser_rejects_empty_string_among_valid_values(
+ tmp_path: Path,
+) -> None:
+ selected = tmp_path / "selected.txt"
+ selected.touch()
+
+ with pytest.raises(ValueError, match=r'Invalid value\[1\]=""'):
+ file_browser(initial_path=tmp_path, value=[selected, ""])
+
+
+def test_file_browser_accepts_relative_string_value(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ selected = tmp_path / "selected.txt"
+ selected.touch()
+ monkeypatch.chdir(tmp_path)
+
+ fb = file_browser(value="selected.txt")
+
+ assert fb.path() == normalize_path(Path("selected.txt"))
+
+
def test_file_browser_rejects_default_value_of_wrong_kind(
tmp_path: Path,
) -> None: