"Navigate > GoToDeclaration" fails if target is in gradle project
- Dominant language
- Java
- Stars
- 3.1k
- Forks
- 935
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 17
Description
### Apache NetBeans version
Apache NetBeans 16
### What happened
Using `NetBeans-16u1` (_as of 2022/12/18_)
If in a maven project's java file and the target declaration is in an open gradle project's file then the gradle project's **sources.jar file is opened instead of the open project's source file**. If in a gradle project, a source file target in maven project works. It can be reproduced with the trivial projects in attached jar file and the steps described in the next section.
Here's some information from doing GoToDeclaration. The ClasspathInfo is the same doing maven project to maven project or gradle to maven; but the contents of `internal compile` paths are very different between maven and gradle projects; this looks suspicious. I don't know enough about the various Classpaths and their usage or how they're set up; **is this a smoking gun?**
As seen at `SourceUtils.getFile()`
cps for .COMPILE is `internal compile` (AFAICT):
```
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s77/java/15/classes:
/junk/Caches/maven_repository/play/m2g/m2g-gradle-lib/1.2/m2g-gradle-lib-1.2.jar:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s1/java/15/classes:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s3/java/15/classes,
```
Referencing the following, see that "index/s2" is not part of `internal compile`, instead there's the gradle jar file.
In .../var/cache/index
```
index/s3/java/15/classes/play/m2g/app/MavenApp.sig
index/s2/java/15/classes/play/m2g/g/GradleLibrary.sig
index/s1/java/15/classes/play/m2g/m/MavenLib.sig
```
Note that when doing GoToDeclaration _maven to maven_, it is resolved by
CacheSourceForBinaryQueryImpl.findSourceRoots, in particular with:
```
URL sourceURL = JavaIndex.getSourceRootForClassFolder(binaryRoot);
```
where binaryRoot is `file:/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s1/java/15/classes/`
and sourceURL is `file:/junk/play/m2g/maven-lib-project/src/main/java/`.
BTW, The jVi plugin uses `NbEditorKit.gotoDeclaration`.
### How to reproduce
[m2g.zip](https://github.com/apache/netbeans/files/10254329/m2g.zip)
Attached jar file has 3 projects, 2 maven and one gradle, one java file per project. jars end up under `/play/m2g`.
Open the three projects and build them. Build order `maven-lib`, `gradle-lib` (publishToMavenLocal), `maven-app`.
In `m2g-app::MavenApp.java`on the line `new GradleLibrary().someGradleLibraryMethod();` place the caret over `someGradleLibraryMethod` and do `Navigate > GoToDeclaration`.
**Observe**: a source file from `m2g-gradle-lib-1.2-sources.jar` is opened.
**Expect**: source file from the open project is opened.
One can similarly do the same over `new MavenLib().mavenLibFunc();` and it works fine.
### Did this work correctly in an earlier version?
No / Don't know
### Operating System
Linux harmony 6.0.6-76060006-generic #202210290932~1667401208~22.04~d2df702 SMP PREEMPT_DYNAMIC Wed N x86_64 x86_64 x86_64 GNU/Linux
### JDK
openjdk 17.0.2 2022-01-18 OpenJDK Runtime Environment (build 17.0.2+8-86) OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
### Apache NetBeans packaging
Own source build
### Anything else
_Some information encountered which may be useful for reference._
**ClasspathInfo [ /// As seen at SourceUtils.getFile**
_Leaving out: boot, module boot, internal boot_
```
compile:
/junk/Caches/maven_repository/play/m2g/m2g-gradle-lib/1.2/m2g-gradle-lib-1.2.jar:
/junk/Caches/maven_repository/play/m2g/m2g-maven-lib/1.0-SNAPSHOT/m2g-maven-lib-1.0-SNAPSHOT.jar:
/junk/play/m2g/maven-app-project/target/classes,
module compile: ,
module class:
/junk/Caches/maven_repository/play/m2g/m2g-gradle-lib/1.2/m2g-gradle-lib-1.2.jar:
/junk/Caches/maven_repository/play/m2g/m2g-maven-lib/1.0-SNAPSHOT/m2g-maven-lib-1.0-SNAPSHOT.jar:
/junk/play/m2g/maven-app-project/target/classes,
src:
/junk/play/m2g/maven-app-project/src/main/java:
/junk/play/m2g/maven-app-project/src/main/resources,
internal compile:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s77/java/15/classes:
/junk/Caches/maven_repository/play/m2g/m2g-gradle-lib/1.2/m2g-gradle-lib-1.2.jar:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s1/java/15/classes:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s3/java/15/classes,
internal module class:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s77/java/15/classes:
/junk/Caches/maven_repository/play/m2g/m2g-gradle-lib/1.2/m2g-gradle-lib-1.2.jar:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s1/java/15/classes:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s3/java/15/classes,
internal src:
/junk/play/m2g/maven-app-project/src/main/java,
internal out:
/src/jvi-dev/nbvi/build/testuserdir/var/cache/index/s3/java/15/classes
]
```
This lookup result shows up in many places around the areas investigated.
```
Lookup.getDefault().lookupAll(SourceForBinaryQueryImplementation.class) Collections$UnmodifiableRandomAccessList ObjectVariable "size = 12"
[ 0] RepositoryForBinaryQueryImpl #33669 org.netbeans.modules.maven.queries.RepositoryForBinaryQueryImpl@48368120
[ 1] GradleSourceForBinary #33670 org.netbeans.modules.gradle.queries.GradleSourceForBinary@375ff7a8
[ 2] GlobalSourceForBinaryQuery #33671 org.netbeans.modules.java.openjdk.project.GlobalSourceForBinaryQuery@6846b983
[ 3] PlatformSourceForBinaryQuery #33672 org.netbeans.modules.java.platform.queries.PlatformSourceForBinaryQuery@343081b1
[ 4] ProjectSourceForBinaryQuery #33673 org.netbeans.modules.java.project.ProjectSourceForBinaryQuery@48733996
[ 5] OpenGradleProjectForBinary #33674 org.netbeans.modules.gradle.java.queries.OpenGradleProjectForBinary@596587
[ 6] GradleSourceForRepository #33675 org.netbeans.modules.gradle.java.queries.GradleSourceForRepository@5d0e5eae
[ 7] CacheSourceForBinaryQueryImpl #33676 org.netbeans.modules.java.source.classpath.CacheSourceForBinaryQueryImpl@6d8361b7
[ 8] J2SELibrarySourceForBinaryQuery #33677 org.netbeans.modules.java.j2seplatform.libraries.J2SELibrarySourceForBinaryQuery@3ced4a18
[ 9] SourceForBinaryQueryImpl #33678 org.netbeans.modules.java.debug.SourceForBinaryQueryImpl@77d3655f
[10] J2eePlatformSourceForBinaryQuery #33679 org.netbeans.modules.j2ee.deployment.impl.query.J2eePlatformSourceForBinaryQuery@5807be25
[11] DefaultSourceForBinaryQuery #33680 org.netbeans.modules.java.j2seplatform.queries.DefaultSourceForBinaryQuery@2e7fe24b
```
### Are you willing to submit a pull request?
No
### Code of Conduct
Yes
Contributor guide
Research direction
Reproduce the problem with the attached m2g.zip projects and the Maven-to-Gradle GoToDeclaration steps. Start at SourceUtils.getFile(), then inspect CacheSourceForBinaryQueryImpl.findSourceRoots and the listed SourceForBinaryQuery implementations, with NbEditorKit.gotoDeclaration as the entry point. Done means the open Gradle project's source file opens instead of its sources.jar file while Maven-to-Maven navigation continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100