Skip to content

Conversation

@kwkam
Copy link
Contributor

@kwkam kwkam commented Jan 13, 2018

Fix handling of -Path argument 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-Command try to resolve -Path as 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.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 chars that forms an invalid pattern.
Rename-Item: 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 char.

*System.Management.Automation/engine/CommandCompletion/CompletionCompleters:
Use WildcardPattern::Escape to 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 GetNextFromPath into ResolvePSPath and 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 complain Cannot find path ... while Get-ChildItem -Path './`[dir`]' -Depth 0 will 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]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.

*System.Management.Automation/engine/regex:
WildcardPattern::Escape should also escape `, since WildcardPattern::Unescape would 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 GetUnresolvedProviderPathFromPSPath since 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' -Append will create a new file named `[out`].txt instead of writing to [out].txt.

PR Checklist

Note: Please mark anything not applicable to this PR NA.

*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
@adityapatwardhan
Copy link
Member

adityapatwardhan commented Jan 22, 2018

@kwkam Please add tests. Marking the PR as WIP

@adityapatwardhan adityapatwardhan changed the title Fix path handling when contains wildcard char WIP: Fix path handling when contains wildcard char Jan 22, 2018
@iSazonov
Copy link
Collaborator

@kwkam Could you continue with the PR?

@kwkam
Copy link
Contributor Author

kwkam commented Feb 13, 2018

@adityapatwardhan Done. I am not sure whether they are in the proper file though

@kwkam kwkam changed the title WIP: Fix path handling when contains wildcard char Fix path handling when contains wildcard char Feb 15, 2018
@adityapatwardhan
Copy link
Member

@daxian-dbw @lzybkr @anmenaga Can you have a look?

@adityapatwardhan
Copy link
Member

@anmenaga @daxian-dbw ping ...

kwkam added 7 commits March 3, 2018 17:55
*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.
@kwkam kwkam requested a review from adityapatwardhan as a code owner March 5, 2018 09:55
@kwkam
Copy link
Contributor Author

kwkam commented Apr 12, 2018

@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.

@adityapatwardhan
Copy link
Member

@anmenaga @daxian-dbw ping..

@kwkam kwkam changed the title Fix path handling when contains wildcard char Fix handling of -Path when it literally contains wildcard char May 12, 2018

Remove-Item -Path $testfilepathSp
$testfilepathSp | Should -Not -Exist
}
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

@iSazonov iSazonov May 20, 2018

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.

Copy link
Contributor Author

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)?

Copy link
Collaborator

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.

Copy link
Collaborator

@iSazonov iSazonov left a 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>
Copy link
Collaborator

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?

Copy link
Contributor Author

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}'.

Copy link
Collaborator

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
Copy link
Collaborator

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)
Copy link
Collaborator

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
Copy link
Collaborator

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
Copy link
Collaborator

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.
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove obvious comment.

@iSazonov
Copy link
Collaborator

@kwkam The PR is very large. Could you split it on some small PRs for more fast review?

@kwkam
Copy link
Contributor Author

kwkam commented May 22, 2018

@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.
P.S. Appending ?w=1 to the URL (or --ignore-white-space on command line) could help a bit when viewing the diff.

@stale
Copy link

stale bot commented Jun 21, 2018

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.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale bot added the Stale label Jun 21, 2018
@stale stale bot removed the Stale label Jun 30, 2018
@kwkam
Copy link
Contributor Author

kwkam commented Jul 29, 2018

Split into #7396 #7397 #7398 #7399

@iSazonov Please let me know if anything is incorrect

@kwkam kwkam closed this Jul 29, 2018
@kwkam kwkam deleted the fix-wcpath branch January 11, 2019 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants