Skip to content

Transient RULE_ERROR #1351

Description

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 *

Activity

  1. ElinoraWhite commented on Nov 17, 2019

    @ElinoraWhite

    Chris Kuech (@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

  2. chriskuech commented on Dec 9, 2019

    @chriskuech
    Author

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

    @BernieWhite , can you say the same?

  3. chriskuech commented on Dec 9, 2019

    @chriskuech
    Author

    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.

  4. ElinoraWhite commented on Dec 10, 2019

    @ElinoraWhite

    Chris Kuech (@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.

  5. bergmeister commented on Dec 10, 2019

    @bergmeister
    Collaborator

    Chris Kuech (@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.

  6. chriskuech commented on Dec 13, 2019

    @chriskuech
    Author

    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()
    
  7. Pxtl commented on Aug 9, 2024

    @Pxtl

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

  8. bradbanister commented on Jul 3, 2026

    @bradbanister

    I can reproduce this on:

    • PowerShell: 7.4.10
    • PSEdition: Core
    • OS: Ubuntu 24.04.4 LTS
    • PSScriptAnalyzer: 1.25.0

    Minimal repro:

    module.psm1

    function Get-Foo { }
    
    Export-ModuleMember -Function *

    Run repeatedly in fresh pwsh processes:

    tmpdir=$(mktemp -d)
    printf 'function Get-Foo { }\n\nExport-ModuleMember -Function *\n' > "$tmpdir/module.psm1"
    
    for i in $(seq 1 100); do
      PSSA_TMP="$tmpdir" pwsh -NoProfile -Command '
        $ErrorActionPreference = "Stop"
        Invoke-ScriptAnalyzer -Path $env:PSSA_TMP -Recurse -ErrorAction Stop | Out-Null
      ' || break
    done

    On my machine this failed by attempt 10 with:

    System.NullReferenceException: Object reference not set to an instance of an object.
       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.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass84_1.<AnalyzeSyntaxTree>b__2()
    

    Removing Export-ModuleMember from the same minimal module ran 100 fresh attempts cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions