Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 6 additions & 1 deletion src/huggingface_hub/hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,9 @@ def _ls_tree(
depth = cache_path[len(path) :].count("/")
if maxdepth is None or depth <= maxdepth:
out.append(cache_path_info)
if not out:
# Empty bucket: `info()` on a child path reads `dircache[parent]`
self.dircache[path] = []
return out

def _list_bucket_tree_with_folders(
Expand Down Expand Up @@ -698,7 +701,9 @@ def _list_bucket_tree_with_folders(
):
bucket_folder.uploaded_at = parent_bucket_folder.uploaded_at

if not out:
# An empty listing is only a "not found" when a specific prefix was requested. Listing the
# bucket root itself is valid even with zero objects (e.g. freshly created bucket).
if prefix and not out:
raise EntryNotFoundError(f"File not found in bucket '{bucket_id}': '{prefix}'")
return out

Expand Down
17 changes: 17 additions & 0 deletions tests/test_buckets_hf_file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ def test_prefix_collision(self):
with pytest.raises(FileNotFoundError):
self.hffs.ls(colliding_path)

def test_list_empty_bucket(self):
bucket_id = self.api.create_bucket(repo_name()).bucket_id
try:
root = f"hf://buckets/{bucket_id}"
assert self.hffs.exists(root)
assert self.hffs.isdir(root)
assert self.hffs.ls(root) == []
assert self.hffs.ls(root, recursive=True) == []
assert self.hffs.glob(f"{root}/*") == []
assert self.hffs.find(root) == []
assert not self.hffs.exists(f"{root}/nope")
assert self.hffs.glob(f"{root}/nope/*") == []
with pytest.raises(FileNotFoundError):
self.hffs.ls(f"{root}/nope")
finally:
self.api.delete_bucket(bucket_id)


class TestHfFileSystemBucketRW(_HfFileSystemBucketChecks, _HfFileSystemBaseRWTests):
__test__ = True
Expand Down
Loading