openrewrite / rewrite-migrate-java

OpenRewrite recipes for migrating to newer versions of Java.
Apache License 2.0
92 stars 65 forks source link

Maven recipe library compilation issues with Lombok in tests when rewrite-templating is on the classpath #316

Closed alexpm-14 closed 8 months ago

alexpm-14 commented 9 months ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project. But this error is during compilation time, this is how I include rewrite dependencies

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.openrewrite.recipe</groupId>
                <artifactId>rewrite-recipe-bom</artifactId>
                <version>${rewrite-recipe-bom.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
...
</dependencyManagement>
<dependencies>
       <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-migrate-java</artifactId>
      </dependency>
....
      <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
            <version>${lombok.version}</version>
      </dependency>

What is the smallest, simplest way to reproduce the problem?

I was working with rewrite-recipe-bom:2.2.1 (rewrite-migrate-java:2.0.10) in a project to develop my own recipes, and I'm using lombok in my Main and Test classes (@Value, @Builder, etc.). Test classes are located in root packages. Everything was working but when I upgrade to rewrite-recipe-bom:2.3.1 (rewrite-migrate-java:2.1.1) I'm having errors at test-compilation phase because lombok isn't generating the code from annotations.

For example, for this java test class:

package utils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
class TestUtil {
    public static void myMethod() {
      ....
      log.error("error");
   }
}

What did you expect to see?

Project compile without errors

What did you see instead?

There are compilation errors ONLY in tests classes:

java: cannot find symbol
  symbol:   variable log
  location: class utils.TestUtil

And trying to find the cause, i've found that rewrite-migrate-java now includes rewrite-templating dependency and excluding it, it compiles correctly:

        <dependency>
         <groupId>org.openrewrite.recipe</groupId>
         <artifactId>rewrite-migrate-java</artifactId>
             <exclusions>
                <exclusion>
                    <groupId>org.openrewrite</groupId>
                    <artifactId>rewrite-templating</artifactId>
                </exclusion>
            </exclusions>
      </dependency>

Can you guess what is causing this issue? Thank you.

timtebeek commented 9 months ago

Hi @alexpm-14 ; thanks for the detailed report! No idea yet why exactly this fails for you, but I can tell you a bit more about rewrite-templating, such that it might help you narrow this down further. That module contains our own annotation processors, such as RefasterTemplateProcessor which allows developers to define recipes with convenient before and after annotated methods. We use this in for instance https://github.com/openrewrite/rewrite-migrate-java/blob/f04adfc61c52858a74f6b66e47135f01f55642d2/src/main/java/org/openrewrite/java/migrate/apache/commons/lang/ApacheCommonsStringUtils.java#L33-L42 also documented here: https://docs.openrewrite.org/recipes/java/migrate/apache/commons/lang/apachecommonsstringutilsrecipesusdabbreviaterecipe

timtebeek commented 9 months ago

I'm surprised to see the inclusion of rewrite-migrate-java, and the processors it includes, to cause Lombok to no longer function. Are you seeing any indication in the logs that there might be issues? And are you seeing these issues both in your IDE and on the commandline with Maven?

alexpm-14 commented 8 months ago

Hi again, I'm sorry for not answering this time. It looks that having those Annotation Processors might create a conflict in my developing environment. It's important to highlight that the issue is related to TEST compilation, main sources using lombok compile w/o errors.

Executing mvn clean test-compile with -X debug option I can find both executions (with and without rewrite-templating) include lombok in their classpaths but there isn't any error message from them.

Yes I'm having the same issues executing in my IDE: image

and with Maven: image

Check this: if I don't exclude rewrite-templating dependency but configure maven-compiler to use (only) lombok annotationProcessor (so rewrite-templating annotationProcessor is exluded) it compiles succesfully:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
        <compilerArgs>
            <arg>-parameters</arg>
            <arg>-encoding</arg>
            <arg>utf8</arg>
        </compilerArgs>
                <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
    </configuration>
</plugin>
timtebeek commented 8 months ago

Thanks for chiming back in; no idea still what might be the cause of this, but it sounds like you've found a workaround in explicitly configuring the annotation processor paths to use Lombok (only). Does that mean you're good enough to continue like that for now?

We don't often see folks develop rewrite recipe libraries with Maven, as most start from our template project that uses Gradle. Might be worth looking into that one if you keep having issues, and are open to that switch (as much as I like Maven).

I'm doubting a bit what to do with your ticket; it doesn't immediately seem actionable for either of us; and with the workaround it seems like you've found something that works for you. Would you be ok with closing this issue, with the option to reopen in more people report the same issue?

timtebeek commented 8 months ago

Closing for now as described above, although the comments remain open in case more folks have similar issues.

alexpm-14 commented 8 months ago

Thanks for chiming back in; no idea still what might be the cause of this, but it sounds like you've found a workaround in explicitly configuring the annotation processor paths to use Lombok (only). Does that mean you're good enough to continue like that for now?

Yes, we can try continue working with this workaround despite i found it a bit "dangerous" as we're not just excluding rewrite-templating's ann.processor but any other than lombok's.

Let me ask you something before leaving this topic: excluding rewrite-templating's ann.processor will make your recipes or any other recipes stop working as expected? (I assume no, because it should be necessary just to compile and generate those recipes libraries but not for use them)

Thanks again.

timtebeek commented 8 months ago

Some of our recipes depend on classes in rewrite-templating such as AbstractRefasterJavaVisitor.java, so I wouldn't exclude the dependency coming in, as that might break those recipes. Instead the workaround to configure the maven-compiler-plugin with explicit annotationProcessorPaths might be your best bet; hope that helps!