Skip to content

[HfFileSystem] fix ls() on new empty bucket - #5097

Merged
hanouticelina merged 2 commits into
mainfrom
fix-ls-new-bucket
Oct 7, 2026
Merged

hanouticelina merged 2 commits into
mainfrom
fix-ls-new-bucket

Conversation

@lhoestq

@lhoestq lhoestq commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

it was raising FileNotFoundError on a new empty bucket instead of returning empty list.


Note

Low Risk
Narrow bucket listing semantics change with targeted tests; non-existent prefixes still fail as before.

Overview
Fixes HfFileSystem treating a newly created bucket with no objects as missing when listing the bucket root.

_list_bucket_tree_with_folders now raises EntryNotFoundError only when a non-empty prefix returns no entries; listing the bucket root with zero objects returns an empty result instead of erroring. _ls_tree seeds dircache[path] = [] when a listing is empty so child-path info() can resolve against the parent cache.

Adds integration test test_list_empty_bucket covering root ls, glob, find, and that non-existent subpaths still raise FileNotFoundError.

Reviewed by Cursor Bugbot for commit e2b4518. Bugbot is set up for automated code reviews on this repo. Configure here.

@bot-ci-comment

bot-ci-comment Bot commented Oct 6, 2026

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@ChihebBENCHEIKH1

Copy link
Copy Markdown
Contributor

Heads-up: with this change, a second ls() on a bucket file returns [].

  • ls() on a bucket file lists the file itself, so path is the file and isn't in dircache yet.
  • The new block then caches dircache[path] = [], and the next ls() returns that empty listing.
from huggingface_hub import HfFileSystem

fs = HfFileSystem(token=False, skip_instance_cache=True)
f = "buckets/hf-internal-testing/integration-failures/history/2026-06-02.json"
print(fs.ls(f, detail=False))
print(fs.ls(f, detail=False))

On this branch:

['buckets/hf-internal-testing/integration-failures/history/2026-06-02.json']
[]

Keying the block on an empty result (if not out:) avoids it. I checked that with mocked listings on top of #5099, which fixes another bug in the same loop (#5098).

@hanouticelina hanouticelina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment thread src/huggingface_hub/hf_file_system.py Resolved
@hanouticelina
hanouticelina merged commit 14a48e7 into main Oct 7, 2026
30 of 31 checks passed
@hanouticelina
hanouticelina deleted the fix-ls-new-bucket branch October 7, 2026 09:26
@lhoestq

lhoestq commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

thank you both !

@huggingface-hub-bot

Copy link
Copy Markdown
Contributor

This PR has been shipped as part of the v2.2.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants