Pmd: [core] PMD exclude rules are failing with IllegalArgumentException with non-default minimumPriority

Created on 16 May 2017  路  7Comments  路  Source: pmd/pmd

Rule Set:
In attached project.

Description:
Upgraded to using PMD 5.6.1, now the following rules in our custom ruleset are failing the build with

java.lang.IllegalArgumentException: Unable to exclude rules [RuleNameGoesHere]; perhaps the rule name is mispelled?

Failing rules:
DataflowAnomalyAnalysis
ShortClassName
UselessParentheses

The rule names match up with latest PMD doc so they are not misspelled (ironically, the error message misspells the word misspelled). Why are these failing?
In addition, PMD should provide a list of all available rules (perhaps in INFO mode) instead of a pretty useless error message that leaves the user in the dark.

Code Sample demonstrating the issue:

demo (3).zip

Running PMD through: Gradle

Also see
https://github.com/gradle/gradle/issues/1970

bug pr

All 7 comments

Thanks for reporting this bug.
I'm not sure, what's happening here: When running your project with commandline PMD using the ruleset in src/main/resources/ruleset.xml, this problem doesn't occur.
I can reproduce the issue with your demo gradle project, though.

@adangel Thanks for looking into it. The Gradle guys tell me that they're just calling PMD, which is also indicated by the stacktrace.

Yes, the error is definitely coming from PMD. Gradle is calling PMD as an Ant task. However, calling PMD via Ant using the ruleset, also does not trigger the error... I'll keep investigating.

@adangel could you make any progress with this? Do you need any help?

Just some note for future: debugging gradle builds: GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000" ./gradlew --no-daemon check

So, the issue is about rule priorities: The gradle project defines the property rulePriority = 3 - which means, that all rules with a minimum priority of medium or higher should be executed. The rule DataflowAnomalyAnalysis has a priority of low (5) and is therefore not retrieved by the ruleset factory. Since it appeared now as if the rule couldn't be excluded (since it actually has not been excluded by the exclusion mechanism but by the priority selection), it's reported.

So, workaround is:

  • either execute all rules regardless of the priority (remove the rulePriority property)
  • don't exclude the rules, that won't be executed anymore due to the priority setting

We should definitely handle this case much better...

@adangel Thanks for getting to the bottom of this. Some comments:

  1. Like I said in the problem description, if PMD prints out a list of available rules with severity when it can't find a match, it'll be a lot easier for the user to troubleshoot. I'm not sure if Gradle info or debug mode is passed to PMD, but this is certainly a good candidate for any of those logging levels.
  2. As for a fix, instead of failing, I recommend displaying a warning like "DataflowAnomalyAnalysis is skipped as it doesn't match the rule selection criteria", or something along that lines.
  3. This used to work with previous versions of PMD, so something changed in PMD code.

On a related note, the compatibility ruleset factory is also triggering issues closely related to this:

For instance, having a ruleset defining:

  <rule ref="rulesets/java/unusedcode.xml">
    <exclude name="UnusedModifier" />
  </rule>

would trigger the same error (java.lang.IllegalArgumentException: Unable to exclude rules [UnnecessaryModifier]; perhaps the rule name is mispelled?) since the rule was moved to the unnecessarycode ruleset.

This is also related to #393

Was this page helpful?
0 / 5 - 0 ratings