Repository navigation
NullReferenceException on PSShouldProcess Rule. #1539
Copy link
Copy link
Closed
Labels
Description
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
I'm using PowerShellBuild and my module was made with Stucco.
I haven't changed the template. And the Analyze task was working fine yesterday.
I can't give steps to reproduce yet, working on it. Could I send someone my zipped module ? Not ready to make my code full public.
If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *
PSMessageDetails :
Exception : System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseShouldProcessCorrectly.TryGetShouldProcessValueFromAst(FunctionInfo functionInfo, Boolean& hasShouldProcessSet)
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>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__DisplayClass83_1.<AnalyzeSyntaxTree>b__2()
TargetObject : C:\Users\avezina\gitrepos\etl.devops.serviceManagement\Output\ServiceManagement\0.6.2\Formatting\Deployment.format.ps1
CategoryInfo : InvalidOperation: (C:\Users\avezin...ment.format.ps1:String) [Invoke-ScriptAnalyzer], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at Test-PSBuildScriptAnalysis, C:\Users\avezina\Documents\WindowsPowerShell\Modules\PowerShellBuild\0.4.0\Public\Test-PSBuildScriptAnalysis.ps1: line 31
at <ScriptBlock>, C:\Users\avezina\Documents\WindowsPowerShell\Modules\PowerShellBuild\0.4.0\psakeFile.ps1: line 66
at Invoke-Task, C:\Users\avezina\Documents\WindowsPowerShell\Modules\psake\4.8.0\public\Invoke-Task.ps1: line 108
at <ScriptBlock>, C:\Users\avezina\Documents\WindowsPowerShell\Modules\psake\4.8.0\public\Invoke-psake.ps1: line 300
at ExecuteInBuildFileScope, C:\Users\avezina\Documents\WindowsPowerShell\Modules\psake\4.8.0\private\ExecuteInBuildFileScope.ps1: line 55
at Invoke-psake, C:\Users\avezina\Documents\WindowsPowerShell\Modules\psake\4.8.0\public\Invoke-psake.ps1: line 258
at <ScriptBlock>, C:\Users\avezina\gitrepos\etl.devops.serviceManagement\build.ps1: line 42
at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.1
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1
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.19.0Reactions are currently unavailable
Activity
rjmholt commentedon Jul 7, 2020
From #1538 (comment).
Here's the relevant code:
PSScriptAnalyzer/Rules/UseShouldProcessCorrectly.cs
Lines 344 to 398 in d04c039
avezinaATastus commentedon Jul 7, 2020
After looking at my version control,
EDIT : I wasn't analyzing checked-in code. ignore this comment.
avezinaATastus commentedon Jul 8, 2020
Using 1.18.0, I have better details about the error, I think.
ServerTransformAttribute is a
ArgumentTransformationAttributederived class. I have two other very similar.avezinaATastus commentedon Jul 8, 2020
I tried doing that function
TryGetShouldProcessValueFromAstin powershell, and I did not have a problem.rjmholt commentedon Jul 8, 2020
I suspect the error you're seeing in 1.18 was fixed in #1397.
I would be nice to work out what's causing the NRE here though.
Does this happen consistently, and if so, can you pinpoint a change in your own script that causes it? If you roll back to a particular script change, does this error stop occurring?
avezinaATastus commentedon Jul 8, 2020
Here is the weird thing now :
I went back to my June 29 commit. ( before implementing my three
ArgumentTransformationAttributederived class ([Server | Service | Environment]TransformAttribute) )And I am getting an error using 1.18.0 and 1.19.0
I'm currently not sure if I was always analyzing (but my runs in Azure DevOps seems to) but I will try to check.
The two 'big' changes in my code since the 29th is "PSTypeNames implementation and formatting" and the argument transformation I finished sunday.
rjmholt commentedon Jul 8, 2020
It looks like PowerShell is trying to reconstruct your attribute and is unable to because one of the parameters required to construct it is null. Are you sure you're using your attribute right?
It seems like PSScriptAnalyzer needs to protect itself here -- we should add a try/catch around the
Attributesproperty reference allowing us to just skip the attributes. Because PowerShell throws here when accessing all attributes, it means we can't selectively skip over attributes, but that's just how things are I think.avezinaATastus commentedon Jul 8, 2020
I tried to clean up my PS environment in VSCode. Went back even earlier and uninstalled PSSA 1.18.0
My module : no more ArgumentTransformationAttributes, no more pstypenames.
Made also sure that I don't use
SupportsShouldProcessanywhere (not needed for now)I am back to the NRE.
rjmholt commentedon Jul 8, 2020
The original one that you reported in the description? Does it happen every time you run PSSA?
avezinaATastus commentedon Jul 8, 2020
yes
yes
I can privately send my zipped module, if you'd like.
rjmholt commentedon Jul 8, 2020
Yeah I think that's the right next step. Can you send it through to psscriptanalyzer@microsoft.com
3 remaining items
avezinaATastus commentedon Jul 8, 2020
Multiple ways.
Mostly via my psake task, but also manually.
I could repro by
avezinaATastus commentedon Jul 8, 2020
arghh, didn't want to close.
avezinaATastus commentedon Jul 8, 2020
oh fuck. it is my machine.
Calisse que j'haïs ça.
Just tested in an "almost fresh" windows VM. Installed PSSA@1.19, ran
Invoke-ScriptAnalyzer -Path . -Recurseand it worked.Thanks for helping!
fuck
avezinaATastus commentedon Jul 8, 2020
Now, I gotta find what is causing that.
Could it be that I have some version of my module installed ? EDIT : Nope
avezinaATastus commentedon Jul 8, 2020
Final edit
I had my module still installed. (That damn PS5 bug with the classes)
Removed it, made sure it wasn't installed anymore.
Closed VSCode. Reopened VSCode.
My Analyze task now works, no PSSA errors.
Thanks again for your help and patience.
rjmholt commentedon Jul 8, 2020
From the stack traces you've reported, I'm thinking that there is a bug in PSSA here as well as one in PowerShell. The key is being able to reproduce without VSCode though. If you can just use
Invoke-ScriptAnalyzerin an ordinary PowerShell prompt and reproduce it, that would be really helpfulavezinaATastus commentedon Jul 8, 2020
I have been able to do that.
Just tried this, and I got the NREs
I re-installed my module (from my private azdo artifact nuget feed).
EDIT:
And after uninstalling the module (and restarting a new ps session, because of the class), no NRE when invoking the analysis
rjmholt commentedon Jul 8, 2020
If you try this in PS7 does the issue ever occur?
avezinaATastus commentedon Jul 8, 2020
I will need to install it.
Gonna spin a vm to try.
rjmholt commentedon Jul 8, 2020
My thinking is that if you're defining attributes as PS classes, then there are some issues in PS 5.1 with that, as you've alluded to. Bugs like that have been fixed in PS 7, although PSSA should still defend itself from them
avezinaATastus commentedon Jul 8, 2020
I have completed the test with PS7.
I had no errors.
I will try harder to keep that info in my head.
Not yet ready to switch to full-time (or part-time) ps7.
webtroter commentedon Aug 23, 2021
Thank you past me :)