fix: correct ENOTDIR vs ENOENT error when using file as directory#1147
Merged
Conversation
…arios Co-authored-by: streamich <[email protected]>
Copilot
AI
changed the title
[WIP] Using a file as a directory throws ENOENT instead of ENOTDIR
fix: correct ENOTDIR vs ENOENT error when using file as directory
Aug 1, 2025
streamich
requested changes
Aug 1, 2025
streamich
left a comment
Owner
There was a problem hiding this comment.
Tests are failing:
PASS src/__tests__/setTimeoutUnref.test.ts
Summary of all failing tests
FAIL src/node/__tests__/volume.test.ts (6.046 s)
● volume › Volume › .getLink(steps) › ["dir", "child.sh"] - Get a child link in a dir
ENOTDIR: not a directory, '/dir/child.sh'
146 |
147 | export function createError(errorCode: string, func = '', path = '', path2 = '', Constructor = Error) {
> 148 | const error = new Constructor(formatError(errorCode, func, path, path2));
| ^
149 | (error as any).code = errorCode;
150 |
151 | if (path) {
at createError (src/node/util.ts:148:17)
at Superblock.walk (src/core/Superblock.ts:[226](https://github.com/streamich/memfs/actions/runs/16679004993/job/47224825085?pr=1147#step:5:227):28)
at Superblock.getLink (src/core/Superblock.ts:259:17)
at Object.<anonymous> (src/node/__tests__/volume.test.ts:60:33)
When done, make sure to run Prettier.
Co-authored-by: streamich <[email protected]>
Contributor
Author
Fixed the test failures. The issue was that my ENOTDIR check was happening before symlink resolution, causing legitimate symlink traversals like |
Contributor
|
🎉 This PR is included in version 4.33.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes an issue where
memfsthrowsENOENTinstead of the more semantically correctENOTDIRwhen attempting to use a file as a directory on non-Windows platforms.Problem
When trying to access a path that traverses through a file (e.g.,
/foo/barwhere/foois a file),memfswas throwingENOENT: no such file or directoryinstead ofENOTDIR: not a directory. This behavior didn't match Node.js's platform-specific behavior:Root Cause
The issue was in the
walkmethod insrc/core/Superblock.ts. When attempting to traverse through a non-directory, the error check was happening at the wrong time and wasn't accounting for symlink resolution.Solution
Fixed the traversal logic: Added a check after symlink resolution to detect when attempting to traverse through a non-directory.
Added platform-specific behavior: Implemented platform-aware error codes to match Node.js behavior:
ENOTDIR(more semantically correct)ENOENT(matches Node.js Windows behavior)Preserved compatibility: The check only applies to filesystem operations (when
checkExistence=true), ensuring internal APIs and symlink traversals continue to work correctly.Changes
src/core/Superblock.ts: Added platform-specific error handling after symlink resolution in thewalkmethodsrc/node/__tests__/volume/readFileSync-enotdir.test.ts: Added comprehensive tests for both platform behaviorsThe fix ensures that all file operations (
readFileSync,writeFileSync,statSync, etc.) correctly handle file-as-directory scenarios while maintaining backward compatibility for valid operations and symlink traversals.Fixes #1050.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.