Skip to content

fix(ensureSymlink): resolve relative srcpath correctly when symlink exists#1064

Merged
RyanZim merged 1 commit intojprichardson:masterfrom
abhu85:fix-symlink-relative-path-1038
Feb 26, 2026
Merged

fix(ensureSymlink): resolve relative srcpath correctly when symlink exists#1064
RyanZim merged 1 commit intojprichardson:masterfrom
abhu85:fix-symlink-relative-path-1038

Conversation

@abhu85
Copy link
Contributor

@abhu85 abhu85 commented Feb 23, 2026

Summary

Fixes #1038

When ensureSymlink is called with a relative srcpath and the destination symlink already exists, the code was incorrectly resolving the srcpath relative to cwd instead of relative to the dstpath directory.

This caused ENOENT errors when calling ensureSymlink a second time with the same relative path:

// First call works
await ensureSymlink('../target', './link-dir/link', 'dir')

// Second call fails with ENOENT (should succeed)
await ensureSymlink('../target', './link-dir/link', 'dir')

Root Cause

In symlink.js lines 22-28 (async) and 48-52 (sync), when checking if an existing symlink already points to the correct target, fs.stat(srcpath) was called directly. For relative paths, this evaluates from cwd, not from the symlink's directory.

Fix

When srcpath is relative, first try to resolve it relative to dstpath's directory (standard symlink behavior), falling back to cwd if that doesn't exist. This mirrors the logic already used in symlinkPaths().

Changes

Test Plan

🤖 Generated with Claude Code

…xists

When ensureSymlink is called with a relative srcpath and the destination
symlink already exists, the code was incorrectly resolving the srcpath
relative to cwd instead of relative to the dstpath directory.

This caused ENOENT errors when calling ensureSymlink a second time with
the same relative path, because fs.stat() evaluated the relative path
from the wrong location.

The fix checks if srcpath is relative, and if so, first tries to resolve
it relative to dstpath's directory (standard symlink behavior), falling
back to cwd if that doesn't exist.

Fixes jprichardson#1038

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Copy link
Collaborator

@RyanZim RyanZim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified added tests as failing before changes.

LGTM

@RyanZim RyanZim merged commit 3e65fbe into jprichardson:master Feb 26, 2026
18 checks passed
@RyanZim
Copy link
Collaborator

RyanZim commented Feb 26, 2026

Probably won't get this released until next week, though.

@abhu85 abhu85 deleted the fix-symlink-relative-path-1038 branch February 27, 2026 08:38
@RyanZim
Copy link
Collaborator

RyanZim commented Mar 3, 2026

Released in [email protected] 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createSymlink fails when ensuring a symbolic link with a relative path if the link already exists

2 participants