Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix ls new empty bucket
  • Loading branch information
lhoestq committed Oct 6, 2026
commit 87aea157b5167a0bfb281201c56696482b6ce0f7
8 changes: 7 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,10 @@ def _ls_tree(
depth = cache_path[len(path) :].count("/")
if maxdepth is None or depth <= maxdepth:
out.append(cache_path_info)
if path not in self.dircache:
# Empty listing (e.g. a bucket with zero objects): still register the directory as
# listed so callers reading `dircache` directly don't miss the key.
Comment thread
hanouticelina marked this conversation as resolved.
Resolved
self.dircache[path] = []
return out

def _list_bucket_tree_with_folders(
Expand Down Expand Up @@ -698,7 +702,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