Fix Rename-Item -Path with wildcard char#7398
Fix Rename-Item -Path with wildcard char#7398adityapatwardhan merged 16 commits intoPowerShell:masterfrom
Conversation
Set the context.SuppressWildcardExpansion in MoveItem instead of escaping the path every time. Also solve the issue where Move-Item complains -Path wildcard pattern is not valid when -Path contains special characters that forms an invalid pattern.
| /// </summary> | ||
| protected override void ProcessRecord() | ||
| { | ||
| do |
There was a problem hiding this comment.
Why do we use cycle here? We have single input string.
I would try to make things easier - just try to rename in try-catch.
| <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.
It makes no sense to tell the user that we have tried to resolve wildcards - in fact, we consider the Path parameter as literal.
There was a problem hiding this comment.
I wonder if we would add support for renaming multiple items like the ren command in cmd?
eg. Rename-Item -Path '*.txt' -NewExtension '.bak'
or, Rename-Item -Path '*.txt' -Format 'renamed_{0}'
There was a problem hiding this comment.
I believe the enhancement is out of the PR and we should discuss it in a new tracking Issue (please open).
| } | ||
| } | ||
| finally { | ||
| } |
There was a problem hiding this comment.
We can remove the empty finally block.
| protected override void ProcessRecord() | ||
| { | ||
| try | ||
| { |
There was a problem hiding this comment.
Seem all used methods use try-catch and we can remove the try.
This reverts commit be59b3d.
|
@kwkam Please rebase to fix CI Travis failure. |
iSazonov
left a comment
There was a problem hiding this comment.
Please look tests - I don't see test related the PR.
|
|
||
| private Collection<PathInfo> GetResolvedPaths(string path) | ||
| { | ||
| Collection<PathInfo> results = new Collection<PathInfo>(); |
There was a problem hiding this comment.
It is extra allocation. I think we should remove this and check null in calling code.
| break; | ||
|
|
||
| default: | ||
| RenameItem(Path, literalPath: true); |
There was a problem hiding this comment.
I'd expect that we unescape path, check existance (or try rename), if fail then resolve globs (write an error if cpunt > 1) and try rename again.
There was a problem hiding this comment.
I believe -Path should try to resolve globs first, since it's the expected behaviour, and users can escape the wildcard character, for example
Rename-Item -Path '`[A-B`]' -NewName 'A-B'will prevent globbing.
| /// </summary> | ||
| protected override void ProcessRecord() | ||
| { | ||
| if (base.SuppressWildcardExpansion) |
There was a problem hiding this comment.
Do we really need base prefix?
|
@iSazonov Should I run |
|
Why do you need to rebase? |
|
@kwkam Ok, I understand. You can rebase to temporary skip broken tests. |
|
@iSazonov I see. I would not rebase if it is not necessary. |
Do not escape CWD when LiteralPath is used. This causes issues when both -LiteralPath and CWD contains special characters.
|
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. |
|
@SteveL-MSFT do we have interest in the change? |
|
@iSazonov yes, interested in this change |
|
@kwkam Please rebase to pass CIs. |
|
@iSazonov OK, rebased to latest master. |
|
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. |
src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs
Show resolved
Hide resolved
|
@kwkam Thank you for your contribution! |
PR Summary
*Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs:
Unescape non-literal, non-glob path in ProccessRecord and
set the context.SuppressWildcardExpansion in RenameItem.
This solve the issue where Rename-Item complains -Path does not exist
when both -Path and CWD contains special characters.
*System.Management.Automation/namespaces/LocationGlobber.cs:
Do not escape CWD when LiteralPath is used.
This causes issues when both -LiteralPath and CWD contains special characters. For example,
will complain that
[test]file.txtdoes not exist when CWD contains special characters.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests