javavscode icon indicating copy to clipboard operation
javavscode copied to clipboard

"<method name> is never used" hint, despite being used in generated source

Open kalaracey opened this issue 1 year ago • 3 comments

In my project, I have a method of a class that is only invoked by generated source code, and this extension appears to think the method is unused. I have attempted to create a minimum-reproducible example in https://github.com/kalaracey/oracle-vscode-incorrect-unused-hint:

Steps to reproduce:

  • Run gradle build.
  • Open the project in VSCode with the Oracle.oracle-java extension enabled and other Java extensions disabled.
  • Open the file app/src/main/java/org/example/FooController.java.
  • Note the squiggle under the post method and the warning (on hover) "post is never used": FooController-warning

This is despite the fact that FooController#post is used in the generated file app/build/generated/sources/annotationProcessor/java/main/org/example/FooController$Route.java:

      var result = controller.post();

Note that right-clicking on that invocation of post in FooController$Route.java and clicking Go To Definition works - it navigates back to FooController.java.

VSCode 1.94.2 Oracle.oracle-java v23.0.0

kalaracey avatar Nov 11 '24 21:11 kalaracey

I also noticed that right-clicking on post in FooController.java and clicking Go To References does not work. Perhaps generated sources are not indexed by this extension by default? If so, is it possible to enable indexing of generated sources?

kalaracey avatar Nov 11 '24 21:11 kalaracey

I thought perhaps the extension relies on all sources being in a Gradle source set, and maybe the generated sources don't belong to any source set.

# build.gradle.kt
tasks.register("sourceSets") {
    doLast {
        sourceSets.forEach {
            println(it)
            it.allSource.forEach {
                println(it)
            }
        }
    }
}
$ gradle sourceSets

> Task :app:sourceSets
source set 'main'
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/App.java
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/FooController.java
source set 'test'
/Users/kal/Code/incorrect-unused-hint/app/src/test/java/org/example/AppTest.java

BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed

Note there is no reference to FooController$Route.java.

However, that doesn't seem to be the issue. Even when I add to build.gradle.kts:

// This doesn't seem to have any affect.
sourceSets["main"].java.srcDir(file("build/generated/sources/annotationProcessor/java/main"))

and confirm that the generated source is now in the main source set:

$ gradle sourceSets

> Task :app:sourceSets
source set 'main'
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/App.java
/Users/kal/Code/incorrect-unused-hint/app/src/main/java/org/example/FooController.java
/Users/kal/Code/incorrect-unused-hint/app/build/generated/sources/annotationProcessor/java/main/org/example/FooController$Route.java
source set 'test'
/Users/kal/Code/incorrect-unused-hint/app/src/test/java/org/example/AppTest.java

BUILD SUCCESSFUL in 966ms
1 actionable task: 1 executed

the "post is never used" hint is still present.

kalaracey avatar Nov 11 '24 21:11 kalaracey

Hi @kalaracey. Thank you for reaching out and bringing this issue to our notice.

Using your reproducer example, I was able to confirm that:

  1. The extension does actually scan the generated sources, without even requiring the 2 subsequent additions you tried for registering the sourceSets.
    • This was observed in the extension Output logs like below:
    INFO [org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater]: Indexing of: .../app/build/generated/sources/annotationProcessor/java/main took: 4 ms
    
  2. The generated source code is analysed properly by the extension when opened in the editor and navigation to the definitions works.
  3. However, in the main app code, the hints analyser and navigation to the references in the generated sources does not work.

Based on this assessment, I am labelling this as a bug that may be targeted in a future release. Please let us know if you have any other inputs on this. Thank you.

sid-srini avatar Nov 27 '24 13:11 sid-srini