Skip to content
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

Fix tab completion for filename contains literal wildcard character #7407

Closed
wants to merge 10 commits into from
Expand Up @@ -4383,6 +4383,12 @@ internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext
if (CompletionRequiresQuotes(completionText, !useLiteralPath))
TravisEz13 marked this conversation as resolved.
Show resolved Hide resolved
{
var quoteInUse = quote == string.Empty ? "'" : quote;

Copy link
Member

Choose a reason for hiding this comment

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

should all changes be behind the experimental feature?

Copy link
Member

Choose a reason for hiding this comment

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

This was a condition of the committee

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Except filename completion will now also escape * and ? in non-literal path, the behaviour should be identical unless the experimental feature (the breaking part) is enabled.

if (!useLiteralPath)
{
completionText = WildcardPattern.Escape(completionText);
}

if (quoteInUse == "'")
{
completionText = completionText.Replace("'", "''");
Expand All @@ -4395,20 +4401,6 @@ internal static IEnumerable<CompletionResult> CompleteFilename(CompletionContext
completionText = completionText.Replace("$", "`$");
}

if (!useLiteralPath)
{
if (quoteInUse == "'")
{
completionText = completionText.Replace("[", "`[");
completionText = completionText.Replace("]", "`]");
}
else
{
completionText = completionText.Replace("[", "``[");
completionText = completionText.Replace("]", "``]");
}
}

completionText = quoteInUse + completionText + quoteInUse;
}
else if (quote != string.Empty)
Expand Down
Expand Up @@ -114,6 +114,9 @@ static ExperimentalFeature()
new ExperimentalFeature(
name: "PSCommandNotFoundSuggestion",
description: "Recommend potential commands based on fuzzy search on a CommandNotFoundException"),
new ExperimentalFeature(
name: "PSWildcardEscapeEscape",
description: "Fix WildcardPattern API: escape the escape character"),
};
EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);

Expand Down
21 changes: 17 additions & 4 deletions src/System.Management.Automation/engine/regex.cs
Expand Up @@ -194,15 +194,28 @@ internal static string Escape(string pattern, char[] charsNotToEscape)

char[] temp = new char[pattern.Length * 2 + 1];
int tempIndex = 0;
bool charNeedsEscaping = false;

for (int i = 0; i < pattern.Length; i++)
{
char ch = pattern[i];

//
// if it is a wildcard char, escape it
//
if (IsWildcardChar(ch) && !charsNotToEscape.Contains(ch))
if (ExperimentalFeature.IsEnabled("PSWildcardEscapeEscape"))
{
//
// if it is a wildcard char or escape char, escape it
//
charNeedsEscaping = IsWildcardChar(ch) || ch == escapeChar;
}
else
{
//
// if it is a wildcard char, escape it
//
charNeedsEscaping = IsWildcardChar(ch);
}

if (charNeedsEscaping && !charsNotToEscape.Contains(ch))
{
temp[tempIndex++] = escapeChar;
}
Expand Down
Expand Up @@ -3363,13 +3363,11 @@ internal static string RemoveProviderQualifier(string path)
StringContainsGlobCharacters(leafElement) ||
isLastLeaf)
{
string regexEscapedLeafElement = ConvertMshEscapeToRegexEscape(leafElement);
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As explained in the PR Summary, regexEscapedLeafElement is passed to WildcardPattern::Get, which does not make sense since in PowerShell, regex escaped string is \[abc\], while wildcard escaped string is `[abc`].
Moreover, why should we escape/alter the pattern string in methods GenerateNew*PathsWithGlobLeaf which is supposed to glob? This behaviour is causing issue on tab completion when filename contains bracket.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can I mark this resolved?


// Construct the glob filter

WildcardPattern stringMatcher =
WildcardPattern.Get(
regexEscapedLeafElement,
leafElement,
WildcardOptions.IgnoreCase);

