Skip to content

Commit 39a1d55

Browse files
authored
Fix caching error when offline and no refs dir (#1307)
1 parent c06efc3 commit 39a1d55

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/huggingface_hub/file_download.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,18 +1177,24 @@ def hf_hub_download(
11771177
"We have no connection or you passed local_files_only, so"
11781178
" force_download is not an accepted option."
11791179
)
1180+
1181+
# Try to get "commit_hash" from "revision"
1182+
commit_hash = None
11801183
if REGEX_COMMIT_HASH.match(revision):
11811184
commit_hash = revision
11821185
else:
11831186
ref_path = os.path.join(storage_folder, "refs", revision)
1184-
with open(ref_path) as f:
1185-
commit_hash = f.read()
1186-
1187-
pointer_path = os.path.join(
1188-
storage_folder, "snapshots", commit_hash, relative_filename
1189-
)
1190-
if os.path.exists(pointer_path):
1191-
return pointer_path
1187+
if os.path.isfile(ref_path):
1188+
with open(ref_path) as f:
1189+
commit_hash = f.read()
1190+
1191+
# Return pointer file if exists
1192+
if commit_hash is not None:
1193+
pointer_path = os.path.join(
1194+
storage_folder, "snapshots", commit_hash, relative_filename
1195+
)
1196+
if os.path.exists(pointer_path):
1197+
return pointer_path
11921198

11931199
# If we couldn't find an appropriate file on disk,
11941200
# raise an error.

tests/test_file_download.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,24 @@ def test_hf_hub_download_with_empty_subfolder(self):
318318
self.assertEqual(filepath.name, CONFIG_NAME)
319319
self.assertEqual(Path(filepath).parent.parent.name, "snapshots")
320320

321+
def test_hf_hub_download_offline_no_refs(self):
322+
"""Regression test for #1305.
323+
324+
If "refs/" dir did not exists on "local_files_only" (or connection broken), a
325+
non-explicit `FileNotFoundError` was raised (for the "/refs/revision" file) instead
326+
of the documented `LocalEntryNotFoundError` (for the actual searched file).
327+
328+
See https://github.com/huggingface/huggingface_hub/issues/1305.
329+
"""
330+
with SoftTemporaryDirectory() as cache_dir:
331+
with self.assertRaises(LocalEntryNotFoundError):
332+
hf_hub_download(
333+
DUMMY_MODEL_ID,
334+
filename=CONFIG_NAME,
335+
local_files_only=True,
336+
cache_dir=cache_dir,
337+
)
338+
321339
def test_hf_hub_url_with_empty_subfolder(self):
322340
"""
323341
Check subfolder arg is processed correctly when empty string is passed to

0 commit comments

Comments
 (0)