forcedotcom / sfdx-scanner

MIT License
217 stars 50 forks source link

[Feature Request] Unable to declare inline XPath rules in ruleset file #1242

Open keirbowden opened 1 year ago

keirbowden commented 1 year ago

I'm creating a custom ruleset based on the instructions. If my ruleset just references other rules, e.g.

<ruleset name="IP Team"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">

    <description>
          Rules which enforce IP Team best practice.
    </description>
    <rule ref="category/apex/bestpractices.xml/ApexUnitTestMethodShouldHaveIsTestAnnotation"/>
</ruleset>

it all works as expected.

When I declare an XPath rule in the file:

<ruleset name="IP Team"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">

    <description>
          Rules which enforce IP Team best practice.
    </description>
    <rule ref="category/apex/bestpractices.xml/ApexUnitTestMethodShouldHaveIsTestAnnotation"/>
    <rule name="SoqlOnlyInTestAndAccessorClasses"
          language="apex"
          message="SOQL queries can only appear in test methods and accessor classes"
          class="net.sourceforge.pmd.lang.rule.XPathRule">
       <description>
             Checks that SOQL queries are only present in test methods and accessor
             classes.
       </description>
       <priority>3</priority>
       <properties>
           <property name="version" value="2.0"/>
           <property name="xpath">
              <value>
<![CDATA[
//UserClass[not(ends-with(@Image, 'Accessor'))]/Method/ModifierNode[@Test=false()]/..//(SoqlExpression | MethodCallExpression[lower-case(@FullMethodName)='database.query'])
]]>
              </value>
           </property>
       </properties>
   </rule>
</ruleset>

I can still add the ruleset :

sf scanner rule add -l apex -p /Users/kbowden/bm-pmd-rules-new/rulesets/apex/new.xml
 ›   Warning: Plugin @salesforce/sfdx-scanner (3.18.0) differs from the version specified by sf (3.17.0)
WARNING: We're continually improving Salesforce Code Analyzer. Tell us what you think! Give feedback at https://research.net/r/SalesforceCA
Successfully added rules for apex.
1 Path(s) added: /Users/kbowden/bm-pmd-rules-new/rulesets/apex/new.xml

But any commands I execute after that throw an error:

INTERNAL ERROR: Unexpected error occurred while cataloging rules: begin 0, end 3, length 0. Please log an issue with us at github.com/forcedotcom/sfdx-scanner.

And the only way to fix this is to remove the entry from .sfdx-scanner/CustomPaths.json

The rule works fine when added from a catalog file, just not in a ruleset.

The instructions mentioned (https://forcedotcom.github.io/sfdx-scanner/en/v3.x/custom-config/pmd/) do flip-flop a bit between rules and rulesets, so it might be a documentation issue to make it clear you can't declare rules in rulesets. However, I do think that it should error when I add the ruleset rather than succeeding and then not being able to execute any more commands.

jfeingold35 commented 1 year ago

@keirbowden , the documentation does instruct users to declare new rules in categories, rather than rulesets. However, the proposal to throw an error when adding the rules instead of allowing the catalog to enter an invalid state is a good one. We can take that on as a feature request.

git2gus[bot] commented 8 months ago

This issue has been linked to a new work item: W-15080498