-
Notifications
You must be signed in to change notification settings - Fork 393
Warn when 'Get-' prefix was omitted. #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn when 'Get-' prefix was omitted. #927
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to be sure that we don't create false positives when the native command exists. date
is the command i'm thinking of which exists properly on a unix system. do we want to warn in this case?
…. 'date' or service' on Unix systems)
… command takes precedence. Tested on Ubuntu 16.04 LTS
Good point, a similar case is |
@@ -95,5 +100,10 @@ Configuration MyDscConfiguration { | |||
$violations = Invoke-ScriptAnalyzer -ScriptDefinition "CD" -Settings $settings -IncludeRule $violationName | |||
$violations.Count | Should -Be 0 | |||
} | |||
|
|||
It "do not warn when about Get-* completed cmdlets when the command exists natively on Unix platforms" -skip:(!$IsLinux -and !$IsmacOS) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a bit confusing - is this better?
-skip:( ! ( $IsLinux -or $IsMacOS))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me both versions are of similar complexity, therefore I don't have a strong opinion on what it should be. I will change it to your version then.
Rules/AvoidAlias.cs
Outdated
suggestedCorrections: GetCorrectionExtent(cmdAst, cmdletNameIfCommandNameWasAlias)); | ||
} | ||
|
||
var isNativeCommand = Helper.Instance.GetCommandInfo(commandName) != null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't necessarily a native command, but it's anything that will be returned by Get-Command
. I was thinking that this was a bit open and wanted to be more specific about native apps, and there is an overload for GetCommandInfo which takes a CommandType, which would be perfect, because we could do this:
var isNativeCommand = Helper.Instance.GetCommandInfo(commandName, (CommandTypes.Application|CommandTypes.ExternalScript))
unfortunately, the internal method doesn't actually use this parameter if it's passed in which is a bug - grrrr
The reason I'm worried is that we may be still casting too broad a net here, and we'll get more false positives as the call as written will find everything named 'date' including scripts/functions/etc.
The long and short of it is there's no way to return just the native apps with the bug in the engine, so this may generate false positives. So the name of the variable is not what it purports to be (a native command)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it turns out that lots of other code seems to be indirectly relying on this bug and other things start to break if I were to fix it. I fixed therefore only the GetCommandInfoInternal
method to actually use the CommandTypes
argument and declared the old GetCommandInfo
method as legacy to not break existing behaviour. I think this bad behaviour is mainly due to the commandInfo cache, which is one of too many shared static members that are indirectly being modified. I re-ran the tests locally on an Ubuntu 16 VM.
…gument However, its calling method GetCommandInfo was relying on this bug (or its callers), therefore the method was declared legacy in order to not break existing behaviour
…nalyzer into WarnWhenGetPrefixWasOmitted
.AddParameter("Name", cmdName) | ||
.AddParameter("ErrorAction", "SilentlyContinue"); | ||
|
||
if(commandType!=null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yay!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR Summary
Fixes #926
Warn when 'Get-' prefix was omitted. . Rename some variables to make the code clearer
The rule
PSAvoidUsingCmdletAliases
is used for this case to keep things simple.PR Checklist
Note: Tick the boxes below that apply to this pull request by putting an
x
between the square brackets. Please mark anything not applicable to this PRNA
.WIP:
to the beginning of the title and remove the prefix when the PR is ready