Skip to content
Closed
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
16 changes: 5 additions & 11 deletions src/System.Management.Automation/engine/GetCommandCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,7 @@ private void AccumulateMatchingCmdlets()
{
Comment thread
iSazonov marked this conversation as resolved.
Resolved
Comment thread
iSazonov marked this conversation as resolved.
Resolved
Comment thread
iSazonov marked this conversation as resolved.
Resolved
Comment thread
iSazonov marked this conversation as resolved.
Resolved
Comment thread
iSazonov marked this conversation as resolved.
Resolved
Comment thread
iSazonov marked this conversation as resolved.
Resolved
_commandType = CommandTypes.Cmdlet | CommandTypes.Function | CommandTypes.Filter | CommandTypes.Alias | CommandTypes.Configuration;

Collection<string> commandNames = new Collection<string>();
commandNames.Add("*");
AccumulateMatchingCommands(commandNames);
AccumulateMatchingCommands(new string[] { "*" });
}

private bool IsNounVerbMatch(CommandInfo command)
Expand Down Expand Up @@ -681,13 +679,7 @@ private bool IsNounVerbMatch(CommandInfo command)
/// </summary>
private void AccumulateMatchingCommands()
{
Collection<string> commandNames =
SessionStateUtilities.ConvertArrayToCollection<string>(this.Name);

if (commandNames.Count == 0)
{
commandNames.Add("*");
}
var commandNames = string.IsNullOrEmpty(Name) ? new string[] { "*" } : Name;

AccumulateMatchingCommands(commandNames);
}
Expand Down Expand Up @@ -722,10 +714,12 @@ private void AccumulateMatchingCommands(IEnumerable<string> commandNames)
options |= SearchResolutionOptions.ResolveFunctionPatterns;
}

foreach (string commandName in commandNames)
foreach (string name in commandNames)
{
try
{
string commandName = name.Trim();

// Determine if the command name is module-qualified, and search
// available modules for the command.
string moduleName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe "Get-Command Tests" -Tags "CI" {
BeforeAll {
function TestGetCommand-DynamicParametersDCR
Expand Down Expand Up @@ -28,7 +29,7 @@ Describe "Get-Command Tests" -Tags "CI" {
"return1" {
$attr = [System.Management.Automation.ParameterAttribute]::new()
$attr.Mandatory = $true
$dynamicParameter = [System.Management.Automation.RuntimeDefinedParameter]::new("OneString",[string],$attr)
$dynamicParameter = [System.Management.Automation.RuntimeDefinedParameter]::new("OneString",[string],$attr)
$dynamicParamDictionary.Add("OneString",$dynamicParameter)
break
}
Expand Down Expand Up @@ -263,4 +264,8 @@ Describe "Get-Command Tests" -Tags "CI" {
VerifyDynamicParametersExist -cmdlet $results[0] -parameterNames $paramName
VerifyParameterType -cmdlet $results[0] -parameterName $paramName -ParameterType System.Text.Encoding
}

It "Get-Command `" `" does not throw" -Pending {
Get-Command " " | Should -Not -Throw

@lordmilko lordmilko Jul 19, 2019 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the expected behavior that it should not throw, or that it should throw in a specific way? (i.e. CommandNotFoundException).

Get-Command "`n"

throws CommandNotFoundException, so if a different type of whitespace results in nothing that would seem inconsistent?

@KirkMunro Kirk Munro (KirkMunro) Jul 19, 2019 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that it is possible to create an alias that contains a single whitespace (see #10165), Get-Command ' ' should return that command if it exists, or fail with a CommandNotFoundException if it does not.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guys,, do you really use whitespaces commands in your scripts and consider it a great practice?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stumbled upon this due to a bug in my own script; when a user enters invalid input PowerShell should respond appropriately so that the user knows what they did wrong.

CommandNotFoundException because space is not a valid command immediately tells you what your problem is and what you should do to fix it; IndexOutOfRangeException doesn't mean anything and you have to go digging through your script to find the non-obvious reason your script crashed. Failure to throw the right exception would also cause issues if you had an outer exception handler for CommandNotFoundException due to some requirement of your program

While I don't have any thoughts as to whether the behavior described in #10165 should be allowed, I would present that the resolution of #10165 issue should not impact the resolution of #10148; a command either exists or it doesn't - CommandSearcher should be able to process any valid string without crashing in an unexpected way

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guys,, do you really use whitespaces commands in your scripts and consider it a great practice?

Of course not.

That doesn't change the fact that it can be done though, and I'm still with lordmilko: the error should be CommandNotFoundException if the command is not found.

}
}