-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix New-Item -Path/-Target with wildcard char #9259
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
Conversation
Unescape non-literal path when -Name is not set. This works around for New-Item treating -Path as literal path while it can also be globbable. For example, assuming there is "[file]1" in current directory, and tab completion suggests './`[file`]1' for the -Path argument, but > New-Item -Path './`[file`]2' will create a file named "`[file`]2" instead of "[file]2".
test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/Remove-Item.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/Remove-Item.Tests.ps1
Outdated
Show resolved
Hide resolved
Co-Authored-By: kwkam <kwkam@users.noreply.github.com>
|
This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days. |
| $expectedTarget = Join-Path -Path $tmpDirectory -ChildPath $testlinkSrcSpName | ||
|
|
||
| $fileInfo = Get-ChildItem -Path $FullyQualifiedLinkSp | ||
| $fileInfo.Target | Should -Match ([regex]::Escape($expectedTarget)) |
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.
Why can't we do an -BeExactly assertion here instead of a -Match?
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.
I guess we can.
Actually this was created using the tests in the same file as reference:
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1
Line 217 in 2737e74
| $fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFile)) |
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1
Line 242 in 2737e74
| $fileInfo.Target | Should -Match ([regex]::Escape($FullyQualifiedFolder)) |
Perhaps we have to review the New-Item test sometime.
src/System.Management.Automation/engine/SessionStateContainer.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/SessionStateContainer.cs
Outdated
Show resolved
Hide resolved
|
I believe we should not change this behavior. To workaround your problem you could simply use the following: PS D:\temp\newitem> New-Item -Name `[file`]2
Directory: D:\temp\newitem
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 5/10/2019 10:32 AM 0 [file]2 |
|
The problem is the inconsistency of # to get '[file]1'
Get-Item -Path './`[file`]1'
# to move '[file]1'
Move-Item -Path './`[file`]1' -Destination ./dir
# to copy '[file]1'
Copy-Item -Path './dir/`[file`]1' -Destination ..
# to remove '[file]1'
Remove-Item -Path './dir/`[file`]1'All above commands behave correctly with tab completion, but when we do # what if we try to overwrite '[file]1'?
New-Item -Path './`[file`]1' -Value 'This is [file]1'A new file Of course, we can use |
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 30 days. It will be closed if no further activity occurs within 10 days of this comment. |
|
@adityapatwardhan Could you please update your review? |
|
Changing the behavior of |
|
@PowerShell/powershell-committee discussed this one. The Issue at hand is that tab completion uses single quotes and escaped characterss in what amounts to a literal path, and you only need one or the other to work properly. We agreed not to fix it in the We'd welcome a PR to fix the tab completion, but given that we're not taking this one, closing it out. |
|
@kwkam Have you plans to fix the tab completion? |
Reopen #7404
PR Summary
Unescape non-literal path when -Name is not set.
Fix failure with symlink/junction target which contains special character.
PR Context
This works around for New-Item treating -Path as literal path while
it can also be globbable. For example, assuming there is
"[file]1"in current directory, and tab completion suggests
'./`[file`]1'for the -Path argument, but
will create a file named
"`[file`]2"instead of"[file]2".Also fixes an issue that New-Item does not handle -Target properly
when it contains escaped wildcard character. For example, there is
a directory
"[dir]1"in current directory, and tab completionsuggests
'./`[dir`]1'for the -Target argument, butwill complain that it cannot find the item.
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.