Comments (7)
ensureDir
is basically a wrapper around fs.mkdir()
with the recursive
option set:
node-fs-extra/lib/mkdirs/make-dir.js
Lines 11 to 18 in cc7b3b2
The Node.js docs state:
Calling
fs.mkdir()
whenpath
is a directory that exists results in an error only whenrecursive
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.
from node-fs-extra.
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:
- A root directory always exists if the drive exists.
- If the user said
ensureDir('D:\\')
, and the driveD
exists, it means that everything is fine! We don't need to care if the current user has permission to write in its root directory. Maybe the user wanted to just ensure the drive itself exists, and doesn't care about being about being able to write to its root directory?
I interpreted the idea of "ensuring a directory exists" to mean:
- Ensure it's even possible that it exists (that the drive exists)
- Create it if needed.
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?)
from node-fs-extra.
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)
}
}
from node-fs-extra.
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.
from node-fs-extra.
Here's the way I see it:
- The goal of a method called
mkdir
is to make a new directory. It doesn't necessary imply what should happen in the directory already exists, or if there are permission errors in creating it, even in the case where it already exists. - The goal of a method called
ensureDir
is to ensure the directory exists. This means that if the directory already exists, it has done its job, and it shouldn't matter if the user has (or would have) permissions to that directory. If it doesn't exist, then obviously it's a problem if it can't be created.
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.
from node-fs-extra.
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.
from node-fs-extra.
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.
from node-fs-extra.
Related Issues (20)
- support conditional exports HOT 1
- Error while writing large JSON. `RangeError: Invalid string length.` HOT 1
- Feature request: emptyDir() - exclude files/folders from being deleted HOT 1
- Your minimist version is out of date needs to be updated to 1.2.6 or later HOT 1
- Can't import json methods using default import name in ESM HOT 1
- Copying a Link from One File to Another Link to the Same File errors out in `isSrcSubdir` HOT 5
- [Proposal] Refactor the entire code base to Promise-based HOT 3
- support `file://` url string path HOT 5
- Bug in onSymlink HOT 1
- Issue upgrading to 11.2.0 HOT 2
- fs.rmSync is not a function HOT 2
- fs.copySync doesn't throw any err while it is failing HOT 1
- Cannot find module 'fs-extra/esm' or its corresponding type declarations. HOT 1
- TypeError is not a function for file write functions HOT 1
- Module not found: Can't resolve 'fs' HOT 2
- Feature request: isEmptyDir() HOT 6
- `createSymlink` fails when ensuring a symbolic link with a relative path if the link already exists HOT 3
- Wrong documents about `copy` and `copySync` HOT 2
- Getting docx file in corrupted format when created from xmlstring HOT 1
- module not found: fs/promises HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-fs-extra.