PowerShell / PowerShell/PSScriptAnalyzer

PSAvoidAssignmentToAutomaticVariable generates false positives during script analysis

Đang mở
#1,532 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Area - Rules Issue - Bug Need Repro Info
Ngôn ngữ chính
C#
Star
2.2k
Fork
414
Merge trung bình
13 giờ 1 phút
Pull request đã merge (30 ngày)
2

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy hàm Write-Log được cung cấp thông qua PSScriptAnalyzer và kiểm tra quy tắc PSAvoidAssignmentToAutomaticVariable báo cáo tham số $Error. Xác nhận chẩn đoán dựa trên hành vi phạm vi của PowerShell đã nêu; được xem là hoàn tất khi trường hợp này không được báo cáo và văn bản chẩn đoán được sửa nếu nó vẫn còn áp dụng ở nơi khác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
csharp, powershell
Lĩnh vực
tooling
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.