PowerShell / PowerShell/PSScriptAnalyzer

PSAvoidAssignmentToAutomaticVariable generates false positives during script analysis

オープン
#1,532 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Area - Rules Issue - Bug Need Repro Info
主要言語
C#
スター
2.2k
フォーク
414
平均マージ
13時間 1分
マージ済み PR(30日)
2

説明

Steps to reproduce

Run this function through PSSA.

function Write-Log {
    [CmdletBinding()]
    param
    (
        [Parameter(Position = 0, Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Message,

        [switch]
        $Error
    )
    $callerEAP = $ErrorActionPreference
    try {
        $messageColor = if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Error') -and $Error) {
            [ConsoleColor]::Red
        } else {
            [ConsoleColor]::Cyan
        }

        Write-Host -Foreground $messageColor -Object $Message

        # Reset colors to return to default after an error message on a compilation error
        [System.Console]::ResetColor()
    } catch {
        Write-Error -ErrorRecord $_ -ErrorAction $callerEAP
    }
}

Expected behavior

It passes.

Actual behavior

PSSA reports the following:

The Variable 'Error' cannot be assigned since it is a readonly automatic variable that is built into PowerShell, please use a different name.

There are a number of problems with this:

  1. The comma should be a period for it to read properly.
  2. The error text is incorrect. $Error can be assigned in child scopes without issue. $Error is read-only only in the global scope, and errors are automatically written into that globally-scoped collection that is read-only from PowerShell. In functions, or child scopes, you can use a $Error variable to your hearts content.
  3. Most importantly: the issue that is being called out by PSSA here should not be called out at all.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.0.1
PSEdition                      Core
GitCommitId                    7.0.1
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.0

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された Write-Log 関数を PSScriptAnalyzer にかけ、$Error パラメーターを報告する PSAvoidAssignmentToAutomaticVariable ルールを調査します。示されている PowerShell のスコープ動作に照らして診断を確認します。このケースが報告されず、他の場所では引き続き該当する場合は診断テキストが修正されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
csharp, powershell
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。