eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
157 stars 125 forks source link

Can't build project with JDT as a dependency when using Java modules #2780

Open bvlj opened 2 months ago

bvlj commented 2 months ago

When using the jdt.core library as a dependency (org.eclipse.jdt:org.eclipse.jdt.core:3.38.0 from maven central) to a Java 21 Gradle project which also makes use of the Java module system, the compilation fails due to a number of packages being provided multiple times by the library.

Excerpt of build output:

Task :compileJava FAILED
1 actionable task: 1 executed

error: the unnamed module reads package org.eclipse.jdt.internal.compiler from both org.eclipse.jdt.core and org.eclipse.jdt.core.compiler.batch
error: the unnamed module reads package org.eclipse.jdt.internal.compiler.parser from both org.eclipse.jdt.core and org.eclipse.jdt.core.compiler.batch
error: the unnamed module reads package org.eclipse.jdt.core from both org.eclipse.jdt.core and org.eclipse.jdt.core.compiler.batch
error: the unnamed module reads package org.eclipse.jdt.core.compiler from both org.eclipse.jdt.core and org.eclipse.jdt.core.compiler.batch
error: the unnamed module reads package org.eclipse.core.runtime from both org.eclipse.equinox.common and org.eclipse.equinox.registry
error: the unnamed module reads package org.eclipse.core.runtime from both org.eclipse.equinox.common and org.eclipse.core.runtime
error: the unnamed module reads package org.eclipse.core.internal.runtime from both org.eclipse.equinox.common and org.eclipse.core.runtime
error: module org.eclipse.core.resources reads package org.eclipse.jdt.core from both org.eclipse.jdt.core.compiler.batch and org.eclipse.jdt.core
error: module org.eclipse.core.resources reads package org.eclipse.jdt.core.compiler from both org.eclipse.jdt.core.compiler.batch and org.eclipse.jdt.core
error: module org.eclipse.core.resources reads package org.eclipse.jdt.internal.compiler from both org.eclipse.jdt.core.compiler.batch and org.eclipse.jdt.core
...

Example of a module-info.java of the module that makes use of the jdt library:

module example {
  requires org.eclipse.jdt.core;
}

I have provided a repository which should help you reproduce and investigate the issue here: https://github.com/bvlj/eclipse_jdt_issue_module

iloveeclipse commented 2 months ago

Why do you use jdt.core and not jdt batch compiler directly?

bvlj commented 2 months ago

Assuming you're referring to using the classes under the org.eclipse.jdt.internal.compiler.batch package (which should come from org.eclipse.jdt.ejc if I understand correctly), rather than those in org.eclipse.jdt.core.dom: I can't find an "equivalent" of the org.eclipse.jdt.core.dom.ASTParser class in the former

iloveeclipse commented 2 months ago

OK, I see, you want have AST, not just compiler. I assume there is no way to use modules for jdt.core now, as we share some packages with batch compiler and so violate "module" rules.

I guess the only viable solution for the world outside of the OSGI would be to provide "merged" JDT module (batch + core) deployed to maven?

@stephan-herrmann : any comments on that?

stephan-herrmann commented 2 months ago

OK, I see, you want have AST, not just compiler. I assume there is no way to use modules for jdt.core now, as we share some packages with batch compiler and so violate "module" rules.

I guess the only viable solution for the world outside of the OSGI would be to provide "merged" JDT module (batch + core) deployed to maven?

@stephan-herrmann : any comments on that?

I assume the task is invoking ASTParser from within a JPMS module, right?

In that case, org.eclipse.jdt.core must indeed be referenced (required) as an automatic module. But it is possible to use --patch-module to link in also org.eclipse.jdt.core.compiler.batch alias ecj (with appropriate paths to the jar files):

$ java -p org.eclipse.jdt.core_3.39.0.v20240724-1734.jar --add-modules org.eclipse.jdt.core  --patch-module org.eclipse.jdt.core=org.eclipse.jdt.core.compiler.batch_3.39.0.v20240725-1906.jar org.eclipse.jdt.internal.compiler.batch.Main -help
stephan-herrmann commented 1 month ago

I assume the above workaround with --patch-module is the best we can offer.

bvlj commented 1 month ago

Ok, thanks for the information 👍