feat: throw ENOTDIR when reading files with trailing slash#1150
Merged
Conversation
Co-authored-by: streamich <[email protected]>
Copilot
AI
changed the title
[WIP] Paths with trailing slashes should be treated as directories
feat: throw ENOTDIR when reading files with trailing slash
Aug 1, 2025
streamich
reviewed
Aug 1, 2025
Comment on lines
+61
to
+65
| // Root path without trailing slash should throw EISDIR | ||
| expect(() => fs.readFileSync('/', 'utf8')).toThrow(/EISDIR/); | ||
|
|
||
| // Root path with trailing slash should also throw EISDIR | ||
| expect(() => fs.readFileSync('/', 'utf8')).toThrow(/EISDIR/); |
Contributor
|
🎉 This PR is included in version 4.34.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 change makes memfs behavior consistent with Node.js on Linux by throwing
ENOTDIRwhen attempting to read a file using a path with a trailing slash.Problem
Previously, memfs would successfully read a file even when accessed with a trailing slash:
However, native Node.js on Linux throws an
ENOTDIRerror in this case:Solution
Modified the
_readfilemethod insrc/node/volume.tsto:ENOTDIRerror when trying to read a file with a trailing slashThe fix preserves existing behavior for directories and handles edge cases like symlinks, multiple trailing slashes, and the root path correctly.
Testing
Added comprehensive test cases covering:
All existing tests continue to pass, ensuring backward compatibility.
Fixes #1051.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.