8000 Write proper error messages for Get-Command ' ' by jakekerr · Pull Request #13564 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@jakekerr
Copy link
Contributor
@jakekerr jakekerr commented Sep 2, 2020

PR Summary

Fix #13342. Checks if _commandName is empty or null in the DoPowerShellRelativePathLookup() method. Removes a Dbg.Assert() that checks if the name is empty or null. The assertion fails because _commandName is trimmed in setupPathSearcher().

PR Context

After this change Get-Command ' ' will return a CommandNotFoundException instead of a IndexOutOfRangeException. The Dbg.Assert was removed because according to Issue #10165 whitespace should be a valid argument for Get-Command.

PR Checklist

@ghost ghost assigned adityapatwardhan Sep 2, 2020
Copy link
Member
@SteveL-MSFT SteveL-MSFT left a comment

Choose a reason for hiding this comment

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

Please add test cases

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 3, 2020
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Sep 4, 2020
@ghost
Copy link
ghost commented Sep 4, 2020

CLA assistant check
All CLA requirements met.

@jakekerr
Copy link
Contributor Author
jakekerr commented Sep 4, 2020

I see now that an alternate and maybe cleaner solution would involve changing only this line _commandName = _commandName.TrimEnd(Utils.Separators.PathSearchTrimEnd); to create a new variable which is only used for the setupPathSearcher() routine. Creating a new variable for path searching may be more desirable than altering the existing _commandName string. If _commandName doesn't get trimmed we don't ever have to worry about the IndexOutOfRangeException being thrown.

// Home Path: "~\command.exe"
// Drive Relative Path: "\Users\User\AppData\Local\Temp\command.exe"
if (_commandName[0] == '.' || _commandName[0] == '~' || _commandName[0] == '\\')
if (!string.IsNullOrEmpty(_commandName) && (_commandName[0] == '.' || _commandName[0] == '~' || _commandName[0] == '\\'))
Copy link
Collaborator
@iSazonov iSazonov Sep 4, 2020

Choose a reason for hiding this comment

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

_commandName is defined as non-nullable so we can do not check null.
Also it is on a hot path and we could optimize the code a bit. I suggest to add && _commandName.Length != 0 in if above, define char firstChar = _commandName[0] and use the firstChar in the line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iSazonov just to confirm. Is this along the lines of what you're suggesting?
char firstChar = _commandName[0];
if (_commandName.Length != 0 && (firstChar == '.' || firstChar == '~' || firstChar == '\\'))

I agree it would be more optimized but the issue of this PR (Get-Command ' ' returns the wrong exception) wouldn't be fixed. Sorry I may be misunderstanding. I'm still learning.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(!string.IsNullOrEmpty(_commandName)

You added the check to fix the issue. But _commandName is annotated as non-nullable and we can exclude null check and have _commandName.Length != 0. And the check should be in previous if condition (before char firstChar = _commandName[0]).

Comment on lines 105 to 107
It "Returns CommandNotFoundException if a single space is used" {
{Get-Command ' ' -ErrorAction Stop} | Should -Throw -ErrorId "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

For consistency we could add tests for null and empty arguments. Please use -TestCases.

@iSazonov
Copy link
Collaborator
iSazonov commented Sep 4, 2020

Creating a new variable for path searching may be more desirable than altering the existing _commandName string.

I think it makes no sense to create new variable - this complicates code and don't protect underlying internal methods.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Sep 4, 2020
Comment on lines 1103 to 1104
//char firstChar = _commandName[0];
if (_commandName.Length != 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove commented code and move the condition in line 1104 to line 1090.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah thanks. Can't believe I missed that.

@jakekerr jakekerr requested a review from SteveL-MSFT September 7, 2020 03:24
@jakekerr
Copy link
Contributor Author

@SteveL-MSFT what do you think of the added test cases? Thanks.

@ghost ghost added the Review - Needed The PR is being reviewed label Sep 22, 2020
@ghost
Copy link
ghost commented Sep 22, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@jakekerr
Copy link
Contributor Author
jakekerr commented Nov 2, 2020

@adityapatwardhan I don't mean to bother you but I just wanted to know if there's anything else I can do to help get this PR approved? Thanks.

@adityapatwardhan
Copy link
Member

Currently working on some high priority items. I will be reviewing this after. Thank you for your patience.

@ghost ghost removed the Review - Needed The PR is being reviewed label Nov 2, 2020
@ghost ghost added the Review - Needed The PR is being reviewed label Nov 10, 2020
@ghost
Copy link
ghost commented Nov 10, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@adityapatwardhan
Copy link
8000
Member

@SteveL-MSFT can you update your review

@ghost ghost removed the Review - Needed The PR is being reviewed label Nov 13, 2020
@ghost ghost added the Review - Needed The PR is being reviewed label Nov 21, 2020
@ghost
Copy link
ghost commented Nov 21, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@iSazonov iSazonov merged commit d3f7ba9 into PowerShell:master Jan 10, 2021
@ghost ghost removed the Review - Needed The PR is being reviewed label Jan 10, 2021
@iSazonov iSazonov added this to the 7.2.0-preview.3 milestone Jan 10, 2021
@iSazonov
Copy link
Collaborator

@jakekerr Thanks for your contribution!< 4E30 /p>

@ghost
Copy link
ghost commented Feb 12, 2021

🎉v7.2.0-preview.3 has been released which incorporates this pull request.:tada:

Handy links:

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 fails with unexpected error when given a blank command-name argument

4 participants

0