diff --git a/src/huggingface_hub/hf_file_system.py b/src/huggingface_hub/hf_file_system.py index a3e3388e30..f5690f654c 100644 --- a/src/huggingface_hub/hf_file_system.py +++ b/src/huggingface_hub/hf_file_system.py @@ -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): @@ -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) if not out: # Empty bucket: `info()` on a child path reads `dircache[parent]` self.dircache[path] = [] diff --git a/tests/test_hf_file_system.py b/tests/test_hf_file_system.py index 9084de5f70..56cc2b4cff 100644 --- a/tests/test_hf_file_system.py +++ b/tests/test_hf_file_system.py @@ -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