diff --git a/src/lib/apexPmd.ts b/src/lib/apexPmd.ts
index 1beceb1..af23e90 100644
--- a/src/lib/apexPmd.ts
+++ b/src/lib/apexPmd.ts
@@ -150,6 +150,9 @@ export class ApexPmd {
}
}
+ // TODO: determine the nearest sfdx-project.json file beginning from targetPath up to workspaceRootPath
+ env["PMD_APEX_ROOT_DIRECTORY"] = workspaceRootPath;
+
const cmd = `"${path.join(pmdBinPath, 'bin', 'pmd')}" check ${pmdKeys}`;
this.outputChannel.appendLine(`node: ${process.version}`);
diff --git a/test/assets/project3_unusedmethod/custom_ruleset.xml b/test/assets/project3_unusedmethod/custom_ruleset.xml
new file mode 100644
index 0000000..a7236d6
--- /dev/null
+++ b/test/assets/project3_unusedmethod/custom_ruleset.xml
@@ -0,0 +1,8 @@
+
+
+ Test ruleset
+
+
+
+
diff --git a/test/assets/project3_unusedmethod/sfdx-project.json b/test/assets/project3_unusedmethod/sfdx-project.json
new file mode 100644
index 0000000..dba67ee
--- /dev/null
+++ b/test/assets/project3_unusedmethod/sfdx-project.json
@@ -0,0 +1,9 @@
+{
+ "packageDirectories": [
+ {
+ "path": "src",
+ "default": true
+ }
+ ],
+ "namespace": "foo_ns"
+}
diff --git a/test/assets/project3_unusedmethod/src/Foo.cls b/test/assets/project3_unusedmethod/src/Foo.cls
new file mode 100644
index 0000000..1b1ca13
--- /dev/null
+++ b/test/assets/project3_unusedmethod/src/Foo.cls
@@ -0,0 +1,9 @@
+public class Foo {
+ // this method is needed, otherwise the whole class is considered as unused, as it would contain
+ // then only one unused method.
+ private void other() {}
+
+ public void unusedMethod() {
+ other();
+ }
+}
diff --git a/test/assets/project3_unusedmethod/src/Foo.cls-meta.xml b/test/assets/project3_unusedmethod/src/Foo.cls-meta.xml
new file mode 100644
index 0000000..541584f
--- /dev/null
+++ b/test/assets/project3_unusedmethod/src/Foo.cls-meta.xml
@@ -0,0 +1,5 @@
+
+
+ 50.0
+ Active
+
diff --git a/test/suite/extension.test.ts b/test/suite/extension.test.ts
index 1f933e9..54295a5 100644
--- a/test/suite/extension.test.ts
+++ b/test/suite/extension.test.ts
@@ -214,4 +214,38 @@ suite('Extension Tests', () => {
done(e);
});
});
+
+ test('UnusedMethod with Apex Link and PMD_APEX_ROOT_DIRECTORY', function (done) {
+ this.timeout(100000);
+
+ const workspaceRootPath = path.join(TEST_ASSETS_PATH, 'project3_unusedmethod');
+ const rulesetPath = path.join(workspaceRootPath, 'custom_ruleset.xml');
+ const apexClassFile = path.join(workspaceRootPath, 'src', 'Foo.cls');
+
+ const collection = vscode.languages.createDiagnosticCollection('apex-pmd-test');
+
+ const config = new Config();
+ config.pmdBinPath = PMD_PATH;
+ config.rulesets = [rulesetPath];
+ config.priorityErrorThreshold = 3;
+ config.priorityWarnThreshold = 1;
+ config.workspaceRootPath = workspaceRootPath;
+ config.additionalClassPaths = [];
+ config.commandBufferSize = 64000000;
+
+ const pmd = new ApexPmd(outputChannel, config);
+
+ const testApexUri = vscode.Uri.file(apexClassFile);
+ pmd
+ .run(apexClassFile, collection)
+ .then(() => {
+ const errs = collection.get(testApexUri);
+ assert.strictEqual(errs.length, 1);
+ assert.strictEqual(errs[0].message, "Unused methods make understanding code harder (rule: Design-UnusedMethod)");
+ done();
+ })
+ .catch((e) => {
+ done(e);
+ });
+ });
});