PowerShell / PowerShell/PSScriptAnalyzer

PSUseDeclaredVarsMoreThanAssignments is not aware of the scriptblock context (invocation or dot-sourcing)

Open
#938 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - PSUseDeclaredVarsMoreThanAssignments Area - Rules
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Steps to reproduce

Extracted from here but similar issues also reported in #711

It’s a PSSA bug. I get the same warning with:

$totalSize = 0
. { $totalSize += 1 }
$totalSize

PSSA is apparently analyzing each script block independently.

It needs to recognize when a script block is dot sourced and analyze it in the context of where it is >invoked. This isn’t always possible, but it’s usually easy to do when using ForEach-Object (which dot >sources like this).

In trying some variants on the idea, it seems like there are multiple bugs:

$totalSize = 0
function foo {
$x = $totalSize + 1
$totalSize = $x # Should warn, doesn't (bug)
}
& {
$x = $totalSize + 1
$totalSize = $x # Should warn, doesn't (bug)
}
. {
$x = $totalSize + 1
$totalSize = $x # Should not warn, doesn't (no bug)
}
. {
$totalSize += 1 # Should not warn, does (bug)
}
$totalSize

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the PSUseDeclaredVarsMoreThanAssignments implementation and reproduce the invocation, dot-sourcing, function, and scriptblock examples in the issue. Compare each diagnostic with the expected behavior described in the comments; done means the analyzer handles these contexts without false positives or missed warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.