diff --git a/changes/2808.bugfix.md b/changes/2808.bugfix.md new file mode 100644 index 0000000000..23ca23632a --- /dev/null +++ b/changes/2808.bugfix.md @@ -0,0 +1 @@ +Fixed `FsspecStore.from_url` failing with a confusing `FileNotFoundError` on the first write for a new store path on a local filesystem. `from_url` now raises a clear `FileNotFoundError` at creation time when the directory does not exist, directing users to create it first or pass `auto_mkdir` in `storage_options`. diff --git a/src/zarr/storage/_fsspec.py b/src/zarr/storage/_fsspec.py index 137043c901..814324789d 100644 --- a/src/zarr/storage/_fsspec.py +++ b/src/zarr/storage/_fsspec.py @@ -4,6 +4,7 @@ import warnings from contextlib import suppress from typing import TYPE_CHECKING, Any +from urllib.parse import urlparse from packaging.version import parse as parse_version @@ -252,6 +253,22 @@ def from_url( opts = {"asynchronous": True, **opts} fs, path = url_to_fs(url, **opts) + if ( + not read_only + and path + and urlparse(url).scheme in ("", "file", "local") + and not opts.get("auto_mkdir") + and not getattr(fs, "auto_mkdir", False) + and not fs.exists(path) + ): + # The local filesystem does not create directories on write + # unless auto_mkdir is set, so a store at a new path only fails + # on the first write (#2808). Raise here instead, where the + # cause is clear. + raise FileNotFoundError( + f"{path} does not exist. Create the directory first, or pass " + "storage_options={'auto_mkdir': True} so it is created on write." + ) if not fs.async_impl: fs = _make_async(fs) diff --git a/tests/test_store/test_fsspec.py b/tests/test_store/test_fsspec.py index f26a6f316a..242b9ed0b4 100644 --- a/tests/test_store/test_fsspec.py +++ b/tests/test_store/test_fsspec.py @@ -506,6 +506,31 @@ def test_wrap_sync_filesystem(tmp_path: pathlib.Path) -> None: array_roundtrip(store) +@pytest.mark.skipif( + parse_version(fsspec.__version__) < parse_version("2024.12.0"), + reason="No AsyncFileSystemWrapper", +) +def test_wrap_sync_filesystem_new_store_path(tmp_path: pathlib.Path) -> None: + # Regression for #2808: the store root does not exist yet, and the + # wrapped local filesystem does not create parent directories on write, + # so from_url raises at creation instead of failing on the first write. + from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper + + with pytest.raises(FileNotFoundError, match="does not exist") as excinfo: + FsspecStore.from_url(f"file://{tmp_path}/new.zarr") + assert "auto_mkdir" in str(excinfo.value) + + # With auto_mkdir set, the same path is created on write as before. + store = FsspecStore.from_url( + f"file://{tmp_path}/new.zarr", storage_options={"auto_mkdir": True} + ) + assert isinstance(store.fs, AsyncFileSystemWrapper) + array_roundtrip(store) + + # A read-only store never writes, so a missing path is not an error. + FsspecStore.from_url(f"file://{tmp_path}/missing.zarr", read_only=True) + + @pytest.mark.skipif( parse_version(fsspec.__version__) >= parse_version("2024.12.0"), reason="No AsyncFileSystemWrapper",