node icon indicating copy to clipboard operation
node copied to clipboard

[v18.x] Wrong path returned by fs.readdir withFileTypes=true

Open H4ad opened this issue 10 months ago • 4 comments

Version

18.20.1

Platform

Linux h4ad 6.5.0-27-generic #28~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 15 10:51:06 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

fs/promise

What steps will reproduce the bug?

You can reproduce the issue by running the following commands:

$ mkdir fs-issue
$ cd fs-issue
$ touch issue.txt
$ node -e "require('fs/promises').readdir('.', { withFileTypes: true, encoding: 'utf-8', }).then(console.log)" 

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

The path should be ..

[
  Dirent {
    name: 'issue.txt',
    parentPath: '.',
    path: '.',
    [Symbol(type)]: 1
  }
]

What do you see instead?

The path is different:

[
  Dirent {
    name: 'issue.txt',
    parentPath: '.',
    path: 'issue.txt',
    [Symbol(type)]: 1
  }
]

This issue only happens on fs/promises, on readdirSync, we don't see this issue:

$ node --print "require('fs').readdirSync('.', { withFileTypes: true, encoding: 'utf-8', })"
[
  Dirent {
    name: 'issue.txt',
    parentPath: '.',
    path: '.',
    [Symbol(type)]: 1
  }
]

Also, if we include recursive, we don't see the issue:

$ node -e "require('fs/promises').readdir('.', { recursive:true, withFileTypes: true, encoding: 'utf-8', }).then(console.log)"
[
  Dirent {
    name: 'issue.txt',
    parentPath: '.',
    path: '.',
    [Symbol(type)]: 1
  }
]

Additional information

This bug was first introduced at 18.20.0, on 18.19.1 the behavior is correct. On latest version of node, 21.7.2 and 20.12.1, we don't see this issue.

This bug was found by @wraithgar at https://github.com/npm/cli/pull/7352#issuecomment-2045701032

H4ad avatar Apr 09 '24 21:04 H4ad