uber / NullAway

A tool to help eliminate NullPointerExceptions (NPEs) in your Java code with low build-time overhead
MIT License
3.63k stars 293 forks source link

NullAway configuration is incorrect #1038

Closed emelyanovkr closed 1 month ago

emelyanovkr commented 1 month ago

Hello,

I've tried to search for the simillar issue but haven't found anything yet. When trying to build gradle project with Nullaway I'm having this exception:

An exception has occurred in the compiler (21.0.2). Please file a bug against the Java compiler via the Java bug reporting page  (https://bugreport.java.com) after checking the Bug Database (https://bugs.java.com) for duplicates. Include your program, the following   diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
java.lang.AssertionError: com.google.errorprone.scanner.ErrorProneInjector$ProvisionException: Failed to initialize com.uber.nullaway.NullAway
    ...
Caused by: java.lang.IllegalStateException: DO NOT report an issue to Error Prone for this crash!  NullAway configuration is incorrect.  Must specify annotated packages, using the -XepOpt:NullAway:AnnotatedPackages=[...] flag.  If you feel you have gotten this message in error report an issue at https://github.com/uber/NullAway/issues.
    at com.uber.nullaway.ErrorProneCLIFlagsConfig.<init>(ErrorProneCLIFlagsConfig.java:169)
    at com.uber.nullaway.NullAway.<init>(NullAway.java:294)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)

build.gradle configuration:

plugins {
    id 'com.gradleup.shadow' version '8.3.1'
    id 'java'
    id 'org.springframework.boot' version '3.3.3'
    id 'io.spring.dependency-management' version '1.1.6'
    id "net.ltgt.errorprone" version "4.0.1"
}

group = 'helium.video'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'

    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    runtimeOnly 'com.mysql:mysql-connector-j'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    // TODO: add spring security

    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

    errorprone 'com.uber.nullaway:nullaway:0.10.25'
    compileOnly "com.google.code.findbugs:jsr305:3.0.2"
    errorprone 'com.google.errorprone:error_prone_core:2.32.0'
    errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"

}

import net.ltgt.gradle.errorprone.CheckSeverity

tasks.withType(JavaCompile) {
    // remove the if condition if you want to run NullAway on test code
    if (!name.toLowerCase().contains("test")) {
        options.errorprone {
            check("NullAway", CheckSeverity.WARN)
            option("NullAway:AnnotatedPackages", "helium.video")
        }
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

I just don't understand what the issue with annotating packages when I'm using this option option("NullAway:AnnotatedPackages", "helium.video").

JDK: 21
nullaway: 0.10.25
erorr_prone_core: 2.32.0
msridhar commented 1 month ago

This one is a bit puzzling. I assume this code is proprietary and you don't have an open-source reproducer?

Could you try with the latest NullAway version, 0.11.2? Though I don't think that should be the issue. I'm wondering why you are on 0.10.25? Is this behavior a regression?

emelyanovkr commented 1 month ago

I assume this code is proprietary and you don't have an open-source reproducer? What do you mean this code is proprietary? It's just a build.gradle configuration, standard, I think.

I'm wondering why you are on 0.10.25 You know its really strange, cause I was relying on IDE, because it always give me the latest version of libraries to upgrade. However, I didn't get any suggestions to update on this one, that why I stayed on this version. That one was from baeldung guide, as I remember.

And I still got the same error after changing version. This error happens when I'm using build command from gradle.

Also, I think, it might be useful to look at this message: > Task :compileTestJava FAILED So I guess it's failing compileTestJava task.

msridhar commented 1 month ago

Sorry, by proprietary, I meant that it would be difficult for you to create a public GitHub repo where I could reproduce the issue. Generally speaking if we can get such a repo it helps us to debug.

That said, based on what you wrote I have a guess as to the issue here. Can you try tweaking your Gradle config like this:

tasks.withType(JavaCompile) {
        options.errorprone {
            option("NullAway:AnnotatedPackages", "helium.video")
        }
    }
}

NullAway runs at the warning level by default so the check("NullAway", CheckSeverity.WARN) shouldn't be needed. With this updated config the NullAway:AnnotatedPackages option will always be set. Does that fix the issue? If so, I will update our documentation.

emelyanovkr commented 1 month ago

Sorry, by proprietary, I meant that it would be difficult for you to create a public GitHub repo where I could reproduce the issue. Generally speaking if we can get such a repo it helps us to debug.

Ah, no, it's a public repo, just check this: https://github.com/emelyanovkr/VideoArchiveService/tree/working_branch Please refer to the working_branch.

Does that fix the issue?

Yeah, that's fixed the issue, thanks for you fast response!

However, if I would like to don't use NullAway in test packages, how can I do it?

msridhar commented 1 month ago

Here is some updated config that will turn off NullAway completely on tests:

tasks.withType(JavaCompile) {
  options.errorprone {
    // always set annotated packages to be safe
    option("NullAway:AnnotatedPackages", "com.foo")
  }
  if (name.toLowerCase().contains("test")) {
    options.errorprone {
      disable("NullAway")
    }
  }
}
emelyanovkr commented 1 month ago

This works well, thank you!