-
Notifications
You must be signed in to change notification settings - Fork 378
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
Exception occurred while constructing custom validator attribute. #966
Comments
The exception is different in 5.1 $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14409.1012
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1012}
BuildVersion 10.0.14409.1012
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
$error[0]|select *
writeErrorStream : True
PSMessageDetails :
Exception : System.Management.Automation.RuntimeException: The following exception occurred while
constructing the attribute "ValidateSomethingAttribute": "La valeur ne peut pas être null.
Nom du paramètre : key" ---> System.ArgumentNullException: La valeur ne peut pas être null.
Nom du paramètre : key
à System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
à System.Runtime.CompilerServices.ConditionalWeakTable`2.TryGetValue(TKey key, TValue&
value)
à ValidateSomethingAttribute..ctor()
à CallSite.Target(Closure , CallSite , Object )
à System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
--- Fin de la trace de la pile d'exception interne ---
à System.Management.Automation.Language.Compiler.GetAttribute(AttributeAst attributeAst)
à System.Management.Automation.Language.Compiler.GetRuntimeDefinedParameter(ParameterAst
parameterAst, Boolean& customParameterSet, Boolean& usesCmdletBinding)
à System.Management.Automation.Language.Compiler.GetParameterMetaData(ReadOnlyCollection`1
parameters, Boolean automaticPositions, Boolean& usesCmdletBinding)
à System.Management.Automation.CompiledScriptBlockData.InitializeMetadata()
à System.Management.Automation.CompiledScriptBlockData.GetAttributes()
à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseShouldProcessCorrectly.Support
sShouldProcess(String cmdName)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseShouldProcessCorrectly.CheckFo
rSupportShouldProcess()
à Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseShouldProcessCorrectly.<Analyz
eScript>d__7.MoveNext()
à System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
à System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
à Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass78_0.<AnalyzeS
yntaxTree>b__1()
TargetObject : C:\Users\Laurent\Downloads\PSSAReproduction-master\Modules\Main\Main.psm1
CategoryInfo : InvalidOperation: (C:\Users\Lauren...\Main\Main.psm1:String) [Invoke-ScriptAnalyzer],
RuntimeException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at Invoke-Analysis, C:\Users\Laurent\Downloads\PSSAReproduction-master\Modules\Main\Main.psm1:
line 5
at <ScriptBlock>, C:\Users\Laurent\Downloads\PSSAReproduction-master\run.ps1: line 2
at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1} The code works if we use a C# class : .Modules\Common\Common.psm1
Add-type @'
public class ValidateSomethingAttribute : System.Management.Automation.ValidateArgumentsAttribute
{
protected override void Validate(object arguments, System.Management.Automation.EngineIntrinsics engineIntrinsics)
{
}
}
'@
function Invoke-WithValidation {
[CmdletBinding()]
param (
[ValidateSomethingAttribute()]
$Path)
}
# class ValidateSomethingAttribute : System.Management.Automation.ValidateArgumentsAttribute
# {
# [void] Validate([object]$arguments, [System.Management.Automation.EngineIntrinsics]$engineIntrinsics)
# {
# }
# }
Export-ModuleMember -Function '*' |
Thanks for reporting this and the detailed repro steps and feedback. I can confirm this reproes as well in the latest development version in Windows PowerShell and PowerShell Core but in both cases only when |
@JamesWTruher What do you think about using the |
All my previous analysis is still true in terms of improvements to be made but I think I could (finally) figure out what happens in this case: By doing some analysis in PowerShell, the following works from the command line: $env:PSModulePath = "$(Resolve-Path 'Modules');$env:PSModulePath"
$c=gcm Invoke-WithValidation
$c.ScriptBlock.Attributes Therefore I think (and I got more confirmed when debugging) that in the call to |
Please specify the released/target version. |
Steps to reproduce
Run run.ps1
Source code available also here:
https://github.com/hubuk/PSSAReproduction
run.ps1
Modules\Common\Common.psm1
Modules\Main\Main.psm1
Expected behavior
No exception shall be reported.
Actual behavior
Exception thrown:
Environment data
The text was updated successfully, but these errors were encountered: