-
Notifications
You must be signed in to change notification settings - Fork 776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ensureDir
shouldn't fail when a directory already exists, but mkdir
throws a write permission error for it
#1012
Comments
node-fs-extra/lib/mkdirs/make-dir.js Lines 11 to 18 in cc7b3b2
The Node.js docs state:
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 |
The issue I was describing is that 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?) |
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)
}
} |
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. |
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 Arbitrarily declaring that |
ensureDir
fails with a permission error when given a root directoryensureDir
shouldn't fail when a directory already exists, but mkdir
throws a write permission error for it
|
I'm not sure what this means. You're basically just asserting that I mean, if you insist that calling something 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. |
v18.17.0
fs-extra
version:11.1.1
Reproduction:
Result:
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:
The text was updated successfully, but these errors were encountered: