Fix cached_files silently returning stale file on read-only filesystem (EROFS) (#47852)
* Fix cached_files silently returning stale file on read-only filesystem (EROFS)
When the HF Hub cache lives on a read-only filesystem (EROFS, errno 30),
`hf_hub_download` contacts the Hub, resolves a newer commit hash, and then
fails with an `OSError(EROFS)` when trying to write the new snapshot pointer
or blob to disk. That error is not caught inside `hf_hub_download` itself
and bubbles up to the `except` block in `cached_files`.
The existing guards there only re-raise for `RepositoryNotFoundError`,
`RevisionNotFoundError`, `PermissionError` (EACCES, errno 13), and
`ValueError`. Python maps EACCES to its own `PermissionError` subclass, but
EROFS (errno 30) is a plain `OSError` that does **not** satisfy
`isinstance(e, PermissionError)`. Without this fix it falls through to the
stale-cache recovery block, which silently returns whatever old file was
previously cached — even though the Hub has a newer revision.
Fix: add an explicit `elif isinstance(e, OSError) and e.errno == errno.EROFS`
guard that re-raises the error before the recovery block, giving callers
(e.g. the CI `_with_tmpdir_cache_fallback` wrapper) a chance to catch it and
retry the download against a writable temporary cache directory.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Simplify EROFS guard comment in cached_files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
---------
Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>