Skip to content

Inconsistent current_exe behavior for symlinks on different platforms #43617

Closed
@jeffweiss

Description

@jeffweiss

Background

I was attempting to use std::env::current_exe() to figure out how my program was invoked so I could respond with convenience code paths (analogous to how gpgv is basically a shortcut/alias for gpg --verify) when I noticed inconsistent behavior between OS X and Linux when the program was invoked via a symbolic link.

Steps to Reproduce

test.rs

fn main() {
    println!("{:?}", std::env::current_exe());
}
$ rustc test.rs
$ ln test one
$ ln -s test two
$ ./test
$ ./one
$ ./two

For convenience I have bundled these in a repo: jeffweiss/rust-current_exe

Rust Version

rustc 1.19.0 (0ade339 2017-07-17)

Expected Behavior

On all platforms, I would expect to get:

Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/test")
Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/one")
Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/two")

Actual Behavior

On Mac OS X, I get:

Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/test")
Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/one")
Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/two")

On Linux, I get:

Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/test")
Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/one")
Ok("/home/jweiss/devel/jeffweiss/rust-current_exe/test")

Related Documentation

The documentation for std::env::current_exe implies that if you run current_exe from the symlink, you'll get the name of the symlink, not the dereferenced target of the symlink; however, the documentation's example does not actually include a symbolic link (ln -s), but rather a hard link (ln).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-mediumMedium priorityT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions