Skip to content

PSUseOutputTypeCorrectly NullReference when using $PSCmdlet.WriteInformation #1223

Closed
@Jaykul

Description

@Jaykul

This is related to #602 but complicated to reproduce:

  1. If you have a PowerShell class.
  2. And you call PSCmdlet.WriteInformation

Then PSUseOutputTypeCorrectly will throw a null reference exception -- presumably trying to check the output type of the [void] method?

Steps to reproduce

Invoke-ScriptAnalyzer -ScriptDefinition '
class Thing {}

function Write-Info {
[CmdletBinding()]
param() 

    $PSCmdlet.WriteInformation( "Hello", "Tag" ) 
}' -IncludeRule PSUseOutputTypeCorrectly

Expected behavior

It should be exactly the same as when I write it this way:

Invoke-ScriptAnalyzer -ScriptDefinition '
class Thing {}

function Write-Info {
[CmdletBinding()]
param() 

    [void]$PSCmdlet.WriteInformation( "Hello", "Tag" ) 
}'

Actual behavior

It throws an exception, terminating analysis -- meaning I get NO RESULTS AT ALL FOR ANY RULES.

Invoke-ScriptAnalyzer : Object reference not set to an instance of an object.
$error[0,1] | Select-Object *
ErrorRecord                 : Object reference not set to an instance of an object.
WasThrownFromThrowStatement : False
Message                     : The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Object reference not set to an instance of an object.
Data                        : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException              :
TargetSite                  : System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject] Invoke(System.Collections.IEnumerable)
StackTrace                  :    at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
                                 at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, Exception& exceptionThrown, ExecutionOptions options)
HelpLink                    :
Source                      : System.Management.Automation
HResult                     : -2146233087

PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.<>c__DisplayClass61_0.<GetTypeFromMemberExpressionAst>b__0(TypeDefinitionAst item)
                           at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast scopeAst, IEnumerable`1 classes)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitInvokeMemberExpression(InvokeMemberExpressionAst invokeAst)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitCommandExpression(CommandExpressionAst commandAst)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitPipeline(PipelineAst pipelineAst)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitNamedBlock(NamedBlockAst namedBlockAst)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitScriptBlock(ScriptBlockAst scriptBlockAst)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput..ctor(FunctionDefinitionAst ast, IEnumerable`1 classes)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.OutputTypes(FunctionDefinitionAst funcAst, IEnumerable`1 classes)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseOutputTypeCorrectly.VisitFunctionDefinition(FunctionDefinitionAst funcAst)
                           at System.Management.Automation.Language.FunctionDefinitionAst.InternalVisit(AstVisitor visitor)
                           at System.Management.Automation.Language.StatementBlockAst.InternalVisit(AstVisitor visitor, ReadOnlyCollection`1 traps, ReadOnlyCollection`1 statements, AstVisitAction action)
                           at System.Management.Automation.Language.NamedBlockAst.InternalVisit(AstVisitor visitor)
                           at System.Management.Automation.Language.ScriptBlockAst.InternalVisit(AstVisitor visitor)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseOutputTypeCorrectly.AnalyzeScript(Ast ast, String fileName)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass83_1.<AnalyzeSyntaxTree>b__2()
TargetObject          :
CategoryInfo          : InvalidOperation: (:) [Invoke-ScriptAnalyzer], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Environment data

> $PSVersionTable

Name                           Value                                                                                                                                                                                                                                                                                        
----                           -----                                                                                                                                                                                                                                                                                        
PSVersion                      5.1.18362.1                                                                                                                                                                                                                                                                                  
PSEdition                      Desktop                                                                                                                                                                                                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                                                                                      
BuildVersion                   10.0.18362.1                                                                                                                                                                                                                                                                                 
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                                                                              
WSManStackVersion              3.0                                                                                                                                                                                                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                                                                                          
SerializationVersion           1.1.0.1                                                                                                                                                                                                                                                                                      



> (Get-Module PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions