Skip to content

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

Merged

Conversation

bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented Mar 10, 2018

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 PR NA.

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • User facing documentation needed
  • Change is not breaking
  • Make sure you've added a new test if existing tests do not effectively test the code changed
  • This PR is ready to merge and is not work in progress. It is ready but I would like to wait a few days if there is any community feedback.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready

Copy link
Contributor

@JamesWTruher JamesWTruher left a 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?

@bergmeister
Copy link
Collaborator Author

bergmeister commented Mar 17, 2018

Good point, a similar case is service. I can handle that case. I think we should not warn in this case and assume that the user knows what he/she is doing. I also added a test for it and tested it locally using an Ubuntu 16.04 (LTS) VM. I used date in the test because a quick search revealed that the service binary is not installed by default on some mac distros.

@@ -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) {
Copy link
Contributor

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))

Copy link
Collaborator Author

@bergmeister bergmeister Mar 23, 2018

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.

suggestedCorrections: GetCorrectionExtent(cmdAst, cmdletNameIfCommandNameWasAlias));
}

var isNativeCommand = Helper.Instance.GetCommandInfo(commandName) != null;
Copy link
Contributor

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)

Copy link
Collaborator Author

@bergmeister bergmeister Mar 23, 2018

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.

@bergmeister bergmeister changed the title Warn when 'Get-' prefix was omitted. [WIP] Warn when 'Get-' prefix was omitted. Mar 23, 2018
…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
@bergmeister bergmeister changed the title [WIP] Warn when 'Get-' prefix was omitted. Warn when 'Get-' prefix was omitted. Mar 26, 2018
.AddParameter("Name", cmdName)
.AddParameter("ErrorAction", "SilentlyContinue");

if(commandType!=null)
Copy link
Contributor

Choose a reason for hiding this comment

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

yay!

Copy link
Contributor

@JamesWTruher JamesWTruher left a comment

Choose a reason for hiding this comment

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

LGTM

@bergmeister bergmeister merged commit a503078 into PowerShell:development Mar 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants