Skip to content

fs: require existing parents in MemoryFS and reject rename into own subtree - #857

Merged
ewhauser merged 3 commits into
mainfrom
claude/dreamy-hermann-4b45a2
Oct 19, 2026
Merged

ewhauser merged 3 commits into
mainfrom
claude/dreamy-hermann-4b45a2

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Summary

Fixes two MemoryFS bugs: a runtime crash, and creating operations that didn't behave like bash.

1. Rename into own subtree panicked

MemoryFS.Rename("/a", "/a/b") deleted /a's subtree and then dereferenced the missing parent. The mv builtin guards against this, but any external command calling commands.CommandFS.Rename crashed the runtime. Rename now returns EINVAL, the same as OverlayFS's isDescendantPath guard. The check runs on resolved paths, so /alias/c with /alias -> /a is caught too.

While checking Link for the same pattern I found a second panic. Link built its children entry from the unresolved destination path, so ln /f /symlinked-dir/x wrote into the symlink node's nil children map. It now uses the resolved path, like Symlink already does.

2. O_CREATE silently created missing parents

echo hi > /nope/dir/x returned 0 and created /nope/dir. It now matches bash:

$ gbash -c 'echo hi > /tmp/nope/dir/x; echo rc=$?; touch /tmp/f; echo hi > /tmp/f/x; echo rc=$?'
/tmp/nope/dir/x: No such file or directory
rc=1
/tmp/f/x: Not a directory
rc=1

Before this change, the first command printed rc=0, and the second printed /tmp/f/x: Is a directory because ErrInvalid was mapped to "Is a directory".

  • OpenFile with O_CREATE (truncate, append, excl), Symlink, Link and Rename now fail with ENOENT for a missing parent and ENOTDIR for a non-directory parent. Rename follows bash mv/rename(2) and gets the same treatment.
  • The shell's open-error text now maps ENOTDIR to "Not a directory" (internal/shell/core.go).
  • MkdirAll, initial-file seeding and the private cloneSpecialFile helper still create parents, because creating them is their job.

OverlayFS

The overlay's upper layer is a MemoryFS, and copy-up writes upper paths whose parents may exist only in the lower layer. That upper layer now uses an unexported implicitParents mode that keeps the old behavior. The only change to fs/overlay.go is one line in NewOverlay, to stay clear of the in-flight OverlayFS symlink rework. Known gap: in overlay mode, echo hi > /missing/x still succeeds. Adding a parent check against the merged view belongs with that rework; there's a hand-over note for it.

Behavior change for library users

Code that called fs.NewMemory().OpenFile("/a/b/c", O_CREATE…) without MkdirAll first now gets ENOENT. Several test helpers relied on the old behavior; they now call MkdirAll first. In contrib/awk, five tests wrote > /data/x without mkdir -p /data, so they hit exactly this bug.

SPEC

Updated the MemoryFS notes in SPEC.md (parent requirement, Rename EINVAL, overlay upper exception).

Testing

  • New fs/memory_create_test.go covers ENOENT/ENOTDIR for each O_CREATE flavor, a symlinked or dangling parent, Symlink/Link/Rename into a missing parent, rename into own subtree (direct, through a symlink, and the /a → /ab sibling case), Link through a symlinked parent, and overlay copy-up into lower-only dirs. Run against the old memory.go, the rename and link tests panic and the O_CREATE test fails.
  • New internal/runtime/memoryfs_parents_test.go is end-to-end: redirect exit codes and stderr, plus a custom command calling inv.FS.Rename into its own subdirectory.
  • go test across the workspace plus contrib modules (with the diff/ripgrep oracles set) passes, except two failures that also happen on main without this change: scripts/bench-compare TestExpansionStressScenarioMatchesExpectedStdoutUnderBash and contrib/nodejs TestNodeJSShebangViaEnvWorks.
  • make conformance-test (full suite) passes.
  • make lint passes with 0 issues, and so does golangci-lint in contrib/awk.

…ubtree

MemoryFS creating operations (OpenFile with O_CREATE, Symlink, Link,
Rename) silently created missing parent directories, so
`echo hi > /nope/dir/x` succeeded where bash fails with ENOENT. They now
fail with ENOENT for a missing parent and ENOTDIR for a non-directory
parent, and the shell renders ENOTDIR as "Not a directory".

Rename of a directory into its own descendant deleted the subtree and
then dereferenced a nil parent; it now returns EINVAL like OverlayFS.
Link used the unresolved destination path and panicked when the parent
was a symlinked directory; it now uses the resolved path.

OverlayFS's private upper layer keeps implicit parent creation so
copy-up into lower-only directories still works.
@ewhauser
ewhauser enabled auto-merge (squash) October 19, 2026 07:10
@ewhauser
ewhauser merged commit 169d68e into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/dreamy-hermann-4b45a2 branch October 19, 2026 07:19
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.

1 participant