streamich / memfs

JavaScript file system utilities
http://streamich.github.io/memfs/
Apache License 2.0
1.77k stars 130 forks source link

Using a file as a directory throws ENOENT instead of ENOTDIR #1050

Open NotWearingPants opened 4 months ago

NotWearingPants commented 4 months ago

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.

G-Rath commented 4 months ago

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.

NotWearingPants commented 4 months ago

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

BadIdeaException commented 1 month ago

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

G-Rath commented 1 month ago

@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 😅

BadIdeaException commented 1 month ago

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.