Start-Process: Correct Unspecified WorkingDirectory Resolution#11946
Start-Process: Correct Unspecified WorkingDirectory Resolution#11946daxian-dbw merged 3 commits intoPowerShell:masterfrom
Conversation
|
Although it might be a grey area, this is a breaking change as it changes how the arguments passed to |
|
@PowerShell/powershell-committee reviewed this, we propose that instead of always treating it as a literal, we would check if the path exists as a literal use it, otherwise fallback to the current behavior |
|
How? Working Directory by definition cannot be a useful wildcarded path, which can usually be assumed to exist. If you have an incomplete path, you can tab complete the rest. If you really need wildcards for any of these, you have Redirection paths typically won't exist for most use cases (again, no use for wildcards if the path doesn't exist), but as I understand it the cmdlet isn't capable of writing to multiple paths anyway, can it? |
|
@vexx32 Agreed. In fact, when I force the command to match multiple matches for output redirection, the command says: @SteveL-MSFT Certainly can't say that makes a lot of sense to me, but I can adjust the code if that's the verdict. Should the documentation be updated to say it "Sort Of" supports wildcards? Thoughts on redirection parameters as @vexx32 mentions? |
I agree that in most use cases the redirection files for standard output/error don't exist beforehand. But
One can create two directories named |
|
@daxian-dbw Based on your examples I can conclude that we should first make wildcard check and then literal. |
|
@iSazonov that's a valid point and would seem to be less breaking as any existing usage expecting wildcard matching would continue to work |
cbcf829 to
9aa2a13
Compare
- Modified Start-Process such that an unspecified working directory is correctly treated as literal when being resolved. - Added test for running Start-Process within a directory with with wildcard characters.
9aa2a13 to
ccfde00
Compare
|
@iSazonov @SteveL-MSFT @daxian-dbw @vexx32 It appears that PathUtils.ResolvePath() is not positioned well to do the sort of trial-and-error that's discussed. Instead of biting off that task right now, what I've done adjusted this PR's scope to address the originally reported behavior that was causing Start-Process to fail when WorkingDirectory is not specified but the current directory is set to a path that contains wildcard-type characters. (e.g. Set-Location 'C:\Path[]'; Start-Process cmd.exe was failing). I believe this change is non-breaking and should be non-controversial. |
|
I think there are additional problems here and definitely some holes in our testing. As the following shows: Describe "Should be able to set location to name with wild card" {
BeforeAll {
$testCases = @{ Name = 'foo[' },@{ Name = 'foo]' },@{ Name = 'foo[]' }, @{ Name = 'foo?' }, @{ Name = 'foo[?]' },
@{ Name = 'foo[*]' },@{ Name = 'foo[*' },@{ Name = 'foo*]' }
}
It "Should be able to create and set the location to <Name>" -TestCases $testCases {
param ( $Name )
if ( $Name -match "\*" -and $IsWindows ) {
Set-ItResult -Skipped -Because "'$Name' is not supported as directory name on Windows"
return
}
$item = New-Item -Type Directory -Path ${TESTDRIVE}/$Name
$item.Name | Should -Be $Name
$i = Get-Item -LiteralPath ${TESTDRIVE}/$Name
$i.Name | Should -Be $Name
$location = Get-Location
try {
Set-Location -LiteralPath ${TESTDRIVE}/$Name
$sName = [System.IO.Path]::GetFileName((Get-Location ).Path)
$sName | Should -Be $Name
}
catch {
throw "Set-Location $Name Failed"
}
finally {
Set-Location -LiteralPath $location.Path
}
}
}when run on my Mac, I get the following result Describing Should be able to set location to name with wild card
[+] Should be able to create and set the location to foo[ 36ms
[+] Should be able to create and set the location to foo] 13ms
[+] Should be able to create and set the location to foo[] 26ms
[+] Should be able to create and set the location to foo? 28ms
[+] Should be able to create and set the location to foo[?] 41ms
[-] Should be able to create and set the location to foo[*] 56ms
Expected strings to be the same, but they were different.
Expected length: 6
Actual length: 5
Strings differ at index 4.
Expected: 'foo[*]'
But was: 'foo[]'
18: $i.Name | Should -Be $Name
at <ScriptBlock>, /tmp/zap/tt.tests.ps1: line 18
[-] Should be able to create and set the location to foo[* 17ms
Expected strings to be the same, but they were different.
String lengths are both 5.
Strings differ at index 4.
Expected: 'foo[*'
But was: 'foo[]'
18: $i.Name | Should -Be $Name
at <ScriptBlock>, /tmp/zap/tt.tests.ps1: line 18
[-] Should be able to create and set the location to foo*] 20ms
Expected strings to be the same, but they were different.
String lengths are both 5.
Strings differ at index 3.
Expected: 'foo*]'
But was: 'foo[]'
18: $i.Name | Should -Be $Name
at <ScriptBlock>, /tmp/zap/tt.tests.ps1: line 18The files present in PS> ls $TESTDRIVE |ft -auto
Directory: /private/tmp/ea03bb1e-4ae5-46b7-af36-6341e4015b20
UnixMode User Group LastWriteTime Size Name
-------- ---- ----- ------------- ---- ----
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo?
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo[
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo[?]
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo[]
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo[*
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo[*]
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo]
drwxr-xr-x james wheel 4/8/2020 13:27 64 foo*]
|
|
@JamesWTruher Yeah, I suspected that there are going to be these sort of wildcard processing issues I located in Start-Process in other cmdlets as well. Unless you've already dove in, I'll look into the behavior you noted as a separate PR (unless you think it's somehow related to this specific fix for Start-Process). Note, for Windows your match should be more like |
|
The more I think about wildcards in the scenario the less I like it. I'd prefer literal paths - it is unnatural behavior to expand wildcards for these paths. I can’t imagine that someone selects a working directory by wildcard, and especially a redirect file. It is unreliable and unpredictable. |
|
@JamesWTruher Reproduced your Mac results. Set-Location -LiteralPath definitely doesn't handle processing '*' characters very well whereas the native shells do just fine. I do believe this is a separate issue though from the one this PR addresses though. Will look at the underlying code tomorrow to verify... |
|
@JamesWTruher I confirmed the behavior you reported is a different issue and is related to the code called within Set-Location that's responsible for case normalization. I have a potential fix and will submit that separately. Please proceed with adjudicating this PR as-is. Update: Noted behavior is corrected in #12294. This includes an enhanced version of your test script. |
src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs
Outdated
Show resolved
Hide resolved
|
@PowerShell/powershell-committee reviewed this, we agree that an implicit |
src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs
Outdated
Show resolved
Hide resolved
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
…w Changes - Added named argument to ResolveFilePath() call for clarity.
50215dd to
4794ad4
Compare
|
@daxian-dbw I think the PR is ready to merge. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
@NoMoreFood Thanks for your contribution! |
|
🎉 Handy links: |
PR Summary
Modified Start-Process such that an unspecified working directory is correctly treated as literal when being resolved.
Fix #11940
PR Context
Problem was discovered when investigating an error that resulted from executing Start-Process in a directory that contained wildcard characters.
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.