Skip to content

fix(ln): replace existing broken symlinks with -f - #1281

Open
cpruijsen wants to merge 1 commit into
shelljs:mainfrom
cpruijsen:fix/issue-1039-0fdade50
Open

cpruijsen wants to merge 1 commit into
shelljs:mainfrom
cpruijsen:fix/issue-1039-0fdade50

Conversation

@cpruijsen

Copy link
Copy Markdown

Fixes #1039

ln -sf and ln -f now replace a destination that is a broken symlink. Before, they failed with ln: EEXIST. Real ln -sf replaces it.

src/ln.js checked dest with fs.existsSync, which follows links, so a broken symlink looked absent. The -f unlink was skipped and fs.symlinkSync threw EEXIST. The check now uses common.statNoFollowLinks (lstat). Regular files, directories and valid symlinks are unaffected, because fs.existsSync already returned true for them.

Without -f, a broken-symlink destination now gives Destination file exists, the same as any other existing destination, instead of the raw EEXIST message.

I used try/catch on statNoFollowLinks because rm.js and cp.js use the same pattern to check existence without following links. fs.existsSync(dest) || <lstat>.isSymbolicLink() also works. I can change to that if you prefer it.

The new test -sf option on existing broken symlink in test/ln.js uses the test/resources/badlink fixture. It fails on master with the EEXIST error from the issue and passes with this change.

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.

"shell.ln" still throws error string "ln: EEXIST" even when running with "-f"

1 participant