From 69a995cf48f061cfc5c77e6299b15478d95beaef Mon Sep 17 00:00:00 2001 From: FordProductions <127124202+1915rising@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:11:18 -0500 Subject: [PATCH 1/2] fix(storage): default auto_mkdir for local URLs in FsspecStore.from_url --- changes/2808.bugfix.md | 1 + src/zarr/storage/_fsspec.py | 7 +++++++ tests/test_store/test_fsspec.py | 14 ++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 changes/2808.bugfix.md diff --git a/changes/2808.bugfix.md b/changes/2808.bugfix.md new file mode 100644 index 0000000000..4e7e6ae1eb --- /dev/null +++ b/changes/2808.bugfix.md @@ -0,0 +1 @@ +Fixed `FsspecStore.from_url` failing with `FileNotFoundError` for a new store path on a local filesystem, because the local filesystem does not create parent directories on write by default. `from_url` now defaults `auto_mkdir` to `True` for local URLs, unless the caller set it in `storage_options`. diff --git a/src/zarr/storage/_fsspec.py b/src/zarr/storage/_fsspec.py index 137043c901..e2881fbe7b 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 @@ -250,6 +251,12 @@ def from_url( opts = storage_options or {} opts = {"asynchronous": True, **opts} + if urlparse(url).scheme in ("", "file", "local"): + # The local filesystem does not create parent directories on + # write by default, so a new store path fails with + # FileNotFoundError (#2808). Default auto_mkdir to True for + # local URLs, unless the caller set it explicitly. + opts.setdefault("auto_mkdir", True) fs, path = url_to_fs(url, **opts) if not fs.async_impl: diff --git a/tests/test_store/test_fsspec.py b/tests/test_store/test_fsspec.py index f26a6f316a..3a4ea0ed15 100644 --- a/tests/test_store/test_fsspec.py +++ b/tests/test_store/test_fsspec.py @@ -506,6 +506,20 @@ 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. + from fsspec.implementations.asyn_wrapper import AsyncFileSystemWrapper + + store = FsspecStore.from_url(f"file://{tmp_path}/new.zarr") + assert isinstance(store.fs, AsyncFileSystemWrapper) + array_roundtrip(store) + + @pytest.mark.skipif( parse_version(fsspec.__version__) >= parse_version("2024.12.0"), reason="No AsyncFileSystemWrapper", From cdaa454a331e77ffeb98a0fd3e5c69bad223ddb7 Mon Sep 17 00:00:00 2001 From: FordProductions <127124202+1915rising@users.noreply.github.com> Date: Fri, 4 Dec 2026 08:16:35 -0500 Subject: [PATCH 2/2] fix(storage): raise a clear error in FsspecStore.from_url when the local directory does not exist --- changes/2808.bugfix.md | 2 +- src/zarr/storage/_fsspec.py | 22 ++++++++++++++++------ tests/test_store/test_fsspec.py | 15 +++++++++++++-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/changes/2808.bugfix.md b/changes/2808.bugfix.md index 4e7e6ae1eb..23ca23632a 100644 --- a/changes/2808.bugfix.md +++ b/changes/2808.bugfix.md @@ -1 +1 @@ -Fixed `FsspecStore.from_url` failing with `FileNotFoundError` for a new store path on a local filesystem, because the local filesystem does not create parent directories on write by default. `from_url` now defaults `auto_mkdir` to `True` for local URLs, unless the caller set it in `storage_options`. +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 e2881fbe7b..814324789d 100644 --- a/src/zarr/storage/_fsspec.py +++ b/src/zarr/storage/_fsspec.py @@ -251,14 +251,24 @@ def from_url( opts = storage_options or {} opts = {"asynchronous": True, **opts} - if urlparse(url).scheme in ("", "file", "local"): - # The local filesystem does not create parent directories on - # write by default, so a new store path fails with - # FileNotFoundError (#2808). Default auto_mkdir to True for - # local URLs, unless the caller set it explicitly. - opts.setdefault("auto_mkdir", True) 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 3a4ea0ed15..242b9ed0b4 100644 --- a/tests/test_store/test_fsspec.py +++ b/tests/test_store/test_fsspec.py @@ -512,13 +512,24 @@ def test_wrap_sync_filesystem(tmp_path: pathlib.Path) -> None: ) 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. + # 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 - store = FsspecStore.from_url(f"file://{tmp_path}/new.zarr") + 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"),