Skip to content

Commit 68e99e5

Browse files
fix: FileBrowser could go two directories up if ".." was selected
When we change the selected file, and we 'sync' the current directory, we should avoid going up two directories if the selected file is "..".
1 parent c9d5b43 commit 68e99e5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

solara/components/file_browser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ def sync_directory_from_selected():
137137
# if we select a file, we need to make sure the directory is correct
138138
# NOTE: although we expect a Path, abuse might make it a string
139139
if isinstance(selected_private, Path):
140-
current_dir.value = selected_private.resolve().parent
140+
# Note that we do not call .resolve() before using .parent, otherwise /some/path/.. will
141+
# set the current directory to /, moving up two levels.
142+
current_dir.value = selected_private.parent.resolve()
141143

142144
solara.use_effect(sync_directory_from_selected, [selected_private])
143145

tests/unit/file_browser_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def Test():
241241
def test_file_browser_programmatic_select():
242242
# using a reactive value to select a file
243243
selected = solara.reactive(cast(Optional[Path], None))
244+
current_dir = solara.reactive(HERE.parent)
244245

245246
@solara.component
246247
def Test():
@@ -258,6 +259,7 @@ def Test():
258259
assert selected.value is None
259260

260261
selected.value = None
262+
rc.close()
261263

262264
# passing selected as a value (non reactive)
263265
@solara.component
@@ -274,3 +276,17 @@ def Test2():
274276
list.test_click("..", double_click=True)
275277
assert list.files != files
276278
assert selected.value is None
279+
rc.close()
280+
281+
@solara.component
282+
def Test3():
283+
return solara.FileBrowser(current_dir, selected=selected.value, on_path_select=selected.set, can_select=True)
284+
285+
div, rc = solara.render_fixed(Test3(), handle_error=False)
286+
assert current_dir.value == HERE.parent
287+
list.test_click("..", double_click=False)
288+
assert current_dir.value == HERE.parent
289+
# this will trigger the sync_directory_from_selected
290+
selected.value = current_dir.value / ".." / "unit" / ".."
291+
assert current_dir.value == HERE.parent
292+
rc.close()

0 commit comments

Comments
 (0)