fix: MemMapFs.OpenFile checks parent dir exists before creating file#572
fix: MemMapFs.OpenFile checks parent dir exists before creating file#572veeceey wants to merge 1 commit into
Conversation
… 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
Test ResultsFull test suite passes: Verified the fix with the new test Also confirmed manually that |
|
gentle ping when you have a moment |
|
The fix is correct — MemMapFs should have matched However, this is a breaking change for downstream consumers and we cannot merge it without coordination. I checked the major consumers: Hugo ( Grafana k6 ( Viper and Cobra are safe (Viper uses Holding this until we can do a coordinated release that fixes Hugo's test files simultaneously. The migration path for callers is straightforward: add |
|
The fix here is correct — MemMapFs should require parent directories to exist before creating a file, matching os.OpenFile semantics. However, this is an observable behavior change for existing users: code that currently does WriteFile(memFs, "/some/path/file.txt", ...) without a preceding MkdirAll will silently break. MemMapFs is widely used as a frictionless test double, and many codebases rely on this permissiveness. Before merging we need to assess the blast radius. A few questions:
The implementation and tests are good. This is a policy/impact question, not a correctness question. |
Fixes #270
MemMapFs.OpenFilewithO_CREATEwas silently creating parent directories that don't exist. This meantWriteFilewould happily write to non-existent paths in MemMapFs whileOsFswould correctly return an error -- inconsistent behavior.The fix adds a parent directory existence check in
OpenFilebefore callingCreate. If the parent doesn't exist, it returnsos.ErrNotExist, matchingos.OpenFilesemantics.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.