Skip to content

[apex] Cannot load ruleset for custom rule ExcessiveClassLength #6395

Description

@aruntyagiTutu

Affects PMD Version:

PMD 7.0.0

Description:

Method List loadRuleSetsWithoutException(List rulesetPaths) is throwing expection for custom ruleset xml for depricated rule (ex: ExcessiveClassLength), earlier versions were supposed to throw a warning not excpetion.

Exception Stacktrace:

"Cannot load ruleset /var/folders/4n/_s0xwt010hd08x07cjfcy1mc0000gn/T/junit-15737253256320483705/excessive-classlength.xml: PMD threw an unexpected exception:
Warning at /var/folders/4n/_s0xwt010hd08x07cjfcy1mc0000gn/T/junit-15737253256320483705/excessive-classlength.xml:7:5
 5|     <description>Sample Custom PMD Rules Description</description>
 6|     <rule ref="category/apex/design.xml"/>
 7|     <rule ref="category/apex/design.xml/ExcessiveClassLength">
        ^^^^^ Discontinue using Rule name category/apex/design.xml/ExcessiveClassLength as it is scheduled for removal from PMD. PMD 8.0.0 will remove support for this Rule.

 8|       <properties>
 9|         <property name="minimum" value="100"/>"

Code Sample demonstrating the issue:

    @Test
    void whenDescribeRulesIsGivenCustomRulesetThatOverridesExcessiveClassLength_thenCustomWins(@TempDir Path tempDir) throws Exception {
        String xml = """
            <?xml version="1.0" encoding="UTF-8"?>
            <ruleset name="Custom PMD Rules"
                     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 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
              <description>Custom PMD rules for Code Analyzer</description>
              <rule ref="category/apex/design.xml"/>
              <rule ref="category/apex/design.xml/ExcessiveClassLength">
                <properties>
                  <property name="minimum" value="100"/>
                </properties>
              </rule>
            </ruleset>
            """;
        Path rulesetFile = tempDir.resolve("excessive-classlength.xml");
        Files.writeString(rulesetFile, xml);

        List<PmdRuleInfo> ruleInfoList = ruleDescriber.describeRulesFor(
                List.of(rulesetFile.toAbsolutePath().toString()),
                Set.of("apex"));

        PmdRuleInfo ruleInfo = assertContainsOneRuleWithNameAndLanguage(ruleInfoList, "ExcessiveClassLength", "apex");
        assertThat(ruleInfo.ruleSetFile, is(rulesetFile.toAbsolutePath().toString()));
        assertThat(ruleInfo.ruleSets, hasItems("Custom PMD Rules", "Design"));
        assertThat(ruleInfo.externalInfoUrl, allOf(startsWith("https://"), endsWith(".html#excessiveclasslength")));
    }

Steps to reproduce:

Please provide detailed steps for how we can reproduce the bug.

  1. for custom pmd.xml with depecricated apex rule engine is throwing error
  2. ...

Running PMD through: [Gradle]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:bugPMD crashes or fails to analyse a file.is:unreproducibleA bug report that cannot be reproduced - reporter should provide a MWEwas:invalid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions