Skip to content

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

Closed

Description

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.

Activity

  1. kapilmb commented on May 20, 2017

    @kapilmb

    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()
  2. johlju commented on May 20, 2017

    @johlju
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions