Document platform-specific behavior of current_exe, including that Linux can add " (deleted)" - #150824
Document platform-specific behavior of current_exe, including that Linux can add " (deleted)"#150824sourcefrog wants to merge 1 commit into
current_exe, including that Linux can add " (deleted)"#150824Conversation
|
r? libs-api |
There was a problem hiding this comment.
The convention is to put such things under a platform-specific behavior section + link to elaboration what that means.
Then we do not consider it a new API guarantee.
This is already under a "platform-specific behavior" heading though? |
Beyond the scope of this PR, I do think some users will want to know what the behavior is on some particular platform: does it search $PATH, does it read The API docs make minimal commitments with text like "some platforms may" but perhaps we should upgrade this to something like "Linux will xyz but this is subject to change?" Anyhow I still think this is a step forward. |
Ah yes, then just add a link to the "subject to change" section.
Yes, that's the stuff that can typically be put in that section. |
current_exe that Linux can add " (deleted)"current_exe, including that Linux can add " (deleted)"
Ah, thanks. I've added that link, and since it says
I've actually extended this to document all the currently implemented per-OS behavior by reading the current code. |
|
See also #69343 |
|
@cuviper would you mind looking at this too while you're thinking about |
| /// been deleted, Linux returns the previous path with the string `" (deleted)"` | ||
| /// appended. | ||
| /// | ||
| /// On Apple operating systems, this function calls `_NSGetExecutablePath`. |
There was a problem hiding this comment.
I don't think it makes sense to exhaustively list how this is implemented on all platforms. That doesn't say much unless you are an expert in low-level OS APIs.
Instead, under "Platform-specific behavior", couldn't we just say something like:
If the executable is removed while it is running, the returned path might be invalid.
I think that fits in well with the current documentation structure and tone.
There was a problem hiding this comment.
I don't think it makes sense to exhaustively list how this is implemented on all platforms. That doesn't say much unless you are an expert in low-level OS APIs.
Instead, under "Platform-specific behavior", couldn't we just say something like:
If the executable is removed while it is running, the returned path might be invalid.
I think that fits in well with the current documentation structure and tone.
So on the one hand, it is quite of a lot of text to cover all these obscure platforms, and it is in danger of going out of date. And you're quite right that it still doesn't directly tell you what the behavior is, it only gives you a pointer to look it up.
On the other hand, I think people could reasonably want to know how this function handles edge-but-not-niche cases like the file being deleted or renamed while running, or it being hard linked. I came to this from fixing a user-visible bug in Zed, and other threads described how there have been historical security vulnerabilities connected to those edge cases.
My thought was that if we list the underlying OS calls, then people at least have a handle to find out how it specifically behaves on various platforms. Without it, they have to dig through the source code to find out what it calls, which is of course possible, I just did it myself... but that logic taken to an extreme means no API docs are needed at all.
I thought it was reasonable to add this because of precedents like https://doc.rust-lang.org/std/fs/struct.Permissions.html#method.readonly and https://doc.rust-lang.org/std/fs/fn.symlink_metadata.html which tell you in detail what the semantics are on Windows and Unix including which particular underlying APIs that they use, for example:
This function currently corresponds to the lstat function on Unix and the GetFileInformationByHandle function on Windows.
I'm open to rolling this back to just say
On Linux, the string
" (deleted)"is added if the file has been deleted. Note that this may change in future.
I don't see a lot of point in just saying the result "might be invalid", as that seems to leave the user with nothing to do but read the source.
Thanks for reading and replying!
|
Reminder, once the PR becomes ready for a review, use |
93dd428 to
a595c1c
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates the std::env::current_exe documentation to clarify platform-specific behavior when the running executable is removed on Linux.
Changes:
- Document Linux behavior when the executable is deleted while running.
- Add a reference-style rustdoc link to the “platform-specific behavior” documentation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
a595c1c to
81f5a99
Compare
This comment has been minimized.
This comment has been minimized.
81f5a99 to
1ee6484
Compare
This comment has been minimized.
This comment has been minimized.
Link to warning that platform-specific behavior is subject to change
1ee6484 to
26a1545
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
View all comments
This documents one other OS-specific behavior that might be surprising to some users. The underlying behavior is documented in for example https://man7.org/linux/man-pages/man5/proc_pid_exe.5.html.
Rust std docs can't and shouldn't try to cover every single OS quirk but this seems reasonably in line with telling people how to use this function, and with the text above about what happens when the exe has been renamed.
This came up in the context of zed-industries/zed#46367
Fixes #69343 (by documenting the behavior)