Description
Steps to reproduce
- Create a script with just
Invoke-Pester
-
Install Pester with
Install-Module -Name Pester
-
Then run
pwsh -Command 'Invoke-ScriptAnalyzer -Path test.ps1'
multiple times until you get the error
Expected behavior
Passes with no exception
Actual behavior
It usually succeeds normally but can sometimes fail with the exception
Invoke-ScriptAnalyzer : Unable to find type [Pester.OutputTypes].
At line:1 char:1
+ Invoke-ScriptAnalyzer -Path "/home/jborean/dev/test_script.ps ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (/home/jborean/dev/test_script.ps1:String) [Invoke-ScriptAnalyzer], RuntimeException
+ FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
writeErrorStream : True
PSMessageDetails :
Exception : System.Management.Automation.RuntimeException: Unable to find type [Pester.OutputTypes].
at System.Management.Automation.TypeOps.ResolveTypeName(ITypeName typeName, IScriptExtent errorPos)
at System.Management.Automation.Language.Compiler.GetAttribute(TypeConstraintAst typeConstraintAst)
at System.Management.Automation.Language.TypeConstraintAst.GetAttribute()
at System.Management.Automation.Language.Compiler.GetRuntimeDefinedParameter(ParameterAst parameterAst, Boolean& customParameterSet, Boolean&
usesCmdletBinding)
at System.Management.Automation.Language.Compiler.GetParameterMetaData(ReadOnlyCollection`1 parameters, Boolean automaticPositions, Boolean&
usesCmdletBinding)
at
System.Management.Automation.Language.FunctionDefinitionAst.System.Management.Automation.Language.IParameterMetadataProvider.GetParameterMetadata(Boolean
automaticPositions, Boolean& usesCmdletBinding)
at System.Management.Automation.CompiledScriptBlockData.InitializeMetadata()
at System.Management.Automation.CompiledScriptBlockData.GetAttributes()
at System.Management.Automation.ScriptBlock.GetAttributes()
at System.Management.Automation.ScriptBlock.get_Attributes()
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseShouldProcessCorrectly.SupportsShouldProcess(String cmdName)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseShouldProcessCorrectly.CheckForSupportShouldProcess()
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseShouldProcessCorrectly.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()
TargetObject : /home/jborean/dev/test_script.ps1
CategoryInfo : InvalidOperation: (/home/jborean/dev/test_script.ps1:String) [Invoke-ScriptAnalyzer], RuntimeException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}
I've found that if I do pwsh -Command 'Import-Module Pester; Invoke-PSScriptAnalyzer -Path ...
I cannot replicate the issue and the Pester types have been fully loaded. What is weird is that this only sometimes occurs which tells me the UseShouldProcessCorrectly
rule is parsing module commands but there's a race condition where the type is not fully loaded by the time it tries to scan the attributes.
Some things I've tried
- Always importing Pester using
Import-Module -Name Pester
before calling PSSA, no more error but I don't want to do this for all modules in the code - Reinstalled both Pester and PSScriptAnalyzer, still occassionally fails
- Use an older version of PSSA (1.17.1), works fine
My guess is that because this is an error only in the new version, the changes in #1074 or #1166 could be the cause.
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 6.2.0
PSEdition Core
GitCommitId 6.2.0
OS Linux 5.0.7-arch1-1-ARCH #1 SMP PREEMPT Mon Apr 8 10:37:08 UTC 2019
Platform Unix
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.18.0
> (Get-Module -ListAvailable Pester).Version | ForEach-Object { $_.ToString() }
4.7.3