// Construct the include filter
Expand Down Expand Up @@ -3969,13 +3967,11 @@ internal static string RemoveProviderQualifier(string path)
(StringContainsGlobCharacters(leafElement) ||
isLastLeaf))
{
string regexEscapedLeafElement = ConvertMshEscapeToRegexEscape(leafElement);

// Construct the glob filter

WildcardPattern stringMatcher =
WildcardPattern.Get(
regexEscapedLeafElement,
leafElement,
WildcardOptions.IgnoreCase);

// Construct the include filter
Expand Down
3 changes: 3 additions & 0 deletions test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Expand Up @@ -882,6 +882,9 @@ dir -Recurse `
@{ inputStr = "Get-Process >'.\My ``[Path``]\'"; expected = "'.${separator}My ``[Path``]${separator}test.ps1'" }
@{ inputStr = "Get-Process >${tempDir}\My"; expected = "'${tempDir}${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > '${tempDir}\My ``[Path``]\'"; expected = "'${tempDir}${separator}My ``[Path``]${separator}test.ps1'" }
@{ inputStr = "Set-Location -Path 'My ``["; expected = "'.${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > 'My ``["; expected = "'.${separator}My ``[Path``]'" }
@{ inputStr = "Get-Process > '${tempDir}\My ``["; expected = "'${tempDir}${separator}My ``[Path``]'" }
)

Push-Location -Path $tempDir
Expand Down
68 changes: 68 additions & 0 deletions test/powershell/engine/Api/WildcardPattern.Tests.ps1
@@ -0,0 +1,68 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Copyright (c) Microsoft Corporation. All rights reserved.
# Copyright (c) Microsoft Corporation.

# Licensed under the MIT License.

Describe "WildcardPattern Escape - Experimental-Feature-Disabled" -Tags "CI" {
kwkam marked this conversation as resolved.
Show resolved Hide resolved
kwkam marked this conversation as resolved.
Show resolved Hide resolved

BeforeAll {
$testName = 'PSWildcardEscapeEscape'
$skipTest = $EnabledExperimentalFeatures.Contains($testName)

if ($skipTest) {
Write-Verbose "Test Suite Skipped. The test suite requires the experimental feature '$testName' to be disabled." -Verbose
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = $true
}
}

AfterAll {
if ($skipTest) {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
}

It "Unescaping '<escapedStr>' which escaped from '<inputStr>' should get the original" -TestCases @(
@{inputStr = '*This'; escapedStr = '`*This'}
@{inputStr = 'Is?'; escapedStr = 'Is`?'}
@{inputStr = 'Real[ly]'; escapedStr = 'Real`[ly`]'}
@{inputStr = 'Ba`sic'; escapedStr = 'Ba`sic'}
@{inputStr = 'Test `[more`]?'; escapedStr = 'Test ``[more``]`?'}
) {
param($inputStr, $escapedStr)

[WildcardPattern]::Escape($inputStr) | Should -BeExactly $escapedStr
[WildcardPattern]::Unescape($escapedStr) | Should -BeExactly $inputStr
}
}

Describe "WildcardPattern Escape - Experimental-Feature-Enabled" -Tags "CI" {

BeforeAll {
$testName = 'PSWildcardEscapeEscape'
$skipTest = -not $EnabledExperimentalFeatures.Contains($testName)

if ($skipTest) {
Write-Verbose "Test Suite Skipped. The test suite requires the experimental feature '$testName' to be enabled." -Verbose
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = $true
}
}

AfterAll {
if ($skipTest) {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
}

It "Unescaping '<escapedStr>' which escaped from '<inputStr>' should get the original" -TestCases @(
@{inputStr = '*This'; escapedStr = '`*This'}
@{inputStr = 'Is?'; escapedStr = 'Is`?'}
@{inputStr = 'Real[ly]'; escapedStr = 'Real`[ly`]'}
@{inputStr = 'Ba`sic'; escapedStr = 'Ba``sic'}
@{inputStr = 'Test `[more`]?'; escapedStr = 'Test ```[more```]`?'}
) {
param($inputStr, $escapedStr)

[WildcardPattern]::Escape($inputStr) | Should -BeExactly $escapedStr
[WildcardPattern]::Unescape($escapedStr) | Should -BeExactly $inputStr
}
}
1 change: 1 addition & 0 deletions test/tools/TestMetadata.json
@@ -1,6 +1,7 @@
{
"ExperimentalFeatures": {
"Microsoft.PowerShell.Utility.PSDebugRunspaceWithBreakpoints": ["test/powershell/Modules/Microsoft.PowerShell.Utility/New-PSBreakpoint.Tests.ps1"],
"PSWildcardEscapeEscape": [ "test/powershell/engine/Api/WildcardPattern.Tests.ps1" ],
"ExpTest.FeatureOne": [ "test/powershell/engine/ExperimentalFeature/ExperimentalFeature.Basic.Tests.ps1" ]
}
}