Path methods — symlinks improvement

We have a method exists() for Path in std, its code:

 pub fn exists(&self) -> bool {
        fs::metadata(self).is_ok()
 }

But symlinks use another metadata getter:

    std::fs::symlink_metadata()

So we can't check whether symlink exists because exists() checks symlink origin.

Also, there is no method called is_symlink()

I think these methods are simple and useful, why aren't they in std?

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=54c3278f4b89a967f20074ee499faf6c

PR: Path methods — symlinks improvement by maxwase · Pull Request #85747 · rust-lang/rust · GitHub

Tracking issue: Tracking Issue for is_symlink · Issue #85748 · rust-lang/rust · GitHub

2 Likes

Yes, but Path doesn't have this method, though is_dir() and is_file() exist

I agree that Path::is_symlink would be quite useful.

I agree with that, it's been a bit confusing at first so I think adding an .is_symlink() method would be quite nice - unless there are specific reasons this does not already exist?

I'd like to see .is_symlink() myself (though I don't have Rust code that cares right now, I do have it in other languages).

IIRC, Path::exists() uses stat, so with this, it is possible that !broken_symlink.exists() && broken_symlink.is_symlink() is true. So it might be a bit confusing without reading the docs (which does mention this issue at least). Though, IMO, using stat was a mistake, I don't think that can be fixed anymore.

See also this thread:

While exists might not be able to change, try_exists is still experimental so it can (at least potentially). I personally would be interested in changing the behaviour but admittedly the presence of exists does make it a harder sell then it might otherwise be.

Deprecating exists is possible once try_exists is stable. :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.