Open chriskuech opened 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
I notice this has only happened to us when importing *.psm1 files that contain Export-ModuleMember
.
@BernieWhite , can you say the same?
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.
@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
.
@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.
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()
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
Have this bug. Running PSScriptAnalyzer 1.22 directly on my AzDO build agent server in PS 5.1 on Windows 10.
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:
Contents of powershell-helpers.psm1: