Skip to content
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

Add warnings about read-only automatic variables introduced in PowerShell 6.0 #917

Merged

Conversation

bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented Mar 3, 2018

PR Summary

Related issue (but not sufficient to close it): #858
The variables IsCoreCLR, IsLinux, IsMacOS, IsWindows were introduced only in PowerShell 6.0 as read-only, automatic variables and attempting to assign to them will result in an error being thrown at runtime.
Therefore warn against using it but reduce severity from Error to Warning for those variables since they do not affect Windows PowerShell, an appropriate message has been added with this additional information

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
    • 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.

I wonder if this should be an error on Core and a warning on non-Core?

@bergmeister
Copy link
Collaborator Author

Good idea, certainly a nice-to-have, but I've implemented it now.


[System.Array] $warnings = Invoke-ScriptAnalyzer -ScriptDefinition "function foo(`$$VariableName){}" | Where-Object {$_.RuleName -eq $ruleName }
$warnings.Count | Should -Be 1
$warnings.Severity | Should -Be $readOnlyVariableSeverity
$warnings.Severity | Should -Be $ExpectedSeverity
Copy link
Contributor

Choose a reason for hiding this comment

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

I see the above pattern a lot, but it worries me. I would think that the Where-Object clause would obfuscate additional unexpected errors. Shouldn't we know the total amount of errors we expect and not have to filter for only what we expect?

Copy link
Collaborator Author

@bergmeister bergmeister Mar 20, 2018

Choose a reason for hiding this comment

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

There is no 'right' answer, I think. There is the approach of having either isolated unit tests (with the benefit of lower maintenance) or doing integration testing as part of the test. We are still running all rules against it, so we would probably still get a test failure if one rule unexpectedly threw an exception but we would not have to adapt tests too much if rules changed. I will leave this decision up to you and will therefore remove the Where-Object and replace it with $warnings.RuleName | Should -Be $ruleName'. In one test case I will use -ExcludeRule PSUseDeclaredVarsMoreThanAssignments`

@JamesWTruher
Copy link
Contributor

@bergmeister we've got merge conflicts at this point - could you resolve and push?

…nalyzer into WarnReadOnlyVarPsCore

Resolved by taking both
# Conflicts:
#	Rules/Strings.resx
@JamesWTruher JamesWTruher merged commit 7c1ca74 into PowerShell:development Apr 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants