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

PSSA: Variable is reported never assigned, but it is #772

Closed
johlju opened this issue May 20, 2017 · 2 comments
Closed

PSSA: Variable is reported never assigned, but it is #772

johlju opened this issue May 20, 2017 · 2 comments

Comments

@johlju
Copy link

johlju commented May 20, 2017

System Details

  • Operating system name and version:
  • VS Code version:
  • PowerShell extension version:
  • Output from $PSVersionTable:
PS > code -v
1.12.2
19222cdc84ce72202478ba1cec5cb557b71163de
PS > $pseditor.EditorServicesVersion
PS > code --list-extensions --show-versions
DavidAnson.vscode-markdownlint@0.7.2
donjayamanne.githistory@0.2.0
eamodio.gitlens@3.4.9
ms-vscode.csharp@1.9.0
ms-vscode.PowerShell@1.1.0
PeterJausovec.vscode-docker@0.0.14
ryu1kn.annotator@0.10.1
streetsidesoftware.code-spell-checker@1.1.0
Tyriar.theme-sapphire@0.2.1
PS > $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.296
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.296
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

I got variables saying they are not assigned... but they are. If thinking of $testParameters. Those variables starting with $mockDynamic* I can understand that they are reporting this, because they are used in a "dynamic script block" for the Mock (not visible in the screen shot).

image

After reloading VSCode all the PSSA rule warnings for the variables disappeared. So I guess I have to report back when I see it again. Submitting here if someone else encounters the same problem.

Attached Logs

Follow the instructions in the README about capturing and sending logs.

I send logs next time I see this.

@kapilmb
Copy link

kapilmb commented May 20, 2017

Pester dot sources the script block given to BeforeEach in the corresponding Context scope. However, there is no way for PSSA to know about this dot-sourcing operation just through static analysis. Hence, unfortunately, the only way around this issue right now is to suppress the violations. More information about suppression can be found here. Due to the limitation of how suppression is implemented, the only way you can suppress these violations in a pester test file is to place an empty param block at the beginning of the test file, and precede it with a suppression attribute. For example, the following will suppress all the PSUseDeclaredVarsMoreThanAssignments violations in the file, including the violations that you have pointed out.

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
param()

@johlju
Copy link
Author

johlju commented May 20, 2017

Yes, that makes sense. I have never seen those warnings before when coding in VS Code. It's after this latest version so I figured something was wrong.
But then we file this under "by design" and use that workaround if it gets too annoying 😄

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants