Skip to content

fix: MemMapFs.OpenFile checks parent dir exists before creating file#572

Open
veeceey wants to merge 1 commit intospf13:masterfrom
veeceey:fix/issue-270-openfile-parent-dir-check
Open

fix: MemMapFs.OpenFile checks parent dir exists before creating file#572
veeceey wants to merge 1 commit intospf13:masterfrom
veeceey:fix/issue-270-openfile-parent-dir-check

Conversation

@veeceey
Copy link
Copy Markdown

@veeceey veeceey commented Feb 23, 2026

Fixes #270

MemMapFs.OpenFile with O_CREATE was silently creating parent directories that don't exist. This meant WriteFile would happily write to non-existent paths in MemMapFs while OsFs would correctly return an error -- inconsistent behavior.

The fix adds a parent directory existence check in OpenFile before calling Create. If the parent doesn't exist, it returns os.ErrNotExist, matching os.OpenFile semantics.

Updated existing tests that relied on the implicit directory creation to explicitly create directories first (which is what you'd need to do on a real filesystem anyway). Added a dedicated test covering the bug.

… file

MemMapFs.OpenFile with O_CREATE was silently creating parent directories
that don't exist, diverging from os.OpenFile semantics. This caused
WriteFile to succeed when writing to non-existent directories in
MemMapFs, while OsFs correctly returned an error.

Now OpenFile verifies the parent directory exists before creating a new
file, returning os.ErrNotExist if it doesn't. This matches the behavior
of the real filesystem.

Fixes spf13#270
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 23, 2026

CLA assistant check
All committers have signed the CLA.

@veeceey
Copy link
Copy Markdown
Author

veeceey commented Feb 23, 2026

Test Results

Full test suite passes:

$ go test -count=1 ./...
ok  	github.com/spf13/afero	4.528s
ok  	github.com/spf13/afero/mem	0.195s
ok  	github.com/spf13/afero/tarfs	0.648s
ok  	github.com/spf13/afero/zipfs	0.506s

Verified the fix with the new test TestOpenFileNonExistentDirectory:

$ go test -count=1 -v -run TestOpenFileNonExistentDirectory ./...
=== RUN   TestOpenFileNonExistentDirectory
--- PASS: TestOpenFileNonExistentDirectory (0.00s)

Also confirmed manually that WriteFile to a non-existent directory now returns os.ErrNotExist, matching OsFs behavior.

@veeceey
Copy link
Copy Markdown
Author

veeceey commented Mar 12, 2026

gentle ping when you have a moment

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.

WriteFile creates non-existing directory for MemMapFs but not for OsFs

2 participants