Skip to content

Invoke-Formatter PSAlignAssignmentStatement Rule errors when a Property Name is specified as an Object Property #1003

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

Closed
X-Guardian opened this issue May 29, 2018 · 5 comments

Comments

@X-Guardian
Copy link

If Invoke-Formatter with the PSAlignAssignmentStatementRule enabled is run against a hashtable that contains a key name specified as an object.property, a NullReferenceException error occurs.

Steps to reproduce

$scriptDefinition = @'
$Hashtable = @{
KeyName = 'test1'
$Object.Property = 'test2'
}
'@

$settings = @{
    IncludeRules = @("PSAlignAssignmentStatement")
    Rules = @{
        PSAlignAssignmentStatement = @{
            Enable = $true
        }
    }
}

Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings

Expected behavior

Return the hashtable with the assignment statements aligned:

$Hashtable = @{
KeyName          = 'test1'
$Object.Property = 'test2'
}

Actual behavior

Invoke-Formatter : Object reference not set to an instance of an object.
At Test-Formatter.ps1:17 char:1
+ Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $setti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
    + FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand

$Hashtable = @{
KeyName = 'test1'
$Object.Property = 'test2'
}

Error Object

$error[0] | Select-Object *
writeErrorStream      : True
PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AlignAssignmentStatement.HasPropertie
                        sOnSeparateLines(IEnumerable`1 tuples)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AlignAssignmentStatement.<FindHashtab
                        leViolations>d__15.MoveNext()
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AlignAssignmentStatement.<AnalyzeScri
                        pt>d__7.MoveNext()
                           at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                           at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass78_0.<AnalyzeSynta
                        xTree>b__1()
TargetObject          :
CategoryInfo          : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, \\nas01\data\users\simon\documents\WindowsPowershell\Test-Formatter.ps1: line 16
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.17134.48
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.48
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.16.1
@bergmeister
Copy link
Collaborator

bergmeister commented May 29, 2018

@X-Guardian Thanks for the detailed report. I just tried it and can confirm that this happens with 1.16.1 but not the latest version of the development branch and I believe this is the PR that fixed the problem that you saw. In the next days, 1.17 will be released where this should not happen any more. :-)
In the meantime you could build it yourself, if you need an urgent fix. Today I also wrote a blog post here on how to build it yourself (but the Readme has enough instructions as well). I will leave the issue for the moment for you to confirm the fix as well once the release is out if that is OK with you?

@X-Guardian
Copy link
Author

Thanks for the quick response @bergmeister. Glad to hear it is already fixed in the next release. It is actually affecting me within the VSCode PowerShell extension. Will I need to wait until the 1.17 PSScriptAnalyzer release is bundled in with vscode-powershell, or will it use a separately installed version?

@bergmeister
Copy link
Collaborator

bergmeister commented May 29, 2018

The newer versions of the extension do not ship with PSSA itself any more (already a few months ago) and just use the locally installed version, therefore you will only need to update your module and vscode will start using it internally as well.

@bergmeister
Copy link
Collaborator

bergmeister commented Jun 1, 2018

@X-Guardian PSSA 1.17.0 has been released today on the PSGallery (and attached as a zip to the release notes on GitHub). When using Install-Module, one has to specify -SkipPublisherCheck though due to signing problems with the Gallery. Can you confirm that this is fixed please?

@X-Guardian
Copy link
Author

Thanks @bergmeister. Yes, I can confirm that this issue is fixed in the 1.17.0 release and VSCode is successfully using it too.

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

2 participants