-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Cargo Scripts with -Zscript
should override current_exe
#12870
Comments
The main issue with this is I don't think there is a way to do it in a cross-platform way. For unix-like systems, we have |
There could be, there's just not std function for it atm. However, |
We have |
I like the idea, it really seems like if you're directly running the binary with |
sh: Relative shell script includes with realpath on FreeBSD, 2 are potentially related. |
Problem
std::env::args
andstd::env::current_exe
return potentially confusing results when running cargo scripts with the-Zscript
unstable feature. While ostensibly correct, returning the actual built executable path is generally less useful to scripts than returning the path to the cargo script file, and leads to antipatterns like usingcurrent_dir().join(SCRIPT_NAME)
to find the script file. Running the executable from the containing directory and havingcurrent_dir()
not be a prefix ofcurrent_exe()
adds another layer of complexity.Proposed Solution
It seems there are ~3 possible ways to go about this:
std::env::current_exe
has a note that it may return the path of a symbolic link instead of a target of a symbolic link, or other platform specific behavior. Treating the script file as a symbolic link-equivalent whose path is returned instead of the actual executable using this escape hatch is the route I'd personally like best.Notes
Related to #3502 and #12207.
The text was updated successfully, but these errors were encountered: