Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions src/System.Management.Automation/engine/GetCommandCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,48 +886,58 @@ private void AccumulateMatchingCommands(IEnumerable<string> commandNames)
{
if (TotalCount < 0 || count < TotalCount)
{
IEnumerable<CommandInfo> commands;
if (UseFuzzyMatching)
{
foreach (var commandScore in ModuleUtils.GetFuzzyMatchingCommands(
foreach (CommandScore commandScore in ModuleUtils.GetFuzzyMatchingCommands(
plainCommandName,
Context,
MyInvocation.CommandOrigin,
_fuzzyMatcher,
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));

// Make sure we don't exceed the TotalCount parameter
++count;

commands = _commandScores.Select(static x => x.Command);
if (TotalCount >= 0 && count >= TotalCount)
{
break;
}
}
}
}
else
{
commands = ModuleUtils.GetMatchingCommands(
IEnumerable<CommandInfo> 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);

// Make sure we don't exceed the TotalCount parameter
++count;
// Cannot pass in "command" by ref (foreach iteration variable)
CommandInfo current = command;

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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ Describe "Get-Command Feature tests" -Tag Feature {
$cmd.Score | Should -BeLessOrEqual 3
}
}

It "Should not return duplicate commands from multiple copies of a module when using -UseFuzzyMatching" {
# Create two discoverable copies of the same module so the duplicate-candidate scenario
# is reproduced consistently without relying on platform-specific installed modules.
$moduleRoots = 1..2 | ForEach-Object {
$moduleRoot = Join-Path $TestDrive "ModuleRoot$_"
$modulePath = Join-Path $moduleRoot "DuplicateFuzzyModule"
$null = New-Item -ItemType Directory -Path $modulePath
New-ModuleManifest -Path (Join-Path $modulePath "DuplicateFuzzyModule.psd1") `
-ModuleVersion "1.0" `
-CmdletsToExport "Get-FuzzyDuplicate"
$moduleRoot
}

$originalModulePath = $env:PSModulePath
try {
$env:PSModulePath = $moduleRoots -join [System.IO.Path]::PathSeparator
$cmds = Get-Command Get-FuzzyDuplicat -UseFuzzyMatching -FuzzyMinimumDistance 1
}
finally {
$env:PSModulePath = $originalModulePath
}

$cmds | Should -HaveCount 1
$cmds.Name | Should -BeExactly "Get-FuzzyDuplicate"
$cmds.Score | Should -Be 1
}
}

Context "-UseAbbreviationExpansion tests" {
Expand Down
Loading