Skip to content

Commit e7ee9da

Browse files
authored
Remove duplicate modules from completion results (#25538)
1 parent e42ce16 commit e7ee9da

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,16 @@ internal static List<CompletionResult> CompleteModuleName(CompletionContext cont
499499
nestedModulesToFilterOut = new(currentModule.NestedModules);
500500
}
501501

502+
var completedModules = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
502503
foreach (PSObject item in psObjects)
503504
{
504505
var moduleInfo = (PSModuleInfo)item.BaseObject;
505506
var completionText = moduleInfo.Name;
506-
var listItemText = completionText;
507+
if (!completedModules.Add(completionText))
508+
{
509+
continue;
510+
}
511+
507512
if (shortNameSearch
508513
&& completionText.Contains('.')
509514
&& !shortNamePattern.IsMatch(completionText.Substring(completionText.LastIndexOf('.') + 1))
@@ -526,7 +531,7 @@ internal static List<CompletionResult> CompleteModuleName(CompletionContext cont
526531

527532
completionText = CompletionHelpers.QuoteCompletionText(completionText, quote);
528533

529-
result.Add(new CompletionResult(completionText, listItemText, CompletionResultType.ParameterValue, toolTip));
534+
result.Add(new CompletionResult(completionText, listItemText: moduleInfo.Name, CompletionResultType.ParameterValue, toolTip));
530535
}
531536
}
532537

test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ Describe "TabCompletion" -Tags CI {
3636
It 'Should not include duplicate command results' {
3737
$OldModulePath = $env:PSModulePath
3838
$tempDir = Join-Path -Path $TestDrive -ChildPath "TempPsModuleDir"
39+
$ModuleDirs = @(
40+
Join-Path $tempDir "TestModule1\1.0"
41+
Join-Path $tempDir "TestModule1\1.1"
42+
Join-Path $tempDir "TestModule2\1.0"
43+
)
3944
try
4045
{
41-
$ModuleDirs = @(
42-
Join-Path $tempDir "TestModule1\1.0"
43-
Join-Path $tempDir "TestModule1\1.1"
44-
Join-Path $tempDir "TestModule2\1.0"
45-
)
4646
foreach ($Dir in $ModuleDirs)
4747
{
4848
$NewDir = New-Item -Path $Dir -ItemType Directory -Force
@@ -61,6 +61,36 @@ Describe "TabCompletion" -Tags CI {
6161
finally
6262
{
6363
$env:PSModulePath = $OldModulePath
64+
Remove-Item -LiteralPath $ModuleDirs -Recurse -Force
65+
}
66+
}
67+
68+
It 'Should not include duplicate module results' {
69+
$OldModulePath = $env:PSModulePath
70+
$tempDir = Join-Path -Path $TestDrive -ChildPath "TempPsModuleDir"
71+
try
72+
{
73+
$ModuleDirs = @(
74+
Join-Path $tempDir "TestModule1\1.0"
75+
Join-Path $tempDir "TestModule1\1.1"
76+
)
77+
foreach ($Dir in $ModuleDirs)
78+
{
79+
$NewDir = New-Item -Path $Dir -ItemType Directory -Force
80+
$ModuleName = $NewDir.Parent.Name
81+
Set-Content -Value 'MyTestFunction{}' -LiteralPath "$($NewDir.FullName)\$ModuleName.psm1"
82+
New-ModuleManifest -Path "$($NewDir.FullName)\$ModuleName.psd1" -RootModule "$ModuleName.psm1" -FunctionsToExport "MyTestFunction" -ModuleVersion $NewDir.Name
83+
}
84+
85+
$env:PSModulePath += [System.IO.Path]::PathSeparator + $tempDir
86+
$Res = TabExpansion2 -inputScript 'Import-Module -Name TestModule'
87+
$Res.CompletionMatches.Count | Should -Be 1
88+
$Res.CompletionMatches[0].CompletionText | Should -Be TestModule1
89+
}
90+
finally
91+
{
92+
$env:PSModulePath = $OldModulePath
93+
Remove-Item -LiteralPath $ModuleDirs -Recurse -Force
6494
}
6595
}
6696

0 commit comments

Comments
 (0)