Node: fs.realpathSync('') returns CWD

Created on 29 Sep 2020  路  3Comments  路  Source: nodejs/node

$ node -v
v12.18.4

What steps will reproduce the bug?

$ cd /tmp
$ node -e "console.log(fs.realpathSync(''))"
/tmp

What is the expected behavior?

$ node -e "fs.realpath.native('', (e) => void console.log(e))"
[Error: ENOENT: no such file or directory, realpath ''] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'realpath',
  path: ''
}
fs

Most helpful comment

That output kind of makes sense, knowing what I know about how fs.realpath() is implemented, but it behaves differently on Windows so we should make a judgment call on what we think is the appropriate behavior.

fs.promises.realpath('') returns a rejection with ENOENT.

That's because it's a wrapper around fs.realpath.native rather than fs.realpath. No idea why.

All 3 comments

For more context:

  • fs.promises.realpath('') returns a rejection with ENOENT.
  • fs.realpath('', (...args) => console.log(args)) passes null, CWD to the callback.
  • fs.realpathSync('') returns CWD.

That output kind of makes sense, knowing what I know about how fs.realpath() is implemented, but it behaves differently on Windows so we should make a judgment call on what we think is the appropriate behavior.

fs.promises.realpath('') returns a rejection with ENOENT.

That's because it's a wrapper around fs.realpath.native rather than fs.realpath. No idea why.

here is some more on this:

> fs.realpath(Buffer.from([]), (e, p) => console.log([e && e.path, p]))
undefined
> [ null, '/tmp' ]

> fs.realpath.native(Buffer.from([]), (e, p) => console.log([e && e.path, p]))
undefined
> [ '', undefined ]

> fs.realpath(Buffer.from([1]), (e, p) => console.log([e && e.path, p]))
undefined
> [ '/tmp/\u0001', undefined ]

> fs.realpath.native(Buffer.from([1]), (e, p) => console.log([e && e.path, p]))
undefined
> [ '\u0001', undefined ]
Was this page helpful?
0 / 5 - 0 ratings