microsoft / PSRule

Validate infrastructure as code (IaC) and objects using PowerShell rules.
https://microsoft.github.io/PSRule/v2/
MIT License
395 stars 49 forks source link

Not useful error info on a rule with issues #565

Closed nightroman closed 4 years ago

nightroman commented 4 years ago

Example

Test.Rule.ps1

Rule Test {
    $item = Get-Item -MisspelledParameter MyItem
    $true
}

Actual result

Invoke-PSRule fails with the following information:

Invoke-RuleBlock : A parameter cannot be found that matches parameter name 'MisspelledParameter'.
At C:\Program Files\WindowsPowerShell\Modules\PSRule\0.21.0\PSRule.psm1:178 char:17
+                 $pipeline.Process($InputObject);
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-RuleBlock], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,PSRule.Commands.InvokeRuleBlockCommand

Note there is no references to the problem rule name, file, error source location.

Expected result

The failure information should mention at least the rule name and ideally the error location in the source file.

Environment

PSRule 0.21.0

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.752
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
BernieWhite commented 4 years ago

@nightroman Thanks for the feedback. Agreed, error messages could be improved to provide more helpful information.

nightroman commented 4 years ago

Another problem with this example (I may submit a separate issue if you want me to). If I have other rule files in the same folder then they are not invoked due to this failure. E.g. if another file is named good.rule.ps1 then it is invoked, then test.rule.ps1 fails. But if another file is named zoo.rule.ps1 then it is not invoked. I would expect all rule files invoked and their outcomes reported.

BernieWhite commented 4 years ago

Another problem with this example (I may submit a separate issue if you want me to). If I have other rule files in the same folder then they are not invoked due to this failure. E.g. if another file is named good.rule.ps1 then it is invoked, then test.rule.ps1 fails. But if another file is named zoo.rule.ps1 then it is not invoked. I would expect all rule files invoked and their outcomes reported.

Split to separate issue as this is a bug.

BernieWhite commented 4 years ago

@nightroman There are some challenges with PowerShell overriding InvocationInfo on the ErrorRecord, however I think the following should address your feedback.

Windows PowerShell example:

   TargetName: dd29ecf524b030a65261e3059c48ab9e1ecb2585

RuleName                            Outcome    Recommendation
--------                            -------    --------------
WithPass                            Pass       Should pass
WithParseError                      Error      Rule to generate a PowerShell parsing exception
invoke-psrule : WithParseError : Reported 'A parameter cannot be found that matches parameter name
'MisspelledParameter'.'. At C:\dev\workspace\PSRule\tests\PSRule.Tests\FromFileWithError.Rule.ps1:78 char:22
At line:1 char:6
+ '' | invoke-psrule .\tests\PSRule.Tests\FromFileWithError.Rule.ps1 -N ...
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (dd29ecf524b030a65261e3059c48ab9e1ecb2585:System.String) [Invoke-PSRule
   ], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetItemCommand,WithParseError,Invok
   e-PSRule

PowerShell 7 example:

   TargetName: dd29ecf524b030a65261e3059c48ab9e1ecb2585

RuleName                            Outcome    Recommendation
--------                            -------    --------------
WithPass                            Pass       Should pass
WithParseError                      Error      Rule to generate a PowerShell parsing exception
Invoke-PSRule: WithParseError : Reported 'A parameter cannot be found that matches parameter name 'MisspelledParameter'.'. At C:\Dev\Workspace\PSRule\tests\PSRule.Tests\FromFileWithError.Rule.ps1:78 char:22