Skip to content

MemMapFs: Mkdir now errors when the parent directory doesn't exist#599

Open
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/mkdir-requires-parent
Open

MemMapFs: Mkdir now errors when the parent directory doesn't exist#599
c-tonneslan wants to merge 1 commit into
spf13:masterfrom
c-tonneslan:fix/mkdir-requires-parent

Conversation

@c-tonneslan

Copy link
Copy Markdown

Fixes #149.

MemMapFs.Mkdir was silently creating any missing parent directories via registerWithParent, so it behaved like MkdirAll. This diverges from os.Mkdir, which returns ENOENT when the parent doesn't exist.

The practical problem: if you have a typo in a path, Mkdir would happily create the whole tree rather than failing, masking the bug entirely. That's the kind of thing that makes test suites using MemMapFs pass while production code (using OsFs) fails.

Changes:

  • Mkdir checks that the parent directory exists before creating the new one. If the parent is missing it returns os.ErrNotExist.
  • MkdirAll is rewritten to explicitly recurse over missing parents, since it can no longer rely on Mkdir's old implicit creation.
  • TempDir now calls MkdirAll on the base dir before calling Mkdir, since in-memory filesystems start empty.
  • Two tests that called Mkdir with deep paths expecting the old behavior are updated.

Before:

fs := afero.NewMemMapFs()
err := fs.Mkdir("/a/b/c", 0755) // nil — creates /a, /a/b, and /a/b/c silently

After:

err := fs.Mkdir("/a/b/c", 0755)
// mkdir /a/b/c: file does not exist

Previously, MemMapFs.Mkdir would silently create any missing parent
directories through registerWithParent, so it acted like MkdirAll.
This diverges from os.Mkdir, which returns ENOENT if the parent isn't
there. It also masked bugs in callers that relied on the error to know
the path was invalid.

Fix: Mkdir now checks that the parent directory exists before creating
the new one. If the parent is missing it returns os.ErrNotExist, matching
os.Mkdir behavior.

MkdirAll is updated to explicitly recurse over missing parents first,
rather than relying on Mkdir's now-removed implicit creation.

TempDir is updated to ensure the base dir exists before calling Mkdir,
since in-memory filesystems start empty. Two tests that called Mkdir
with deep paths expecting implicit parent creation are fixed to use
MkdirAll or pre-create the parent.

Fixes spf13#149
@CLAassistant

CLAassistant commented May 12, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

c-tonneslan added a commit to c-tonneslan/portfolio that referenced this pull request May 12, 2026
c-tonneslan added a commit to c-tonneslan/c-tonneslan that referenced this pull request May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemMapFs.Mkdir should error if parent path does not exist

2 participants