Use the same temporary home directory when 'HOME' env variable is not set#16263
Use the same temporary home directory when 'HOME' env variable is not set#16263adityapatwardhan merged 4 commits intoPowerShell:masterfrom
Conversation
| return _tempDirectory; | ||
| // Directory creation may fail if the account doesn't have filesystem permission such as some service accounts. | ||
| // Return an empty string in this case so the process working directory will be used. | ||
| s_tempHome = string.Empty; |
There was a problem hiding this comment.
Put temp files in CWD could annoying users. Maybe try CWD + tempHomeFolderName?
Should we thing about right permission mask if we fallback?
There was a problem hiding this comment.
This is the same behavior we have today. I don't know what is the right thing to do when the user account has no file system permission, so keep the existing behavior.
There was a problem hiding this comment.
As for permission mask, yes, we should consider that. I have updated the code to use one temp home folder for each user account, instead of having all user accounts sharing the same one -- imagining multiple service accounts available on the same machine.
This is because the first user that creates the folder will make the folder non-writable by other non-root users by default. Also, it makes more sense for different users have different temp home folders.
| if (!Directory.Exists(systemResourceRoot)) | ||
| { | ||
| configSystemPath = Platform.GetFolderPath(Environment.SpecialFolder.System); | ||
| configSystemPath = Environment.GetFolderPath(Environment.SpecialFolder.System); |
There was a problem hiding this comment.
If we change this should we keep consistency through all code base? I found 6 SpecialFolder.System and 6 SpecialFolder.System and 9 usings of Platform.GetFolderPath() (not all code compiled).
If we can not rid of Platform.GetFolderPath() maybe use Environment.GetFolderPath() in Platform.GetFolderPath() ?
There was a problem hiding this comment.
If you go up from this call site, you will find this call site is in the else block of the if (Platform.IsLinux || Platform.IsMacOS), meaning that this code is for windows only. So we can use Environment.GetFolderPath instead.
If we can not rid of Platform.GetFolderPath() maybe use Environment.GetFolderPath() in Platform.GetFolderPath()
We already uses Environment.GetFolderPath() within that method for Windows code path.
There was a problem hiding this comment.
What I mean is that the code is unnecessarily convoluted, although all the logic could have been concentrated in the Platform.GetFolderPath()
There was a problem hiding this comment.
What code is unnecessarily convoluted? My plan is to get rid of Platform.GetFolderPath() altogether. Now the blocking piece is the s_debugPreferenceCachePath cache file, as soon as that dependency is gone, we can remove Platform.GetFolderPath() (the MyDocuments dependency is easy to tackle with).
There was a problem hiding this comment.
s_runspaceDebugPreference
Do you mean s_debugPreferenceCachePath? Is there plan to remove the dependency?
What code is unnecessarily convoluted? My plan is to get rid of altogether.
I see you point. But since we can not rid of the method my preference would be to put all logic to the method so that we can explicitly see why we can not remove the method and use .Net API directly. It is not a request for the PR :-)
There was a problem hiding this comment.
Yes, s_debugPreferenceCachePath. My comment above was corrected.
Given that the current path for that cache file is obviously wrong, I think we will remove that dependency on Platform.GetFolderPath() when fixing that. No plan to submit a PR yet, but should be simple changes.
After that, the only dependency on Platform.GetFolderPath() is just MyDocuments, and it'd be easy to get rid of Platform.GetFolderPath().
| if (envHome is not null) | ||
| { | ||
| return; | ||
| return envHome; |
There was a problem hiding this comment.
Hmm, if user change the HOME env variable we return new value. Should we be stable and return s_tempHome always?
I think changing HOME on the fly can destroy current session (not only PowerShell but OS too).
There was a problem hiding this comment.
That is the existing behavior. If you change HOME env variable and create a new Runspace, you will find it uses the new HOME value for that Runspace. Personally, I don't think this is practically a problem. And it could potentially be a breaking change if you change that behavior.
There was a problem hiding this comment.
I always thought that .config and .cache were always created per session
There was a problem hiding this comment.
They are not. All sessions share the same .config and .cache folders. .cache folder contains the module analysis cache file and the startup profile data, and those are supposed to be used across sessions.
There was a problem hiding this comment.
I see the code:
string xdgConfigHomeDefault = Path.Combine(envHome, ".config", "powershell");
string xdgDataHomeDefault = Path.Combine(envHome, ".local", "share", "powershell");
string xdgModuleDefault = Path.Combine(xdgDataHomeDefault, "Modules");
string xdgCacheDefault = Path.Combine(envHome, ".cache", "powershell");Here envHome is re-evaluated every time.
There was a problem hiding this comment.
SelectProductNameForDirectory is used only at startup and new Runspace creation (for module path).
Again, this PR doesn't change the current behavior when "HOME" value gets changed during a session.
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
@adityapatwardhan Can you please review this PR? Thanks! |
|
🎉 Handy links: |
PR Summary
Fix #15299
Use the same temporary home directory for a user account when the environment variable 'HOME' is not set for the user.
The previous solution used for unset
HOMEenvironment variable was to create a temporary folder and delete if when the Runspace is disposed. However, whenpwshexits,Runspace.Disposedoesn't get called -- the whole process is being torn down, so no much point to dispose the Runspace anyways. Therefore, every execution ofpwshwill leave a temporary folder behind.This PR updates
pwshto create and reuse a single temporary home directory for every user account. So executions ofpwshby a specific user will only have one temporary home folder created.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.