-
Notifications
You must be signed in to change notification settings - Fork 393
NullReferenceException on PSShouldProcess Rule. #1539
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
Comments
From #1538 (comment). Here's the relevant code: PSScriptAnalyzer/Rules/UseShouldProcessCorrectly.cs Lines 344 to 398 in d04c039
|
After looking at my version control,
EDIT : I wasn't analyzing checked-in code. ignore this comment. |
Using 1.18.0, I have better details about the error, I think. writeErrorStream : True
PSMessageDetails :
Exception : System.Management.Automation.RuntimeException: The following exception occurred while constructing the attribute "ServerTransformAttribute": "Value cannot be null.
Parameter name: key" ---> System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Runtime.CompilerServices.ConditionalWeakTable`2.TryGetValue(TKey key, TValue& value)
at ServerTransformAttribute..ctor()
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
--- End of inner exception stack trace ---
at System.Management.Automation.Language.Compiler.GetAttribute(AttributeAst attributeAst)
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.CompiledScriptBlockData.InitializeMetadata()
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>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\Public\Update-mdbDeployment.ps1
CategoryInfo : InvalidOperation: (C:\Users\avezin...bDeployment.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} ServerTransformAttribute is a |
I tried doing that function
|
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? |
Here is the weird thing now :
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. |
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 |
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 I am back to the NRE. |
The original one that you reported in the description? Does it happen every time you run PSSA? |
yes I can privately send my zipped module, if you'd like. |
Yeah I think that's the right next step. Can you send it through to [email protected] |
|
Hmmm, in that case try [email protected] |
Not currently able to reproduce the error:
How are you invoking script analyzer? |
Multiple ways. Mostly via my psake task, but also manually. I could repro by Set-Location ~\Downloads\ServiceManagement\0.6.2
Invoke-ScriptAnalyzer -Path . -Recurse |
arghh, didn't want to close. |
oh fuck. it is my machine. Just tested in an "almost fresh" windows VM. Installed [email protected], ran Thanks for helping! |
Now, I gotta find what is causing that. Could it be that I have some version of my module installed ? EDIT : Nope |
Final edit Thanks again for your help and patience. |
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 |
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). install-module ServiceManagement -Repository TestFeed -Credential (Find-Credential AzDoPat*) -RequiredVersion 0.6.2
Set-Location ~\Downloads\ServiceManagement\0.6.2
Invoke-ScriptAnalyzer -Path . -Recurse EDIT: |
If you try this in PS7 does the issue ever occur? |
I will need to install it. Gonna spin a vm to try. |
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 |
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. |
Thank you past me :) |
Before submitting a bug report:
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 *
Environment data
The text was updated successfully, but these errors were encountered: