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

Add Process.executable_path? to access current executable file path #3376

Merged

Conversation

ysbaddaden
Copy link
Contributor

@ysbaddaden ysbaddaden commented Oct 3, 2016

The implementation is platform specific, and falls back to resolving PROGRAM_NAME (argv[0]) relatively to the initial PWD, then searching the executable name within the initial PATH.

Use cases:

Based on work by @kostya, and whereami.
closes #1812

@ysbaddaden ysbaddaden force-pushed the std-process-executable-path branch from bda1da4 to 4ed03fc Compare October 3, 2016 08:32
@kostya
Copy link
Contributor

kostya commented Oct 3, 2016

on linux you return /proc/self/exe, this is symlink, i think need to readlink for it. because some times need real path, dir or exe.

@ysbaddaden
Copy link
Contributor Author

ysbaddaden commented Oct 3, 2016

@kostya I don't follow symlinks by default for all destinations. This is stated in the documentation. For every platform, the reported executable file may be a symlink.

I chose to because there is no need for that to open the current executable (eg: to read the DWARF sections); but I have no problem changing this to always call File.real_path(executable) to canonicalize symlinks. This will require to rescue errors in case the file doesn't exist anymore or can't be found.

@luislavena
Copy link
Contributor

I don't follow symlinks by default for all destinations. This is stated in the documentation. For every platform, the reported executable file may be a symlink.

I think that might be a bit inconsistent and will force users that want to use executalbe_path? to rely on File.real_path or platform-specific conditions, making end-user code a bit ugly.

There is also chances for newcomers to perform File.basename(Process.executable_path?) and ending just with exe as result (that will be pretty high considering lot of people do not read the docs and caveats, hehe)

One last point is that you lookup for that executable name in PATH but executables can be called directly without being in PATH.

@asterite
Copy link
Member

asterite commented Oct 3, 2016

I didn't look at the code yet, but I would just name it File.executable_path without the trailing ?. I know it can return nil, but we usually use that when we have two methods, one possibly returning nil and the other one raising.

@ysbaddaden
Copy link
Contributor Author

@luislavena good points. I'll change that.
@asterite ok.

@ysbaddaden ysbaddaden force-pushed the std-process-executable-path branch from 4ed03fc to 83a1d59 Compare October 3, 2016 19:55
@ysbaddaden
Copy link
Contributor Author

ysbaddaden commented Oct 3, 2016

@asterite I just pushed the changes. Process -> File, no more ? methods and I expand all symlinks and canonicalize the full path (thanks to realpath).

you lookup for that executable name in PATH but executables can be called directly without being in PATH

I only lookup into PATH only after checking that the name didn't contain any file separator ('' or / depending on platform), so it's a mere filename, and thus was found in PATH. On POSIX platforms (which are all we support now), even if I have a foo executable in my current directory, running foo won't invoke ./foo, unless I have . in my PATH that is.

@RX14
Copy link
Member

RX14 commented Oct 3, 2016

I like Process.executable_path more, because we're talking about the current process, instead of a generic file operation.

@asterite
Copy link
Member

asterite commented Oct 5, 2016

👍 from me. I'd also use Process.executable_path, feels better than File.executable_path. After that I'd just merge it. Thank you @ysbaddaden !

PATH_DELIMITER = {% if flag?(:windows) %} ';' {% else %} ':' {% end %}

# :nodoc:
INITIAL_PATH = ENV["PATH"]
Copy link
Member

Choose a reason for hiding this comment

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

Is PATH always present? Maybe, just in case, we can use ENV["PATH"]? (same below) and return nil if it's not set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good catch!

@ysbaddaden
Copy link
Contributor Author

I'll fix that today.

The implementation is platform specific, and falls back to resolving
PROGRAM_NAME (`argv[0]`) relatively to the initial PWD, then
searching the executable name within the initial PATH.

Based on work by @kostya

closes crystal-lang#1812
@ysbaddaden ysbaddaden force-pushed the std-process-executable-path branch from 83a1d59 to ab59fc9 Compare October 7, 2016 09:07
@ysbaddaden
Copy link
Contributor Author

@asterite done.

@asterite
Copy link
Member

asterite commented Oct 7, 2016

@ysbaddaden Thank you!!! ❤️

@asterite asterite merged commit c4caa42 into crystal-lang:master Oct 7, 2016
@ysbaddaden ysbaddaden deleted the std-process-executable-path branch October 7, 2016 12:20
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.

question, current binary dir
5 participants