Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The type javax.annotation.Nonnull cannot be resolved #2712

Closed
yotov opened this issue Oct 3, 2022 · 16 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#2264
Closed

The type javax.annotation.Nonnull cannot be resolved #2712

yotov opened this issue Oct 3, 2022 · 16 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#2264
Assignees
Labels

Comments

@yotov
Copy link

yotov commented Oct 3, 2022

In one of my projects, I'm getting an error after upgrading to the latest version:

The type javax.annotation.Nonnull cannot be resolved. It is indirectly referenced from required .class filesJava(16777540)
Annotation type 'javax.annotation.Nonnull' cannot be found on the build path, which is implicitly needed for null analysisJava(536871894)

I saw the changelog and found that the issue relates to enhancement - Enable annotation-based null analysis. See #1693.

Removing "javax.annotation.Nonnull" from "java.compile.nullAnalysis.nonnull" config fixes the problem for me.

Environment
  • Operating System: MacOS 12.6
  • JDK version: openjdk 17 2021-09-14
  • Visual Studio Code version: 1.71.2
  • Java extension version: 1.11.0
@rgrunber rgrunber added the bug label Oct 3, 2022
@rgrunber
Copy link
Member

rgrunber commented Oct 3, 2022

Do you have a sample project available to share that was causing this issue. Looks like the annotations are used in some library/class files but aren't on the classpath ?

CC @CsCherrYY

@rgrunber
Copy link
Member

rgrunber commented Oct 4, 2022

If anyone is able to point to a sample project (or upload one) that causes this problem, it would be very helpful.

Looking at some past issues in JDT, I noticed this would happen when null analysis was enabled, but no implementation on the classpath. Since we currently enable the analysis automatically, it's obviously being enabled in circumstances where it shouldn't be.

@chowethan
Copy link

This is also occurring on a project I'm a part of. It's preventing the project from building within the IDE. Unfortunately, I'm not able to share any source code.

Is there a way to turn off/disable null analysis in configuration for the time being?

@nam178
Copy link

nam178 commented Oct 5, 2022

I'm having the same problem using VSCode + SSH into a remote Linux machine

I'm not using javax.annotation.Nonnull anywhere in the code other than lombok's NonNull
Downgraded Java extension to 1.10.0 + multiple mvn clean install fixed it

Mac Version 12.5
VSCode Version 1.71.2 (Universal)
OpenJDK Runtime Environment Corretto-17.0.4.9.1 (build 17.0.4.1+9-LTS)

Unfortunately, I'm not able to share the code

@rgrunber
Copy link
Member

rgrunber commented Oct 5, 2022

You should be able to disable this feature with the following settings (on the individual workspace, or globally).

.vscode/settings.json

{
    "java.compile.nullAnalysis.nonnull": [
    ],
    "java.compile.nullAnalysis.nullable": [
    ]
}

@rgrunber rgrunber added this to the End October milestone Oct 5, 2022
@snjeza
Copy link
Contributor

snjeza commented Oct 5, 2022

A related issue - #2721

@chowethan
Copy link

chowethan commented Oct 6, 2022

I have setup my settings.json file as instructed (I chose the workspace root .vscode directory)

{
	"java.compile.nullAnalysis.nonnull": [],
	"java.compile.nullAnalysis.nullable": []
}

However, I'm still seeing the same error after running mvn clean install as well as the extension command Java: Clean Java Language Server Workspace

Annotation type 'javax.annotation.Nonnull' cannot be found on the build path, which is implicitly needed for null analysis Java(536871894)

Does this have anything to do with the issue mentioned above?

@yotov
Copy link
Author

yotov commented Oct 6, 2022

If anyone is able to point to a sample project (or upload one) that causes this problem, it would be very helpful.

Looking at some past issues in JDT, I noticed this would happen when null analysis was enabled, but no implementation on the classpath. Since we currently enable the analysis automatically, it's obviously being enabled in circumstances where it shouldn't be.

