szwacz / fs-jetpack

Better file system API for Node.js
MIT License
777 stars 41 forks source link

Inconsistent `exists` behavior on Linux/Windows in edge case #97

Open papb opened 4 years ago

papb commented 4 years ago
const jetpack = require('fs-jetpack');
jetpack.write('a', 'anything');
console.log(jetpack.exists('a/b'));

This gives false on Windows, but gives an error in Linux:

ENOTDIR: not a directory, stat '/home/runner/work/test/a/b'

I see meaning in both behaviors, but I think one of them should be chosen so that fs-jetpack behaves consistently on this across platforms.

In my use-case, I wanted false in both cases, but I can see meaning in getting an error as well. I am not sure which behavior to suggest.

Regardless, I worked around this with a try-catch, so don't rush about it. Just letting you know.

szwacz commented 4 years ago

The problem as you described it, is that there is a file on disk where your path expects directory. Returning false in that case can be misleading, because you just can't create directory or file on path a/b. Technically yes, this file doesn't exist, but more important is that the path is invalid for current state of files on disk. I'd say it should throw.

szwacz commented 3 years ago

The problem is rooted in windows operating system itself: https://github.com/nodejs/node/issues/18014 You can check it manually, but this check seems to be time-consuming and will fire also when path is valid, but doesn't exist. Maybe I'll tacle it as better, coherent behaviour across whole API, not only in exists case. Need to think more.