From cdc18a51880f230bb000056c1b7a5b0dd0e5d0f5 Mon Sep 17 00:00:00 2001 From: KirtiRamchandani Date: Sun, 24 May 2026 19:49:11 +0530 Subject: [PATCH 1/2] Respect CommandType during fuzzy Get-Command lookup --- .../engine/GetCommandCommand.cs | 49 ++++++++++++------- .../Get-Command.Tests.ps1 | 17 +++++++ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 10e8334021b..fc5ba87f33f 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -886,10 +886,9 @@ private void AccumulateMatchingCommands(IEnumerable commandNames) { if (TotalCount < 0 || count < TotalCount) { - IEnumerable commands; if (UseFuzzyMatching) { - foreach (var commandScore in ModuleUtils.GetFuzzyMatchingCommands( + foreach (CommandScore commandScore in ModuleUtils.GetFuzzyMatchingCommands( plainCommandName, Context, MyInvocation.CommandOrigin, @@ -897,37 +896,49 @@ private void AccumulateMatchingCommands(IEnumerable commandNames) rediscoverImportedModules: true, moduleVersionRequired: _isFullyQualifiedModuleSpecified)) { - _commandScores.Add(commandScore); - } + CommandInfo current = commandScore.Command; + + if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current)) + { + _accumulatedResults.Add(current); + _commandScores.Add(new CommandScore(current, commandScore.Score)); - commands = _commandScores.Select(static x => x.Command); + // Make sure we don't exceed the TotalCount parameter + ++count; + + if (TotalCount >= 0 && count >= TotalCount) + { + break; + } + } + } } else { - commands = ModuleUtils.GetMatchingCommands( + IEnumerable commands = ModuleUtils.GetMatchingCommands( plainCommandName, Context, MyInvocation.CommandOrigin, rediscoverImportedModules: true, moduleVersionRequired: _isFullyQualifiedModuleSpecified, useAbbreviationExpansion: UseAbbreviationExpansion); - } - - foreach (CommandInfo command in commands) - { - // Cannot pass in "command" by ref (foreach iteration variable) - CommandInfo current = command; - if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current)) + foreach (CommandInfo command in commands) { - _accumulatedResults.Add(current); + // Cannot pass in "command" by ref (foreach iteration variable) + CommandInfo current = command; - // Make sure we don't exceed the TotalCount parameter - ++count; - - if (TotalCount >= 0 && count >= TotalCount) + if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current)) { - break; + _accumulatedResults.Add(current); + + // Make sure we don't exceed the TotalCount parameter + ++count; + + if (TotalCount >= 0 && count >= TotalCount) + { + break; + } } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index a07ed78e753..5d43a316cff 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -29,6 +29,23 @@ Describe "Get-Command Feature tests" -Tag Feature { $cmd.Score | Should -BeLessOrEqual 3 } } + + It "Should respect CommandType when fuzzy matching" { + function Invoke-ZzqFuzzyCommandTypeThing { } + Set-Alias -Name Invoke-ZzqFuzzyCommandTypeThang -Value Invoke-ZzqFuzzyCommandTypeThing + + try { + $cmds = Get-Command Invoke-ZzqFuzzyCommandTypeThng -UseFuzzyMatching -CommandType Function + + $cmds.Name | Should -Contain 'Invoke-ZzqFuzzyCommandTypeThing' + $cmds.Name | Should -Not -Contain 'Invoke-ZzqFuzzyCommandTypeThang' + $cmds.CommandType | Should -Not -Contain ([System.Management.Automation.CommandTypes]::Alias) + } + finally { + Remove-Item -Path Function:\Invoke-ZzqFuzzyCommandTypeThing -ErrorAction SilentlyContinue + Remove-Item -Path Alias:\Invoke-ZzqFuzzyCommandTypeThang -ErrorAction SilentlyContinue + } + } } Context "-UseAbbreviationExpansion tests" { From cb0b32ef236bd4be07367b446e140cda9350907b Mon Sep 17 00:00:00 2001 From: KirtiRamchandani Date: Sun, 24 May 2026 20:11:54 +0530 Subject: [PATCH 2/2] Address Get-Command fuzzy review feedback --- .../engine/GetCommandCommand.cs | 47 ++++++++++--------- .../Get-Command.Tests.ps1 | 6 +-- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index fc5ba87f33f..af3b058078c 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -898,18 +898,9 @@ private void AccumulateMatchingCommands(IEnumerable commandNames) { CommandInfo current = commandScore.Command; - if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current)) + if (TryAddCommandToResults(ref current, commandScore.Score, ref count, out isDuplicate)) { - _accumulatedResults.Add(current); - _commandScores.Add(new CommandScore(current, commandScore.Score)); - - // Make sure we don't exceed the TotalCount parameter - ++count; - - if (TotalCount >= 0 && count >= TotalCount) - { - break; - } + break; } } } @@ -928,17 +919,9 @@ private void AccumulateMatchingCommands(IEnumerable commandNames) // Cannot pass in "command" by ref (foreach iteration variable) CommandInfo current = command; - if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current)) + if (TryAddCommandToResults(ref current, null, ref count, out isDuplicate)) { - _accumulatedResults.Add(current); - - // Make sure we don't exceed the TotalCount parameter - ++count; - - if (TotalCount >= 0 && count >= TotalCount) - { - break; - } + break; } } } @@ -977,6 +960,28 @@ private void AccumulateMatchingCommands(IEnumerable commandNames) } } + private bool TryAddCommandToResults(ref CommandInfo current, int? fuzzyScore, ref int currentCount, out bool isDuplicate) + { + if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current)) + { + _accumulatedResults.Add(current); + if (fuzzyScore is int score) + { + _commandScores.Add(new CommandScore(current, score)); + } + + // Make sure we don't exceed the TotalCount parameter + ++currentCount; + + if (TotalCount >= 0 && currentCount >= TotalCount) + { + return true; + } + } + + return false; + } + private bool FindCommandForName(SearchResolutionOptions options, string commandName, bool isPattern, bool emitErrors, ref int currentCount, out bool isDuplicate) { var searcher = new CommandSearcher( diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index 5d43a316cff..318c7ffdb30 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -31,10 +31,10 @@ Describe "Get-Command Feature tests" -Tag Feature { } It "Should respect CommandType when fuzzy matching" { - function Invoke-ZzqFuzzyCommandTypeThing { } - Set-Alias -Name Invoke-ZzqFuzzyCommandTypeThang -Value Invoke-ZzqFuzzyCommandTypeThing - try { + function Invoke-ZzqFuzzyCommandTypeThing { } + Set-Alias -Name Invoke-ZzqFuzzyCommandTypeThang -Value Invoke-ZzqFuzzyCommandTypeThing -Force + $cmds = Get-Command Invoke-ZzqFuzzyCommandTypeThng -UseFuzzyMatching -CommandType Function $cmds.Name | Should -Contain 'Invoke-ZzqFuzzyCommandTypeThing'