Node: Inconsistent behavior with fs module accross platforms

Created on 21 Dec 2017  路  2Comments  路  Source: nodejs/node

  • Version: v9.2.0
  • Platform: Windows 8.1, Windows 10, Ubuntu 17.10
  • Subsystem: fs

In Windows, if a file name ends with forward slash (/), fs interprets it as regular file (like there is no slash at all) and does not throw any errors. In Linux, it throws error

EISDIR: illegal operation on a directory, open

IMO, fs should either throw the same error in Windows, or don't throw the error in Linux.

Of course, when we talk about file name consistency, there will always be some characters which are allowed in Linux, but not allowed in Windows (like \u0001), but that inconsistency is a result of the way OS handles these situations. However, if a file name ends with a forward slash, it either means that someone mistakenly tried to open directory as a file, or just have forgotten to remove the slash. Therefore, fs should be consistent with the way it handles forward slash at the end of the file name. It means: either throw error always, or behave like there is no slash.

I made a script which shows how it currently works.

fs help wanted windows

All 2 comments

Thanks for reporting this issue!

So

require('fs').wrtieFileSync('test/', 'smth');
  • on Windows creates file test with smth inside,
  • on Linux it fails with Error: EISDIR: illegal operation on a directory, open 'test/'

Yep, this looks like a bug for me.

@nodejs/fs

@bzoz I don't know if am looking at the right place, But after looking at this line commet https://github.com/libuv/libuv/blob/2b32e77bb6f41e2786168ec0f32d1f0fcc78071b/src/win/fs.c#L519
at libuv windows implementation it seem the same code is used to access files and directories.
So I fixed it in the JS implementation, I have made a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Icemic picture Icemic  路  3Comments

loretoparisi picture loretoparisi  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

addaleax picture addaleax  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments