-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 Environment.ProcessPath #42768
Add Environment.ProcessPath #42768
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @eiriktsarpalis, @jeffhandley |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this usable most places Process.GetCurrentProcess().MainModule.FileName;
is currently used? If yes, can we add an analyzer for that? Looking at source.dot.net, that pattern is quite common.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetProcessPath.cs
Outdated
Show resolved
Hide resolved
Yes, it should be.
Sounds good to me. Is the best way to do it to copy&paste UseEnvironmentProcessId analyzer and fix up the few places in it? It feels like a lot of code duplication to me, but it seems to be the common pattern in the analyzer repo. |
It's ok for a single analyzer/fixer to support multiple diagnostic IDs. Given that both of these are detecting patterns on Process usage and recommending Environment usage instead, I think it would make sense for the same code to handle both (assuming there is indeed a lot of overlap), albeit triggering different diagnostic IDs. @mavasani can provide alternative guidance if he thinks another approach is better. |
|
@CoffeeFlux Do you have any thoughts on the invariants that this API should maintain and how to implement it? |
src/libraries/System.Private.CoreLib/src/System/Environment.Unix.cs
Outdated
Show resolved
Hide resolved
Various thoughts on issues raised above: We should definitely be calling Returning null for wasm also seems straightforward, though of some note here is that this only holds true for browser and not necessarily standalone wasm runtimes, should we ever want to support those.
This one seems like a no-brainer to me. The returned value should not change over the course of program execution, so it should return the original executable path. I don't see any value in keeping it up to date with filesystem or cwd changes. The easiest way to implement this is to cache the value after the first call, and we should do that. The other questions are more interesting.
I personally think it should return something that isn't cwd-sensitive if at all possible, to the point that I'd consider checking args[0] before using cwd-sensitive detection. It might be hard to 100% guarantee this though, so I would consider it best-effort.
Once again, returning the original executable seems desirable if possible, and I think this is also the simplest implementation for Windows, OSX, and Linux? I once again lean towards saying this is best-effort rather than a 100% guarantee. If we decide we want to make that a hard guarantee, I don't see any good alternative to putting it in the runtime and fetching it once during startup. Again, I personally don't think we have to absolutely guarantee that, but if we want to this is the only real option. Another complication is Android, which does not have an 'executable' in the traditional sense when launching apps normally; everything forks from Zygote and loads the relevant APK. This means Another consideration for
This isn't an issue when trying to open it, because |
Do you mean
Agree. I should have said browser, the implementation file is
Agree. This API is intentionally policy free and returns the executable name from the OS point of view. There is a second API proposal (#41341) to return the "directory that user wanted" that is computed via some to be determined policy.
So what I am hearing:
Sounds reasonable? I can behind this. |
Sorry, what I meant with the glib and musl comment is whether either of them is aware this can happen and handles it inside |
Agreed, it could work with cache as well. So if we do not get the perfect (real and absolute) path on some platform in some edge-case scenario against the first call, we could still cache the value and use it for the lifetime of application (best effort was made just once in lifetime of app). |
src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs
Outdated
Show resolved
Hide resolved
@am11 This should be ready for review if you would like to take a look. |
Will go through it tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jkotas for this API. I have tested the changes on illumos. I think it is quite fair to return null for non-absolute or other forms of unresolved cases as done here, instead of prepending cwd in fallback path (which is not clear how to even reproduce with .NET hosting). 👍
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetProcessPath.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Environment.Browser.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs
Outdated
Show resolved
Hide resolved
…tProcessPath.cs Co-authored-by: Stephen Toub <stoub@microsoft.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly just some random nits. LGTM, thanks!
#else | ||
|
||
#ifdef __linux__ | ||
#define symlinkEntrypointExecutable "/proc/self/exe" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think a normal variable is more appropriate here than a mid-function define, unless there's a good reason for this to be using the preprocessor that I'm missing. Codegen should be identical.
src/libraries/System.Private.CoreLib/src/System/Environment.Browser.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Ryan Lucia <ryan@luciaonline.net>
Co-authored-by: Ryan Lucia <ryan@luciaonline.net>
Co-authored-by: Ryan Lucia <ryan@luciaonline.net>
Opened #42948 on the analyzer. Thank you all for the discussion about the design and codereview feedback. |
Fixes #40862