Lombok compilation error: cannot find symbol

I was working on the service that powers this blog when I stumbled upon an odd issue.

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /app/service/src/main/java/ee/fakeplastictrees/blog/service/user/model/UserExceptionFactory.java:[8,1] cannot find symbol
  symbol:   static withData
  location: class ee.fakeplastictrees.blog.service.core.exceiption.PublicExceptionFactory

There were 52 more errors like this in different classes. I hadn't changed the Java version or any dependencies since the previous build. Moreover, most of the files that reported failures had absolutely no changes in them. I was able to build this code not long ago! It was as if lombok had suddenly stopped working for no obvious reason. Nothing in maven's log pointed to the root cause.

A confused man screaming at his laptop

Photo: not exactly me, but the same energy.

Search results were misleading and suggested that my project was configured incorrectly, even though it had worked in the exact same environment just a couple of weeks ago. Eventually, I found the answer:

There's a known bug in javac about statically importing generated methods. javac concludes that an annotation processor can't 'fix' it incorrectly and just throws out an error. The fix is to not statically import it.

Indeed, in just one class I replaced PublicExceptionFactory.withData(...) with withData(...) to save some horizontal space, which confused the compiler.

@UtilityClass
public class PublicExceptionFactory {
  public PublicException withData(String message, MessageDefinition definition, HttpStatus status) {
    return new PublicException(message, definition, status);
  }
}

As you can see, I didn't explicitly mark withData with a static keyword because the @UtilityClass annotation does that for us. According to the documentation:

A utility class is a class that is just a namespace for functions. No instances of it can exist, and all its members are static. All members of a utility class are automatically marked as static.

It even mentions my issue, which I was unaware of:

Due to limitations in javac, currently non-star static imports cannot be used to import stuff from @UtilityClasses; don't static import, or use star imports.

In essence, one problematic import produced numerous seemingly unrelated compilation errors across the whole project! That's why it was so bewildering to me. Now I know, and so do you!

Useful links