Skip to content

System.NullReferenceException when using Invoke-ScriptAnalyzer #1181

Closed
@adityapatwardhan

Description

@adityapatwardhan

Before submitting a bug report:

  • Make sure you are able to repro it on the latest released version
  • Perform a quick search for existing issues to check if this bug has already been reported

Steps to reproduce

Execute Invoke-ScriptAnalyzer on file which has:

enum LogLevel
{
    Error
    Information
    Verbose
    Debug
}

class Logger
{
    static [Logger] $instance
}

function Write-Log
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)] [LogLevel] $LogLevel,
        [Parameter(Mandatory = $true, Position = 1)] [string] $Message,
        [Parameter()][switch] $NewLog
    )

    Enable-Logger
    $script:logger.WriteLog($LogLevel, $Message, $NewLog.IsPresent)
}

Expected behavior

no NullReferenceException

Actual behavior

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.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
                           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 System.Management.Automation.Language.InvokeMemberExpressionAst.Accept(ICustomAstVisitor visitor)
                           at System.Management.Automation.Language.Ast.Visit(ICustomAstVisitor astVisitor)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitCommandExpression(CommandExpressionAst commandAst)
                           at System.Management.Automation.Language.CommandExpressionAst.Accept(ICustomAstVisitor visitor)
                           at System.Management.Automation.Language.Ast.Visit(ICustomAstVisitor astVisitor)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitPipeline(PipelineAst pipelineAst)
                           at System.Management.Automation.Language.PipelineAst.Accept(ICustomAstVisitor visitor)
                           at System.Management.Automation.Language.Ast.Visit(ICustomAstVisitor astVisitor)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitNamedBlock(NamedBlockAst namedBlockAst)
                           at System.Management.Automation.Language.NamedBlockAst.Accept(ICustomAstVisitor visitor)
                           at System.Management.Automation.Language.Ast.Visit(ICustomAstVisitor astVisitor)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.FindPipelineOutput.VisitScriptBlock(ScriptBlockAst scriptBlockAst)
                           at System.Management.Automation.Language.ScriptBlockAst.Accept(ICustomAstVisitor visitor)
                           at System.Management.Automation.Language.Ast.Visit(ICustomAstVisitor astVisitor)
                           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 System.Management.Automation.Language.Ast.Visit(AstVisitor astVisitor)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseOutputTypeCorrectly.AnalyzeScript(Ast ast, String fileName)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass82_1.<AnalyzeSyntaxTree>b__2()

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.3
PSEdition                      Core
GitCommitId                    6.1.3
OS                             Microsoft Windows 10.0.18860
Platform                       Win32NT
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
1.16.1

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