diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 9dec8bcb3a5..3de731faba4 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4618,9 +4618,27 @@ private static List GetFileSystemProviderResults( string basePath; if (!relativePaths) { - basePath = dirInfo.FullName.EndsWith(provider.ItemSeparator) - ? providerPrefix + dirInfo.FullName - : providerPrefix + dirInfo.FullName + provider.ItemSeparator; + string providerName = $"{provider.ModuleName}\\{provider.Name}::"; + if (pathInfo.Path.StartsWith(providerName, StringComparison.OrdinalIgnoreCase)) + { + basePath = pathInfo.Path.Substring(providerName.Length); + } + else + { + providerName = $"{provider.Name}::"; + if (pathInfo.Path.StartsWith(providerName, StringComparison.OrdinalIgnoreCase)) + { + basePath = pathInfo.Path.Substring(providerName.Length); + } + else + { + basePath = pathInfo.Path; + } + } + + basePath = basePath.EndsWith(provider.ItemSeparator) + ? providerPrefix + basePath + : providerPrefix + basePath + provider.ItemSeparator; basePath = RebuildPathWithVars(basePath, homePath, stringType, literalPaths, out baseQuotesNeeded); } else diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index ca159b798c3..4ae438e423d 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -1254,6 +1254,13 @@ class InheritedClassTest : System.Attribute } } + It 'Should keep custom drive names when completing file paths' { + $TempDriveName = "asdf" + $null = New-PSDrive -Name $TempDriveName -PSProvider FileSystem -Root $HOME + (TabExpansion2 -inputScript "${TempDriveName}:\").CompletionMatches[0].CompletionText | Should -BeLike "${TempDriveName}:*" + Remove-PSDrive -Name $TempDriveName + } + Context "Cmdlet name completion" { BeforeAll { $testCases = @(