Create Maven nested project Test Case - Reproduce the Issue ".java is not on the classpath of project"#3460
Create Maven nested project Test Case - Reproduce the Issue ".java is not on the classpath of project"#3460AnqiHuangQiQi wants to merge 20 commits intoeclipse-jdtls:mainfrom
Conversation
Create maven nested project test case
|
Can one of the admins verify this patch? |
|
The project doesn't work correctly even from CLI. If I run Part of this is that the default source location is set to If you create the folder structure, |
...pse.jdt.ls.tests/projects/maven/nested-project/.eclipse/projects/mypackage_mytarget/.project
Outdated
Show resolved
Hide resolved
|
@rgrunber This is not supposed to be a valid MAVEN project. Please ignore the Maven part. This should be a setup how the Bazel extension generates projects. It works nicely in Eclipse but fails in JDTLS. This is an outcome of her investigations in salesforce/bazel-vscode-java#143. The problem is suspected in this code: TLDR; JDTLS is picking the wrong project to pick the |
...ipse.jdt.ls.tests/projects/maven/nested-project/.eclipse/projects/mypackage_mytarget/pom.xml
Outdated
Show resolved
Hide resolved
|
Ok, so we should probably have some kind of check to determine if the file is a linked resource, and prefer the original file as opposed to the shortest path. |
The opposite is true in this case. The links are setup to simulate a project structure that JDT can understand. I think a check is needed that the picked resource is actually part of a source directory ( Where it probably gets challenging is overlapping source files. It is possible to define different targets with different set of sources from the same Java package. Some classes might appear in both. Thus, there could be two nested projects both linking to the same file. I think this case should be explicitly ignored (eg., first one wins) because it is actually not encouraged. |
format change
Find the resource linked to target projects instead of the Workspace project
|
|
||
| IPackageFragmentRoot[] packageFragmentRoots = unit.getJavaProject().getPackageFragmentRoots(); | ||
| boolean containsPackageFragmentRoot = Arrays.stream(packageFragmentRoots) | ||
| .anyMatch(root -> root.getClass().equals(PackageFragmentRoot.class) |
There was a problem hiding this comment.
That's odd. Don't use getClass().equals(..). Use isInstance. Don't use internal classes (eg., PackageFragmentRoot). Use official JDT API only. Also, this check is unnecessary. The array is already IPackageFragmentRoot
| //find the resource which has PackageFragmentRoot | ||
| //find closest project containing that file, in case of nested projects |
There was a problem hiding this comment.
| //find the resource which has PackageFragmentRoot | |
| //find closest project containing that file, in case of nested projects | |
| //find closest project containing that file, in case of nested projects | |
| //ignore files outside a source folder (https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/3447) |
There was a problem hiding this comment.
However, the method name findResource no longer matches the contract now. This should be addressed.
org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java
Outdated
Show resolved
Hide resolved
With Bazel, it's impossible to know which path to the file is the right one because that depends on which "target" you're looking at. Files can be on the build path for different targets at the same time. You can't resolve this without either
IMO, the best thing we can do is to (optionally) delegate the resolution of URIs to Java elements to the build tool. Only Bazel knows which solution is the right one. |
|
This issue is related to salesforce/bazel-vscode-java#159
that has been fixed. |
Providing a reproducible case for this issue:
#3447
Error Message:
.java is not on the classpath of project, only syntax errors are reportedWhen opening this project in Vscode, 2 Java projects are recognized(This part is ok.):
The only real Java file is under /com/packages/foo/File.java.
There is 1 Java project which is under /.eclipse/projects/mypackage_mytarget.
Within .project file, we have a Link which is linking the real java file to
.eclipse/projects/mypackage_mytarget/linked-srcs/com/packages/foo/File.java.Here workspace_root only means it's the root folder.
The nested project mypackage_mytarget is the project we care and it has all the necessary class paths.
Then opening File.java in Vscode editor.

Got the error.
So I did live debug through Eclipse Jdtls and found the line of code which causing this error.
JDTUtils.findResource
When there are 2 IResources are found, it will simply choose the shorter path one(with lower number of segments)

In this case, /workspace_root is chosen.
But /mypackage_mytarget should be chosen.
If I changed the code in JDTUtils and return Resource(/mypackage_mytarget), the error is gone.
Let's work together to fix this issue.