Node: fs.mkdir/mkdirSync hang with {recursive: true} if name is invalid

Created on 8 Jul 2019  路  5Comments  路  Source: nodejs/node

  • Version: 10.16.0, 11.14.0, 12.6.0
  • Platform: Windows 10
  • Subsystem: fs

If the name of the dir to be created is invalid, fs.mkdir never calls back and fs.mkdirSync blocks:

const fs = require('fs')

console.log("1")
fs.mkdir('invalid1:', {recursive: true}, (err) => {
  console.log("is not called")
  if (err) throw err
})

console.log("2")
fs.mkdirSync('invalid2:', {recursive: true})
console.log("is not reached")

BTW, the error message that is reported with {recursive: false} appears a bit strange:

Error: ENOENT: no such file or directory, mkdir 'invalid2:'

Wouldn't it be more convenient to use EPERM, EACCES or ENOTDIR?

confirmed-bug fs windows

All 5 comments

Colons on NTFS files systems on Windows denote file streams (source).

The error is the same for "invalid>", "invalid<" oder "invalid|".

Those are all invalid characters on NTFS, see here.

Not sure if Node.js should do validation for those (and take a performance hit on every platform). There are modules like this which you can use to validate yourself.

The hang itself should probably be investigated, thought.

Yes - I know this module. However, it is not cross plattform as far as I can tell - ':', '<', '>','|' may be valid on Linux (although not necessarily recommended, I guess). Yes, of course I could distinguish platforms. But I could as well rely on "fs.mkdir" to fail, if something is wrong.

The point is, Node should, if it fails to create the directory for whatever reason, give a reasonable error and, of course, not hang - with { recursive: true } or not.

This is similar to https://github.com/nodejs/node/issues/27198, which was fixed by https://github.com/nodejs/node/pull/27207.

For invalid filenames, uv_fs_mkdir returns UV_ENOENT, which confuses the fs.mkdir function. It constantly creates the parent directory then tries to create the invalid folder.

Made a libuv PR that makes it return UV_EINVAL for invalid filenames. This fixes this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vsemozhetbyt picture vsemozhetbyt  路  3Comments

cong88 picture cong88  路  3Comments

addaleax picture addaleax  路  3Comments

jmichae3 picture jmichae3  路  3Comments

Icemic picture Icemic  路  3Comments