@rgrunber here is a sample project that causes the problem for me - https://github.com/yotov/vscode-nullable
I managed to find that I get this error in projects that use namedParameterJdbcTemplate.batchUpdate and having a spring-kafka in dependencies. Removing the line with batchUpdate fixes the error. Removing the spring-kafka dependency also fixes the error.

Hope that this can help you :)

@snjeza
Copy link
Contributor

snjeza commented Oct 6, 2022

Does this have anything to do with the issue mentioned above?

@chowethan @yotov Could you try to remove .project, .classpath, .settings from your project?

@rgrunber
Copy link
Member

rgrunber commented Oct 6, 2022

@yotov thank-you. Am able to reproduce.

Looking at the classpath of the project, I see both com.google.code.findbugs:jsr305:jar:3.0.2:runtime & org.springframework:spring-core:jar:5.3.22:compile. So basically we have 2 annotation providers on the classpath. It looks like the defaults prefer the jsr305 provides (eg. javax.annotation..) over the spring framework provider (eg. org.springframework.lang..). However clearly in this case it should be reversed.

Can you try, the following setting ? It's basically just the default settings, but the spring framework annotations are moved to the top.

.vscode/settings.json

{
	"java.compile.nullAnalysis.nullable": [
		"org.springframework.lang.Nullable",
		"javax.annotation.Nullable",
		"org.eclipse.jdt.annotation.Nullable"
	],
	"java.compile.nullAnalysis.nonnull": [
		"org.springframework.lang.NonNull",
		"javax.annotation.Nonnull",
		"org.eclipse.jdt.annotation.NonNull"
	]
}

@yotov
Copy link
Author

yotov commented Oct 6, 2022

@rgrunber yes, reordering the classes fixes the problem and I'm able to compile the project from IDE

@rgrunber
Copy link
Member

rgrunber commented Oct 6, 2022

There may be a few things at play here as pointed out to me by @snjeza .

Apparently com.google.code.findbugs:jsr305:jar:3.0.2:runtime is somehow being recognized by m2e as a test dependency. We don't check either way when enabling null analysis if the annotation provider is only a test dependency (which is wrong). So I think that should be how we resolve this issue.

At https://github.com/eclipse/eclipse.jdt.ls/blob/f8452b422add64f9bbc80d0e22b48ed71797e563/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java#L2055 , could we just set the classpath options scope to be "compile" ?

@snjeza
Copy link
Contributor

snjeza commented Oct 7, 2022

At https://github.com/eclipse/eclipse.jdt.ls/blob/f8452b422add64f9bbc80d0e22b48ed71797e563/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java#L2055 , could we just set the classpath options scope to be "compile" ?

We can't because the scope is set in upstream project (spring-core). m2e adds the classpath test attribute for the maven runtime and test scope.
The PR (eclipse-jdtls/eclipse.jdt.ls#2264) skips all test classpath entries. It enables null analysis only if there is an annotation type in a classpath entry without the test attribute.
For instance, the vscode-nullable project
This PR enables null analysis for org.springframework.lang.* because the org.springframework:spring-core classpath entry has no the test attribute. The PR skips the jsr305 classpath entry because it has the test attribute. You shouldn't reorder java.compile.nullAnalysis.*.

@chowethan
Copy link

@yotov Could you try to remove .project, .classpath, .settings from your project?

@snjeza Mentioned the wrong person, but combined with @yotov's settings options, this worked to fully disable null analysis in my case

@jvansant
Copy link

jvansant commented Oct 12, 2022

@yotov Could you try to remove .project, .classpath, .settings from your project?

@snjeza Mentioned the wrong person, but combined with @yotov's settings options, this worked to fully disable null analysis in my case

It took adding
"java.compile.nullAnalysis.nullable": [],
"java.compile.nullAnalysis.nonnull": []

to my user and workspace settings.json. No change in the project .project, .classpath, .settings necessary.

@CsCherrYY
Copy link
Contributor

CsCherrYY commented Oct 13, 2022

@jvansant The fix eclipse-jdtls/eclipse.jdt.ls#2264 is available in the newest pre-release verison 1.12.2022101204, you can try to switch to this version to see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment