-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
"Actual" current directory and PowerShell current directory mismatch when using .NET BCL methods #3428
Comments
If I have process with multiple |
Possible solutions I can think of:
|
@antiufo Process-wide lock will not going to work, unless everyone agreed to use it and performance implication are not very good. Also, not every process host PowerShell just for sake of hosting PowerShell. Process may have its own work to do and unlikely be happy if you just change its working directory to dummy one. And as PS C:\Users\PetSerAl> function ConvertTo-FileSystemPath {
>> param($PSPath)
>> $PSProvider = $null
>> $Result = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($PSPath, [ref]$PSProvider, [ref]$null)
>> if($PSProvider.ImplementingType -eq [Microsoft.PowerShell.Commands.FileSystemProvider]) {
>> $Result
>> } else {
>> Write-Error 'Not filesystem provider'
>> }
>> }
PS C:\Users\PetSerAl> New-PSDrive Win FileSystem C:\Windows | Out-Null
PS C:\Users\PetSerAl> ConvertTo-FileSystemPath qwe
C:\Users\PetSerAl\qwe
PS C:\Users\PetSerAl> cd \
PS C:\> ConvertTo-FileSystemPath asd
C:\asd
PS C:\> ConvertTo-FileSystemPath Win:\zxc
C:\Windows\zxc
PS C:\> ConvertTo-FileSystemPath HKLM:\SOFTWARE
ConvertTo-FileSystemPath : Not filesystem provider
At line:1 char:1
+ ConvertTo-FileSystemPath HKLM:\SOFTWARE
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,ConvertTo-FileSystemPath |
This behavior is by design and it's been this way since version 1.0. We considered options like those proposed above but none of them were viewed as satisfactory for various reasons. Acquiring a global lock, then setting the process pwd, then calling a method is a lot of overhead when the majority of .NET APIs don't depend on pwd. Doing this also greatly complicates a host application's (e.g. exchange UI, VMM console, etc.) use of the current working directory since it can't depend on PWD anymore. The workaround for .NET APIs that take paths is to, as mentioned in another comment, resolve the path before passing It to the API which need not be onerous e.g. |
Too bad that there's no good solution, because the behavior is a perennial pain point. The Remedying that doesn't take much extra effort, fortunately: [io.file]::WriteAllText($PWD.ProviderPath + '/fileInCurrentDir.txt', 'foo') If you also want to guard against the current location being on a different provider's drive: [io.file]::WriteAllText((Get-Location -PSProvider FileSystem).ProviderPath + '/fileInCurrentDir.txt', 'foo') @PetSerAl's However, what this function does could be folded into the standard |
I think you've missed a use-case, and that's libraries that don't know anything about PS. For example, Terminal.Gui has at least three APIs (related, but still - FileDialog, OpenDialog, SaveDialog) that initialize the displayed folder from [system.environment]::currentdirectory. So I have to set it to what PS thinks the working directory is before initializing any of those APIs. |
When
cd
is used, the actual current directory of the process is not changed. This means that plain BCL methods still see the old current directory.This creates an inconsistency with other ways of executing commands:
$pwd
is honored$pwd
is honored$pwd
is not honoredSteps to reproduce
Expected behavior
C:\temp
containsexample1.txt
andexample2.txt
.Actual behavior
C:\temp
only containsexample1.txt
.example2.txt
is created inC:\Users\Andrea
.Environment data
Btw the same happens with PowerShell v6.0.0-alpha.17.
The text was updated successfully, but these errors were encountered: