From bdf1cfd2b17bcbb4d2b58231939670eed0564650 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 19 Oct 2023 14:41:28 -0700 Subject: [PATCH] Fix UNC path completion regression (#20419) (#20541) --- .../CommandCompletion/CompletionCompleters.cs | 21 ++++++++----------- .../TabCompletion/TabCompletion.Tests.ps1 | 5 +++++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index e222075060..357ce126ab 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4629,22 +4629,19 @@ namespace System.Management.Automation string basePath; if (!relativePaths) { - string providerName = $"{provider.ModuleName}\\{provider.Name}::"; - if (pathInfo.Path.StartsWith(providerName, StringComparison.OrdinalIgnoreCase)) + if (pathInfo.Drive is null) { - basePath = pathInfo.Path.Substring(providerName.Length); + basePath = dirInfo.FullName; } else { - providerName = $"{provider.Name}::"; - if (pathInfo.Path.StartsWith(providerName, StringComparison.OrdinalIgnoreCase)) - { - basePath = pathInfo.Path.Substring(providerName.Length); - } - else - { - basePath = pathInfo.Path; - } + int stringStartIndex = pathInfo.Drive.Root.EndsWith(provider.ItemSeparator) && pathInfo.Drive.Root.Length > 1 + ? pathInfo.Drive.Root.Length - 1 + : pathInfo.Drive.Root.Length; + + basePath = pathInfo.Drive.VolumeSeparatedByColon + ? string.Concat(pathInfo.Drive.Name, ":", dirInfo.FullName.AsSpan(stringStartIndex)) + : string.Concat(pathInfo.Drive.Name, dirInfo.FullName.AsSpan(stringStartIndex)); } basePath = basePath.EndsWith(provider.ItemSeparator) diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index 800e15ad84..613e673cee 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -1276,6 +1276,11 @@ class InheritedClassTest : System.Attribute } } + It 'Should correct slashes in UNC path completion' -Skip:(!$IsWindows) { + $Res = TabExpansion2 -inputScript 'Get-ChildItem //localhost/c$/Windows' + $Res.CompletionMatches[0].CompletionText | Should -Be "'\\localhost\c$\Windows'" + } + It 'Should keep custom drive names when completing file paths' { $TempDriveName = "asdf" $null = New-PSDrive -Name $TempDriveName -PSProvider FileSystem -Root $HOME