Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.existsSync(brokenSymlink) returns false #14025

Closed
fuweichin opened this issue Jul 1, 2017 · 7 comments
Closed

fs.existsSync(brokenSymlink) returns false #14025

fuweichin opened this issue Jul 1, 2017 · 7 comments
Labels
fs Issues and PRs related to the fs subsystem / file system.

Comments

@fuweichin
Copy link

  • Version: node/6.10.2; npm/3.10.10
  • Platform: Windows 10 64-bit
  • Subsystem: fs (File System)

fs.existsSync(linkName) returns false when linkName is a broken symbolic link path(whose target does not exist).

Is the result by design or like a bug?

@mscdex mscdex added the fs Issues and PRs related to the fs subsystem / file system. label Jul 1, 2017
@mscdex
Copy link
Contributor

mscdex commented Jul 1, 2017

Sounds logical to me.

@bnoordhuis
Copy link
Member

Yes, it's expected; symlinks are supposed to be transparent. I'll close the issue.

@kingces95
Copy link

Er.. so how do I test for the existence of a broken symlink?

@addaleax
Copy link
Member

@kingces95 You can use fs.lstat and fs.stat/fs.exists on a symlink; if the former succeeds and the latter fails, you’ve got a broken symlink.

@tomasreimers
Copy link

Hi, sorry to resurrect this thread - I'm finding myself in need of something similar: I am trying to move a file to NEW_PATH, and if NEW_PATH already exists, the operation will fail.

The naive solution is to do something like:

if (fs.existsSync(NEW_PATH)) fs.unlinkSync(NEW_PATH);
fs.moveSync(OLD_PATH, NEW_PATH);

However, this will break if NEW_PATH is a broken symlink (b/c exists will return false, and move will fail), which doesn't feel super intuitive and potentially like a quick way for library authors to shoot themselves in the foot (as not everyone will be thinking about the case where there are broken symlinks).

Is there a function that is better suited than existsSync()? Or is there a more straightforward way than putting a try-catch around lstat(NEW_PATH)? Or is the belief that it is the correct behavior for libraries to fail here?

If those aren't the case, thoughts on adding a flag to existsSync options to check for symlinks? Or possibly a pathExists() method?

@addaleax
Copy link
Member

addaleax commented Jan 4, 2021

Is there a function that is better suited than existsSync()?

You could not use existsSync and unconditionally use unlinkSync() (and then ignore ENOENT errors from that).

Or is there a more straightforward way than putting a try-catch around lstat(NEW_PATH)?

You might want to be more specific about the error you’re seeing, because fs.renameSync(OLD_PATH, NEW_PATH); just works for me.

If those aren't the case, thoughts on adding a flag to existsSync options to check for symlinks? Or possibly a pathExists() method?

I’d be against that – you already came up with good alternatives :)

@tomasreimers
Copy link

Appreciate it! And unlinking unconditionally is a good idea :)

I believe I was doing a copy, not a move--my mistake--, and that broke b/c it was trying to dereference the path.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

No branches or pull requests

6 participants