Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/huggingface_hub/hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ def _ls_tree(
revision=resolved_path.revision,
repo_type=resolved_path.repo_type,
)
listings: dict[str, list[dict[str, Any]]] = {}
for path_info in tree:
cache_path = root_path + "/" + path_info.path
if isinstance(path_info, RepoFile):
Expand Down Expand Up @@ -650,11 +651,13 @@ def _ls_tree(
"type": "directory",
"uploaded_at": path_info.uploaded_at,
}
parent_path = self._parent(cache_path_info["name"])
self.dircache.setdefault(parent_path, []).append(cache_path_info)
if cache_path != path: # listing a bucket file returns the file, not its folder's listing
listings.setdefault(self._parent(cache_path), []).append(cache_path_info)
depth = cache_path[len(path) :].count("/")
if maxdepth is None or depth <= maxdepth:
out.append(cache_path_info)
# replace cached listings, only once the listing is complete
self.dircache.update(listings)
return out

def _list_bucket_tree_with_folders(
Expand Down
8 changes: 8 additions & 0 deletions tests/test_hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,14 @@ def test_list_data_file(self):
self._check_info_not_expanded(files[0])
self._check_info_fields(files[0])

def test_list_data_directory_after_listing_subpaths(self):
"""Regression test for #5098."""
data_dir = self.hf_path + "/data"
self.hffs.ls(self.text_file)
self.hffs.find(self.hf_path)
self.hffs.ls(data_dir, refresh=True)
assert sorted(self.hffs.ls(data_dir, detail=False)) == [data_dir + "/binary_data.bin", self.text_file]

def test_list_root_directory_no_detail_then_with_detail(self):
files = sorted(self.hffs.ls(self.hf_path, detail=False))
assert len(files) == 2
Expand Down
Loading