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(file_browser): report missing paths as not found (#10610)
Kiran Gadhave07edae26599c
diff --git a/marimo/_plugins/ui/_impl/file_browser.py b/marimo/_plugins/ui/_impl/file_browser.pyindex 19609c0baa..b350cdd587 100644--- a/marimo/_plugins/ui/_impl/file_browser.py+++ b/marimo/_plugins/ui/_impl/file_browser.py@@ -104,21 +104,23 @@ def _common_parent(paths: Sequence[Path]) -> Path:def _is_path_within(path: Path, parent: Path) -> bool:- """Return whether `path` resolves within `parent`."""+ """Return whether `path` resolves within `parent`.- def resolve_existing(candidate: Path) -> Path:+ A path that does not exist can still be within `parent`.+ """++ def resolve_for_containment(candidate: Path) -> Path:try:return candidate.resolve(strict=True)+ except FileNotFoundError:+ return candidate.resolve()except TypeError:# Some Path subclasses do not accept pathlib's `strict` argument.- resolved = candidate.resolve()- if not resolved.exists():- raise FileNotFoundError(resolved) from None- return resolved+ return candidate.resolve()try:- resolved_path = resolve_existing(path)- resolved_parent = resolve_existing(parent)+ resolved_path = resolve_for_containment(path)+ resolved_parent = resolve_for_containment(parent)if not is_cloudpath(resolved_path):resolved_path = Path(resolved_path)resolved_parent = Path(resolved_parent)@@ -539,9 +541,14 @@ class file_browser(files: list[TypedFileBrowserFileInfo] = []# Sort based on natural sort (alpha, then num)- all_file_paths = sorted(- path.iterdir(), key=lambda f: natural_sort(f.name)- )+ try:+ all_file_paths = sorted(+ path.iterdir(), key=lambda f: natural_sort(f.name)+ )+ except FileNotFoundError:+ raise FileNotFoundError(+ f"Directory {path} does not exist."+ ) from Noneis_truncated = Falsefor files_examined, file in enumerate(all_file_paths, 1):diff --git a/tests/_plugins/ui/_impl/test_file_browser.py b/tests/_plugins/ui/_impl/test_file_browser.pyindex 8299827d77..8663162436 100644--- a/tests/_plugins/ui/_impl/test_file_browser.py+++ b/tests/_plugins/ui/_impl/test_file_browser.py@@ -58,7 +58,9 @@ def test_is_path_within_cases(tmp_path: Path) -> None:assert _is_path_within(jail, jail) is Trueassert _is_path_within(outside, jail) is Falseassert _is_path_within(root, jail) is False- assert _is_path_within(jail / "nope.txt", jail) is False+ # Containment does not require existence.+ assert _is_path_within(jail / "nope.txt", jail) is True+ assert _is_path_within(root / "nope.txt", jail) is Falsedef test_is_path_within_rejects_symlink_escape(tmp_path: Path) -> None:@@ -600,6 +602,37 @@ def test_navigation_restriction_dotdot_escape(tmp_path: Path) -> None:fb._list_directory(ListDirectoryArgs(path=traversal))+def test_missing_path_inside_root_reports_not_found(tmp_path: Path) -> None:+ """A missing path inside the root reports not-found, not a restriction."""+ restricted = tmp_path / "restricted"+ restricted.mkdir()+ missing = restricted / "missing"++ fb = file_browser(initial_path=restricted, restrict_navigation=True)+ with pytest.raises(FileNotFoundError, match="does not exist"):+ fb._list_directory(ListDirectoryArgs(path=str(missing)))+++def test_missing_path_outside_root_reports_restriction(tmp_path: Path) -> None:+ """A missing path outside the root still reports a restriction."""+ restricted = tmp_path / "restricted"+ restricted.mkdir()+ missing = tmp_path / "outside" / "missing"++ fb = file_browser(initial_path=restricted, restrict_navigation=True)+ with pytest.raises(RuntimeError, match="Navigation is restricted"):+ fb._list_directory(ListDirectoryArgs(path=str(missing)))+++def test_missing_path_without_restriction_reports_not_found(+ tmp_path: Path,+) -> None:+ """Unrestricted navigation to a missing path reports not-found."""+ fb = file_browser(initial_path=tmp_path, restrict_navigation=False)+ with pytest.raises(FileNotFoundError, match="does not exist"):+ fb._list_directory(ListDirectoryArgs(path=str(tmp_path / "missing")))++def test_name_method() -> None:fb = file_browser(initial_path=Path.cwd())fb._value = [