Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transient RULE_ERROR #1351

Open
chriskuech opened this issue Oct 17, 2019 · 8 comments
Open

Transient RULE_ERROR #1351

chriskuech opened this issue Oct 17, 2019 · 8 comments

Comments

@chriskuech
Copy link

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
Copy link

BernieWhite commented Nov 17, 2019

@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
Copy link
Author

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

@BernieWhite , can you say the same?

@chriskuech
Copy link
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.

@BernieWhite
Copy link

BernieWhite commented Dec 10, 2019

@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
Copy link
Collaborator

bergmeister commented Dec 10, 2019

@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
Copy link
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()

@rifftual
Copy link

rifftual commented Feb 4, 2021

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
Copy link

Pxtl commented Aug 9, 2024

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

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

No branches or pull requests

6 participants