memfs icon indicating copy to clipboard operation
memfs copied to clipboard

Using a file as a directory throws ENOENT instead of ENOTDIR

Open SnirBroshi opened this issue 1 year ago • 5 comments

Using memfs:

const { fs } = require('memfs');
fs.writeFileSync('/foo', 'hello');
fs.readFileSync('/foo/bar'); // throws ENOENT

Using the builtin fs (on Linux):

const fs = require('node:fs');
fs.writeFileSync('/foo', 'hello');
fs.readFileSync('/foo/bar'); // throws ENOTDIR

Using the builtin fs (on Windows):

const fs = require('node:fs');
fs.writeFileSync('C:/foo', 'hello');
fs.readFileSync('C:/foo/bar'); // throws ENOENT

I'm not sure which behavior makes more sense and if this difference is a bug in Node.js, but I think that if !isWin then memfs should also throw ENOTDIR.

SnirBroshi avatar Aug 02 '24 22:08 SnirBroshi

Can you mention what version of Node you're using? As I suspect this might have been something that changed - note too we don't aim to 1:1 match the behaviour of Node especially in regards to error codes due to the potential number of differences across both Node majors and OSs, but this one seems like it should be straightforward to support.

G-Rath avatar Aug 02 '24 22:08 G-Rath

I tested this with memfs v4.11.1 and Node.js v20.12.2

SnirBroshi avatar Aug 02 '24 22:08 SnirBroshi

@G-Rath This is fixed in 4.12.0. This issue can be closed now.

BadIdeaException avatar Oct 11 '24 06:10 BadIdeaException

@BadIdeaException do you know if we've got a test to ensure this doesn't regress in the future?

It looks like we might have actually have a test that verifies the opposite error than what we want here? But I'm on mobile so might have missed something 😅

G-Rath avatar Oct 12 '24 01:10 G-Rath

Hmm, you're right. I didn't read closely enough. This is only fixed for creating a file, where openSync.test.ts tests that the correct error is thrown. (NB: mkdirSync.test.ts tests the analogous case when trying to create a directory under a file, at least in recursive mode.)

When trying to read from an illegally nested file, I am still seeing ENOENT, just like OP said.

BadIdeaException avatar Oct 13 '24 15:10 BadIdeaException

:tada: This issue has been resolved in version 4.33.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Aug 01 '25 21:08 github-actions[bot]