PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.84k stars 372 forks source link

Transient RULE_ERROR #1351

Open chriskuech opened 4 years ago

chriskuech commented 4 years ago

I wish I could provide more information, but the error is transient and therefore cannot be deterministically reproed.

PowerShell version: 6.2.1 PSScriptAnalyzer version: 1.18.3 OS Version: Ubuntu 1604

PSScriptAnalyzer is failing occasionally with the following error:

Invoke-ScriptAnalyzer : Object reference not set to an instance of an object.
At /source/.build/Assert-ValidStyle.ps1:21 char:11
+ $issues = Invoke-ScriptAnalyzer -Path "$PSScriptRoot\.." -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (/source/.build/powershell-helpers.psm1:String) [Invoke-ScriptAnalyzer], NullReferenceException
+ FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand

Contents of powershell-helpers.psm1:

# execute a command block and display stderr output without throwing powershell exceptions
# Taken from https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right
function Invoke-Script {
    [CmdletBinding()]
    [Alias('exec')]
    param
    (
        [ScriptBlock] $ScriptBlock,
        [string] $StderrPrefix = "",
        [int[]] $AllowedExitCodes = @(0)
    )

    $backupErrorActionPreference = $script:ErrorActionPreference

    $script:ErrorActionPreference = "Continue"
    try {
        & $ScriptBlock *>&1 | ForEach-Object -Process `
        {
            if ($_ -is [System.Management.Automation.ErrorRecord]) {
                "$StderrPrefix$_"
            }
            else {
                "$_"
            }
        }

        if ($AllowedExitCodes -notcontains $LASTEXITCODE) {
            throw "Execution failed with exit code $LASTEXITCODE"
        }
    }
    finally {
        $script:ErrorActionPreference = $backupErrorActionPreference
    }
}

function Get-File {
    param(
        [string]$Uri,
        [string]$Path,
        [string]$Hash
    )

    New-Item -ItemType Directory -Path (Split-Path $path -Parent) -Force | Out-Null

    Invoke-WebRequest -Uri $uri -OutFile $path

    $actual = Get-FileHash -Path $path -Algorithm SHA256 | Select-Object -ExpandProperty Hash

    if ($actual -ne $hash) {
        throw "File $path with hash $actual doesn't match expected hash $hash"
    }
}

function Invoke-InDirectory {
    Param(
        [ValidateScript( { Test-Path $_ -PathType Container } )]
        [string]$Path,
        [ValidateNotNullOrEmpty()]
        [scriptblock]$ScriptBlock
    )

    Push-Location $Path
    try {
        &$ScriptBlock
    }
    finally {
        Pop-Location
    }
}

Export-ModuleMember -Alias * -Function *
BernieWhite commented 4 years ago

@chriskuech I have also been getting this for a while. It's is flaky.

I have Invoke-PSScriptAnalyzer included in a Azure Pipeline as part of a CI process. Sometimes it will fail/ sometimes not. Even on the same code base (git commit). So often I'll just rerun the build and it works.

I've noticed the issue from v1.18.0 and still getting the issue on v1.18.3 as well.

The issue mostly happens on a MacOS build, however it also happens on Windows with PowerShell Core. Both running 6.2.3. Although I have noticed the issue on previous 6.2.x versions.

I've been holding off logging an issue until I had a little more information. Previously I though it was specific rules. But I managed to get a stack trace. When running locally.

Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
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
PS> $error[0].exception
Object reference not set to an instance of an object.
PS> $error[0].exception.stacktrace
   at System.Management.Automation.CommandInfo.ResolveParameter(String name)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidReservedCharInCmdlet.AnalyzeScript(Ast ast, String fileName)+MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass82_1.<AnalyzeSyntaxTree>b__2()

microsoft/PSRule#299

chriskuech commented 4 years ago

I notice this has only happened to us when importing *.psm1 files that contain Export-ModuleMember.

@BernieWhite , can you say the same?

chriskuech commented 4 years ago

I found that you can repro this issue locally after a long time. I ran it in a loop in a new powershell session and finally reproed it after 25 tries.

1..100 | % {pwsh .\my-psscriptanalyzer-runner.ps1}

I found that after removing all calls to Export-ModuleMember, I no longer see issues even after hundreds of trials.

BernieWhite commented 4 years ago

@chriskuech interesting, I did bulk testing in the same PowerShell process and the issue doesn't predictably reoccur. Can't say on what happens without Export-ModuleMember because I haven't done any testing, but confirming that my module does use Export-ModuleMember.

bergmeister commented 4 years ago

@chriskuech When it happens, can you please also report $error[0].exception.stacktrace. If you have it in your foreach loop, you might have to try to catch it and then report the exception stack trace in the catch block. Also please report the used PowerShell versions as the exception reported by @BernieWhite originates from within PowerShell itself.

chriskuech commented 4 years ago

Here's my $PSVersionTable:

Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
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

I made a minimal repro. When I run the repro, I always eventually get the same error, but not always the same stack trace.

# repro.ps1
for ($i = 1; $true; $i++) {
    Write-Host "`rAttempt $i" -NoNewLine
    pwsh -c 'Invoke-ScriptAnalyzer . -Recurse | Out-Null; if (-not $?) {$Error[0].Exception.StackTrace}'
}

# module.psm1

function f() { }

Export-Module -Function *

These are the two main stack traces I saw:

   at System.Management.Automation.CommandInfo.ResolveParameter(String name)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.ProvideCommentHelp.AnalyzeScript(Ast ast, String fileName)+MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass82_1.<AnalyzeSyntaxTree>b__2()

   at System.Management.Automation.CommandInfo.ResolveParameter(String name)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidReservedCharInCmdlet.AnalyzeScript(Ast ast, String fileName)+MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass82_1.<AnalyzeSyntaxTree>b__2()
rifftual commented 3 years ago

What's the status of this? Still occurring for us.

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.1490
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.1490
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
Pxtl commented 1 month ago

Have this bug. Running PSScriptAnalyzer 1.22 directly on my AzDO build agent server in PS 5.1 on Windows 10.