Search code examples
bazelchecker-framework

Run Checker Framework with Bazel


Consider this github repository. https://github.com/dfabulich/bazel-checker-framework-bug

It includes a sample X.java file that flagrantly violates the rules of the @Nonnull annotation.

import javax.annotation.Nonnull;
public class X {
    public static void main(String[] args) {
        new X().exec();
    }

    public void exec() {
        System.out.println(this.method());
    }

    @Nonnull
    private String method() {
        return null;
    }
}

The WORKSPACE file just includes checker.jar.

maven_jar(
    name="checker",
    artifact="org.checkerframework:checker:2.3.1"
)

The BUILD file invokes the compiler with the checker framework configured as a java_plugin.

java_library(
    name='x',
    srcs=['X.java'],
    deps=['@checker//jar'],
    plugins=['checker'],
)

java_plugin(
    name='checker',
    deps=['@checker//jar'],
    processor_class='org.checkerframework.checker.nullness.NullnessChecker',
)

When I bazel build x, the build fails with this error:

error: InvocationTargetException when invoking constructor for class org.checkerframework.checker.nullness.KeyForAnnotatedTypeFactory; Underlying cause: java.lang.StackOverflowError; The Checker Framework crashed. Please report the crash. To see the full stack trace invoke the compiler with -AprintErrorStack

When I comment out the plugins line in the BUILD file, the build succeeds without error. (That makes sense, but I ultimately want the Checker Framework to fail this build with a return.type.incompatible error.)

Am I making a mistake here? Is this a bug in Bazel?


Solution

  • I got a good answer on the bazel-discuss mailing list, telling me to try NullAway, an Error Prone plugin that depends on the Checker Framework.

    The github repo now includes a working example using NullAway, like this.

    WORKSPACE:

    maven_jar(
        name='jsr305',
        artifact='com.google.code.findbugs:jsr305:3.0.2',
    )
    
    maven_jar(
        name="nullaway",
        artifact="com.uber.nullaway:nullaway:0.3.2"
    )
    
    maven_jar(
        name="guava",
        artifact="com.google.guava:guava:22.0",
    )
    

    BUILD:

    java_library(
        name='x',
        srcs=['X.java'],
        deps=['@jsr305//jar'],
        plugins=['nullaway'],
        javacopts=[
            '-Xep:NullAway:ERROR',
            '-XepOpt:NullAway:AnnotatedPackages=com.example',
        ],
    )
    
    java_plugin(
        name='nullaway',
        deps=[
            '@nullaway//jar',
            '@guava//jar',
        ],
    )
    

    (The dependency on guava is unfortunate; it's required because without Guava, NullAway refuses to load.)