Skip to content

Get-Command should not throw with white space argument - #10157

Closed
Ilya (iSazonov) wants to merge 5 commits into
PowerShell:masterfrom
iSazonov:fix-getcommand-crash-empty-argument
Closed

Ilya (iSazonov) wants to merge 5 commits into
PowerShell:masterfrom
iSazonov:fix-getcommand-crash-empty-argument

Conversation

@iSazonov

@iSazonov Ilya (iSazonov) commented Jul 15, 2019 •

Copy link
Copy Markdown
Collaborator

PR Summary

Fix #10148

  • Skip whitespace arguments
  • Remove unneeded collection allocation

PR Context

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jul 15, 2019
@iSazonov Ilya (iSazonov) added this to the 7.0.0-preview.2 milestone Jul 15, 2019
@iSazonov Ilya (iSazonov) self-assigned this Jul 15, 2019

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One minor nit for readability, but otherwise looks great! 👍

Co-Authored-By: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com>
@KirkMunro

Copy link
Copy Markdown
Contributor

Maybe related: #10165. Just mentioning because there are broader needs when it comes to whitespace in parameters that may be worth considering.

}

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.

@iSazonov

Copy link
Copy Markdown
Collaborator Author

We need a conclution in related issue before we could fix something

@iSazonov
Ilya (iSazonov) deleted the fix-getcommand-crash-empty-argument branch October 2, 2019 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get-Command " " Crashes with IndexOutOfRangeException

6 participants