streamich/memfs

`readdir` with `withFileTypes:true` incorrectly includes file name in `path`

ziacik opened this issue ยท 2 comments

The test here

{ mode: 33206, name: 'af1', path: '/z/af1' },

is wrong - node.js actually doesn't include the file name in the path.

So the expectation should be
{ mode: 33206, name: 'af1', path: '/z' },

I just ran into the same issue.

Looks like the Dirent.path property is being set to the full path, rather than the parent path:

dirent.path = link.getPath();

Note how Link.getPath includes the name:

memfs/src/node.ts

Lines 408 to 414 in 6f4e72e

getPath(): string {
return this.steps.join(SEP);
}
getName(): string {
return this.steps[this.steps.length - 1];
}

I think it'd make sense to add a getParentPath() method to Link along the lines of:

getParentPath(): string {
  return this.steps.slice(0, -1).join(SEP);
}

so that the path assignment in Dirent could be updated to:

dirent.path = link.getParentPath();

PRs are welcome ๐Ÿ™‚