Skip to content

Commit

Permalink
correctly readlink() when in multiply-nested symlink dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 28, 2024
1 parent 7654654 commit 7160c5f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.9

- Add `path` for compatibility with Node v20 Dirent
- Correctly readlink() in multiply nested symlink directories

## 1.8

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ export abstract class PathBase implements Dirent {
/* c8 ignore stop */
try {
const read = await this.#fs.promises.readlink(this.fullpath())
const linkTarget = this.parent.resolve(read)
const linkTarget = (await this.parent.realpath())?.resolve(read)
if (linkTarget) {
return (this.#linkTarget = linkTarget)
}
Expand Down Expand Up @@ -889,7 +889,7 @@ export abstract class PathBase implements Dirent {
/* c8 ignore stop */
try {
const read = this.#fs.readlinkSync(this.fullpath())
const linkTarget = this.parent.resolve(read)
const linkTarget = (this.parent.realpathSync())?.resolve(read)
if (linkTarget) {
return (this.#linkTarget = linkTarget)
}
Expand Down
25 changes: 25 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1617,3 +1617,28 @@ t.test('chdir', async t => {
expect
)
})

t.test('link targets in multiply nested symlinks', async t => {
const cwd = t.testdir({
dir_baz: {
bar: t.fixture('symlink', '../dir_bar'),
},
dir_bar: {
foo: t.fixture('symlink', '../dir_foo'),
},
dir_foo: {
'foo.txt': 'hello'
}
})
t.test('async', async t => {
const p = new PathScurry(cwd)
const link = p.cwd.resolve('./dir_baz/bar/foo')
t.ok(await (await link.readlink())?.lstat(), 'found the link target')
})
t.test('sync', t => {
const p = new PathScurry(cwd)
const link = p.cwd.resolve('./dir_baz/bar/foo')
t.ok(link.readlinkSync()?.lstatSync(), 'found the link target')
t.end()
})
})

0 comments on commit 7160c5f

Please sign in to comment.