Skip to content

Commit 16017aa

Browse files
committed
Prefer exact command matches in autoloaded modules over implicit Get- prefix (PowerShell#24970)
1 parent eb7d6c1 commit 16017aa

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/System.Management.Automation/engine/CommandDiscovery.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,20 @@ internal static CommandInfo LookupCommandInfo(
728728
searchResolutionOptions, commandTypes, ref lastError);
729729
}
730730

731+
if (result == null && !commandName.Contains('-') && !commandName.Contains('\\'))
732+
{
733+
discoveryTracer.WriteLine(
734+
"The command [{0}] was not found, trying again with get- prepended",
735+
commandName);
736+
737+
try
738+
{
739+
result = LookupCommandInfo(StringLiterals.DefaultCommandVerb + '-' + commandName,
740+
commandTypes, searchResolutionOptions, commandOrigin, context);
741+
}
742+
catch (CommandNotFoundException) { }
743+
}
744+
731745
// Otherwise, invoke the CommandNotFound handler
732746
result ??= InvokeCommandNotFoundHandler(commandName, context, originalCommandName, commandOrigin);
733747
} while (false);
@@ -902,24 +916,7 @@ private static CommandInfo TryNormalSearch(string commandName,
902916

903917
try
904918
{
905-
if (!searcher.MoveNext())
906-
{
907-
if (!commandName.Contains('-') && !commandName.Contains('\\'))
908-
{
909-
discoveryTracer.WriteLine(
910-
"The command [{0}] was not found, trying again with get- prepended",
911-
commandName);
912-
913-
commandName = StringLiterals.DefaultCommandVerb + StringLiterals.CommandVerbNounSeparator + commandName;
914-
915-
try
916-
{
917-
result = LookupCommandInfo(commandName, commandTypes, searchResolutionOptions, commandOrigin, context);
918-
}
919-
catch (CommandNotFoundException) { }
920-
}
921-
}
922-
else
919+
if (searcher.MoveNext())
923920
{
924921
result = ((IEnumerator<CommandInfo>)searcher).Current;
925922
}

0 commit comments

Comments
 (0)