Skip to content

Test-PSBuildScriptAnalysis never fails the build: $_Severity should be $_.Severity #125

@tablackburn

Description

@tablackburn

Summary

Test-PSBuildScriptAnalysis counts analyzer findings by severity using $_Severity
(an undefined variable) instead of $_.Severity (the property). Every count is
therefore always 0, so the function — and the psake/Invoke-Build Analyze task that
calls it — never throws, regardless of findings. PSScriptAnalyzer enforcement is
silently disabled for every consumer relying on the Analyze task.

Affected code

PowerShellBuild/Public/Test-PSBuildScriptAnalysis.ps1:

$errors   = ($analysisResult.where({ $_Severity -eq 'Error' })).Count
$warnings = ($analysisResult.where({ $_Severity -eq 'Warning' })).Count
$infos    = ($analysisResult.where({ $_Severity -eq 'Information' })).Count

$_Severity is parsed as the variable $_Severity (which does not exist → $null), not
as the Severity property of the pipeline item $_. $null -eq 'Error' is $false for
every record, so .where({...}) always returns an empty collection and .Count is 0.

The downstream gate is then dead code:

if ($errors -gt 0) { throw ... }                       # never true
if ($errors -gt 0 -or $warnings -gt 0) { throw ... }    # never true

Impact

  • Any project whose build relies on the Analyze task believes PSScriptAnalyzer is
    gating its build, when in fact violations of any severity (including Error) pass.
  • Affects both the psake (psakeFile.ps1) and Invoke-Build (IB.tasks.ps1) task wrappers,
    since both call Test-PSBuildScriptAnalysis.

Affected versions

Confirmed present in every published version on the PowerShell Gallery (0.1.0 through
0.8.0, the current latest) and on the main branch. Not version-specific.

Reproduction

$record = [pscustomobject]@{ Severity = 'Error' }
(@($record).Where({ $_Severity  -eq 'Error' })).Count   # => 0  (bug)
(@($record).Where({ $_.Severity -eq 'Error' })).Count   # => 1  (correct)

Or end to end: add a function guaranteed to raise an Error-severity diagnostic, run the
Analyze/Test task, and observe that the build still succeeds.

Suggested fix

Reference the property, not a variable:

$errors   = ($analysisResult.where({ $_.Severity -eq 'Error' })).Count
$warnings = ($analysisResult.where({ $_.Severity -eq 'Warning' })).Count
$infos    = ($analysisResult.where({ $_.Severity -eq 'Information' })).Count

Related

  • Tests: Test-PSBuildScriptAnalysis #96 (adding unit tests for Test-PSBuildScriptAnalysis): a test that feeds the function a
    result set containing an Error record and asserts it throws at SeverityThreshold = 'Error'
    would have caught this and would guard against recurrence.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions