tycho icon indicating copy to clipboard operation
tycho copied to clipboard

Null analysis fails during tycho build when building for older JDK (e.g. 11) with newer java version (e.g. 17)

Open bhufmann opened this issue 2 years ago • 13 comments

I noticed a problem during maven tycho build where the null annotation specified in an eea file doesn't seem to be applied when building with Java 17 for JDK 11. It works fine when building for JDK 17.

To reproduce I created an example to reproduce: testmaven-build.tar.gz. I ran the example on Linux with OpenJDK 17 and maven 3.8.6, Eclipse platform 4.25.

  1. cd ~/tmp
  2. tar xvzf testmaven-build.tar.gz
  3. cd testmaven-build
  4. mvn clean install

The following error will be created:

[ERROR] Failed to execute goal org.eclipse.tycho:tycho-compiler-plugin:2.7.5:compile (default-compile) on project CollectionTest: Compilation failure: Compilation failure: 
[ERROR] /home/eedbhu/git/testmaven-build/CollectionTest/src/collectiontest/TestClass.java:[32] 
[ERROR] 	test = Objects.requireNonNull(test).concat(suffix);
[ERROR] 	       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[ERROR] Potential null pointer access: The method requireNonNull(String) may return null
[ERROR] 1 problem (1 error)

However, Objects.requireNonNull() is set to return non-null as per ./CollectionTest/annotations/java/util/Objects.eea.

The build is successful when changing the JDK version to 17 (in the root pom.xml lines 48-49) or when disabling the null check by adding -Dtycho-use-project-settings=false to the build command-line.

bhufmann avatar Nov 08 '22 15:11 bhufmann

@bhufmann has you configured a "true" java 11 JVM in your toolchains? Also please note that Tycho 3.0 is the current version as it has some improvements of EJC regarding Java 17 <-> Java 11.

laeubi avatar Nov 08 '22 15:11 laeubi

@bhufmann has you configured a "true" java 11 JVM in your toolchains?

@laeubi How would I do that? Please let me know.

bhufmann avatar Nov 08 '22 15:11 bhufmann

Here is an example toolchains that is used by Tycho itself:

https://github.com/eclipse-tycho/tycho/blob/master/.github/toolchains.xml

General information regarding toolchains:

https://maven.apache.org/guides/mini/guide-using-toolchains.html

laeubi avatar Nov 08 '22 15:11 laeubi

I tried it with the toolchain and I think it was using the JDK 11 toolchain because I see the following print-out:

[INFO] Required toolchain: jdk [ version='11' ]
[INFO] Found matching toolchain for type jdk: JDK[/usr/lib/jvm/java-11-openjdk-amd64/]

But it still fails with the same error.

bhufmann avatar Nov 08 '22 15:11 bhufmann

I'm not sure how these null checks work and why there could be a difference but maybe @iloveeclipse can tell what might makes a difference here.

laeubi avatar Nov 10 '22 06:11 laeubi

null annotation specified in an eea file doesn't seem to be applied when building with Java 17 for JDK 11. It works fine when building for JDK 17.

I have no idea what compiler flags tycho uses here, but looks like it would need to use "-release" in this case, right?

I would guess the external annotations are "not found" in that case.

I don't know how they are hooked up in JDT (@stephan-herrmann should know that for sure), but most likely that need to be fixed in JDT => please create a bug there, ideally by using plain Java project example that uses similar combination of compiler settings that is used by tycho.

iloveeclipse avatar Nov 10 '22 07:11 iloveeclipse

I see org.eclipse.tycho:tycho-compiler-plugin:2.7.5 being used. This uses an older version of ECJ. A first thing is to try with latest Tycho snapshots as the issue may just be already fixed.

mickaelistria avatar Nov 10 '22 09:11 mickaelistria

If you suspect null annotations not being found at compile time by Tycho, then you may want to look into configuring them explicitly for the maven run. I have something like this in my tycho-compiler-plugin configuration:

<!-- workaround external null annotations, https://bugs.eclipse.org/bugs/show_bug.cgi?id=526011 -->
<arg>-annotationpath</arg>
<arg>${project.basedir}/../../releng/com.foo.extannotations</arg>

Bananeweizen avatar Nov 10 '22 09:11 Bananeweizen

I'm late for sure, but ..

... if this is still relevant, I'd be interested in the following:

  • the .classpath files of projects involved, and
  • any additional compiler configuration in the pom
  • the options which tycho passes into ECJ

stephan-herrmann avatar Feb 04 '23 21:02 stephan-herrmann

@stephan-herrmann the issue contains a reproducing project: testmaven-build.tar.gz that should contain all files. If you run maven with the -X option it prints out any ECJ arguments passed to the compiler, if you use mvnDebug instead of mvn you can even remote debug ECJ and set a breakpoint in the Main.

Let me know if you need any additional info/help here.

By the way @Bananeweizen has mentioned some compile-time annotations, if there are some common ones and we can somehow detect that they are required we can even enhance Tycho to add them automatically, please open a separate issue then how/if they should be detected and passed to ECJ.

laeubi avatar Feb 05 '23 06:02 laeubi

Found two JDT bugs:

  • Normally using a modern ecj, methods java.util.Objects.requireNonNull(..) should be understood in terms of nullness even without any external annotations, but this is broken when --release is set.
    • see eclipse-jdt/eclipse.jdt.core#703
  • The argument -annotationpath foo is not applied to a system library when --release is used
    • see eclipse-jdt/eclipse.jdt.core#702

As I see tycho passing an annotationpath argument already, should we synchronize to document the possible ways to configure this? In particular for the feature from https://bugs.eclipse.org/bugs/show_bug.cgi?id=526011 I could actually imagine tycho automatically generating a suitable argument value from the resolved dependencies.

stephan-herrmann avatar Feb 05 '23 13:02 stephan-herrmann

I think one goal of Tycho is to give users a seamless experience with EJC, so if we can improve things here just let me know, especially in Tycho 4.x we are now a lot more felxible, e.g. now "hyprid" bundles are supported that can either come from a targetplatform or maven directly, and also compile-only dependencies are now possible without much effort.

laeubi avatar Feb 05 '23 13:02 laeubi

Found two JDT bugs:

Both JDT bugs have been fixed.

stephan-herrmann avatar Jul 08 '23 21:07 stephan-herrmann