jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.43k stars 775 forks source link

`ensureDir` shouldn't fail when a directory already exists, but `mkdir` throws a write permission error for it #1012

Closed rotemdan closed 10 months ago

rotemdan commented 1 year ago

Reproduction:

import { ensureDir } from "fs-extra"

await ensureDir("D:\\")

Result:

[Error: EPERM: operation not permitted, mkdir 'D:\'] {
  errno: -4048,
  code: 'EPERM',
  syscall: 'mkdir',
  path: 'D:\\'
}

I think that since the method is meant to 'ensure' a directory (which would include the root directory), it should ensure the drive (D: in this case) exists, and do nothing otherwise.

Workaround

Here is my workaround, which also tries to ensure the specified drive actually exists:

import fsExtra from "fs-extra"
import { existsSync } from "fs"
import path from "path"

export async function ensureDir(dirPath) {
    dirPath = path.normalize(dirPath)

    const parsedPath = path.parse(dirPath)

    if (parsedPath.root && !existsSync(parsedPath.root)) {
        throw new Error(`The root path '${parsedPath.root}' does not exist`)
    }

    if (parsedPath.dir != parsedPath.root) {
        return fsExtra.ensureDir(dirPath)
    }
}
RyanZim commented 1 year ago

ensureDir is basically a wrapper around fs.mkdir() with the recursive option set: https://github.com/jprichardson/node-fs-extra/blob/cc7b3b22a984de5124131c7897574091c9df00e1/lib/mkdirs/make-dir.js#L11-L18

The Node.js docs state:

Calling fs.mkdir() when path is a directory that exists results in an error only when recursive is false.

So this seems to actually a bug in Node.js itself. Can you verify and file a Node issue? Also would be good to check if fs.mkdirSync() has the same bug.

rotemdan commented 1 year ago

The issue I was describing is that ensureDir was failing with a permission error when it was given a root path of a drive, like (D:\) :

The logic I was using in my workaround attempt is:

I interpreted the idea of "ensuring a directory exists" to mean:

And in the case of a root directory, I don't see it being necessary to error due permissions issues (as they may actually be common).

Edit: We can extend this idea and say that for any directory, as long as it exists it's fine and we don't care if the user has permissions to write to it? (if that's an issue as well?)

rotemdan commented 1 year ago

Here's my modified workaround, which tries to first check if the path exists and is a directory, and will then return and ignore permission errors in that case:

export async function ensureDir(dirPath: string) {
    dirPath = path.normalize(dirPath)

    if (existsSync(dirPath)) {
        const dirStats = await stat(dirPath)

        if (!dirStats.isDirectory()) {
            throw new Error( `${dirPath} exists but is not a directory.`)
        }
    } else {
        return fsExtra.ensureDir(dirPath)
    }
}
RyanZim commented 1 year ago

Yeah, I'm not sure what the logic for permission issues should be. But in any case, this seems to be something that should be discussed on the Node core level.

rotemdan commented 1 year ago

Here's the way I see it:

By the nature of naming itself, the user may develop expectations on how the method should behave in certain situations.

If the goal of what is currently called ensureDir was just to be alias of mkdir({ recursive: true }), and nothing more, then it should have been called mkdirRecursive.

Arbitrarily declaring that ensureDir is just an alias of mkdir({ recursive: true }), and nothing more, is confusing for users, and creates false expectations.

RyanZim commented 1 year ago

ensureDir also has aliases mkdirp & mkdirs. It actually predates the recursive: true option; Node added this option, largely inspired by fs-extra. It was long after names were chosen that it became an alias.

rotemdan commented 1 year ago

I'm not sure what this means. You're basically just asserting that ensureDir is an arbitrary name, and it doesn't matter to you how it's interpreted.

I mean, if you insist that calling something ensureDir doesn't carry any sort of responsibility to "ensure" the directory exist, in the sense that it should always succeed as long as it exist, that's okay.

There's not much I can do. I guess.

I mean also given the random EPRM errors that Windows may produce, then, basically using this function may randomly fail, even when the directory exists, so that's okay too.

I've said what I could. You can close the issue if you want.