Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changes/2808.bugfix.md
Original file line number Diff line number Diff line change
@@ -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`.
17 changes: 17 additions & 0 deletions src/zarr/storage/_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
25 changes: 25 additions & 0 deletions tests/test_store/test_fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down