-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix handling of -Path when it literally contains wildcard char #5896
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
*engine/CommandCompletion/CompletionCompleters: Use WildcardPattern::Escape to escape completion text *engine/SessionStateContainer: GetChildItem: Unescape non-literal, non-glob path before existence checking NewItem: Unescape non-literal path when -Name is not set *engine/regex: WildcardPattern::Escape should also escape '`', since WildcardPattern::Unescape would unescape it, and the matcher parse it as escape char *namespaces/LocationGlobber: Do not pass regex escaped string to WildcardPattern::Get *utils/PathUtils: Unescape non-literal path, non-glob path before calling GetUnresolvedProviderPathFromPSPath, it only accepts literal path
|
@kwkam Please add tests. Marking the PR as |
|
@kwkam Could you continue with the PR? |
|
@adityapatwardhan Done. I am not sure whether they are in the proper file though |
|
@daxian-dbw @lzybkr @anmenaga Can you have a look? |
|
@anmenaga @daxian-dbw ping ... |
*namespaces/LocationGlobber: Do not escape CWD when LiteralPath is used
Rename-Item will fail if -Path is relative and contains special char, and CWD also contains special char, complaining -Path does not exist because IsCurrentLocationOrAncestor can only handle literal path.
This reverts commit 2f22264.
|
@adityapatwardhan Test failures related to the changes are solved (I have no idea why the remote session test is failing). Please let me know if anything were missing/wrong. |
|
@anmenaga @daxian-dbw ping.. |
|
|
||
| Remove-Item -Path $testfilepathSp | ||
| $testfilepathSp | Should -Not -Exist | ||
| } |
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.
Please correct a indentation of the It block.
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 believe my indentation is correct.
What I notice is, there are 93 lines of code that is mixing tab/space for indentation.
Should I fix them here?
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.
Sorry, GitHub show a shift.
Closed.
Feel free to fix if it is only one string.
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 I should leave it since the PR is not related to code formatting.
Perhaps open an issue for replacing tabs to spaces in the project (whole/some modules/only the misused)?
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 hope we already fixed most of tabs in test .ps1 files.
Feel free to fix remains in new PR.
iSazonov
left a comment
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.
@kwkam The PR is very large. Can we split it on some new PRs?
| <value>Cannot move item because the item at '{0}' is in use.</value> | ||
| </data> | ||
| <data name="RenameMultipleItemError" xml:space="preserve"> | ||
| <value>Cannot rename item because the path '{0}' resolved to multiple items. Only one item can be renamed at a time.</value> |
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.
With long path names the message will be unreadable. Can we reword and place path '{0}' at end?
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 am not sure. I just copied that from another resx file.
How about this?
Cannot rename item because only one item can be renamed at a time, but multiple items resolved from the path '{0}'.
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.
@SteveL-MSFT Could you please review the resource string?
| # This should find the file [.exe | ||
| $result = Get-Command -Name .\[.exe -Type Application | ||
| $result | Should -Not -BeNullOrEmpty | ||
| $result | Should -Be [.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.
Please use -BeExactly for string.
| } // ProcessRecord | ||
|
|
||
| private void MoveItem(string path) | ||
| private void MoveItem(string path, bool literalPath) |
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.
Should we assign default bool literalPath = false?
| switch (resolvedPaths.Count) | ||
| { | ||
| case 0: | ||
| // Do nothing |
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.
Please remove obvious comment.
| } | ||
| } while (false); | ||
|
|
||
| } // ProcessRecord |
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.
Please remove the comment.
| CommandDiscovery.discoveryTracer.WriteLine( | ||
| "Path resolved to: {0}", | ||
| path); | ||
| // Find the match if it is. |
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.
Seems previous tracer comment is more useful. Please remove comment in the line or reword.
| result = GetInfoFromPath(path); | ||
| } | ||
| } while (false); | ||
| // If the path was resolved, and it exists |
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.
Please remove obvious comment.
|
@kwkam The PR is very large. Could you split it on some small PRs for more fast review? |
|
@iSazonov Sorry, I am afraid I would not have free time get it done very soon. Feel free to fork this if you are in hurry. |
|
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. |
Fix handling of
-Pathargument that contains wildcard char (eg../`[file`].txt) on some Cmdlet.Fix tab completion when path contains wildcard char (eg.
./`[fi[TAB] did not get completed).Make
Get-Commandtry to resolve-Pathas literal when it contains invalid wildcard pattern.I believe this also solve/work around the following issues:
#3724 #3725 #4726 #6232
PR Summary
*Microsoft.PowerShell.Commands.Management/commands/management/Navigation:
Move-Item: Set the
context.SuppressWildcardExpansioninMoveIteminstead of escaping the path every time. Also solve the issue where Move-Item complains -Path wildcard pattern is not valid when -Path contains special chars that forms an invalid pattern.Rename-Item: Unescape non-literal, non-glob path in
ProccessRecordand set thecontext.SuppressWildcardExpansioninRenameItem. This solve the issue where Rename-Item complains -Path does not exist when both -Path and CWD contains special char.*System.Management.Automation/engine/CommandCompletion/CompletionCompleters:
Use
WildcardPattern::Escapeto escape completion text of filename.*System.Management.Automation/engine/CommandSearcher:
Do not resolve path as glob when it contains invalid wildcard pattern (eg.
./[.ps1).Merge the path resolving code from
GetNextFromPathintoResolvePSPathand use that function.*System.Management.Automation/engine/GetCommandCommand:
Do not treat path as pattern when it contains invalid wildcard pattern (eg.
./[.ps1).*System.Management.Automation/engine/SessionStateContainer:
Get-ChildItem: Unescape non-literal, non-glob path before existence checking. This solve the issue where Get-GhildItem does not behave correctly when -Path contains special char. For example,
Get-ChildItem -Path './`[dir`]'will complainCannot find path ...whileGet-ChildItem -Path './`[dir`]' -Depth 0will work fine.New-Item: 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]1in current directory, and tab completion suggests./`[file`]1for the -Path argument, butNew-Item -Path './`[file`]2'will create a file named`[file`]2instead of[file]2.*System.Management.Automation/engine/regex:
WildcardPattern::Escapeshould also escape`, sinceWildcardPattern::Unescapewould unescape it, and the matcher parse it as escape char.*System.Management.Automation/namespaces/LocationGlobber:
Do not escape CWD when LiteralPath is used. This causes issues when both -LiteralPath and CWD contains special char.
Do not pass regex escaped string to
WildcardPattern::Get. This fails tab completion when doing./path/to/`[f[TAB].*System.Management.Automation/utils/PathUtils:
Unescape non-literal, non-glob path before calling
GetUnresolvedProviderPathFromPSPathsince it only accepts literal path. This solve the issue where some commands depending on that method will treat -Path as literal unintentionally. For example,Out-File -Path './`[out`].txt' -Appendwill create a new file named`[out`].txtinstead of writing to[out].txt.PR Checklist
Note: Please mark anything not applicable to this PR
NA.[feature]if the change is significant or affects feature testsWIP:to the beginning of the title and remove the prefix when the PR is ready.