openrewrite / rewrite-gradle-plugin

OpenRewrite's Gradle plugin.
Apache License 2.0
60 stars 37 forks source link

Gradle plugin 6.6.5 and up leaving out (removing) lombok imports #264

Closed aholland closed 7 months ago

aholland commented 8 months ago

What version of OpenRewrite are you using?

I have tried multiple versions, of this plugin. The problem occurs with 6.6.5 and up. 6.6.4 is okay.

I am using

How are you running OpenRewrite?

Gradle plugin

plugin id("org.openrewrite.rewrite") version("6.6.4")
rewrite {
    activeRecipe("org.openrewrite.java.migrate.UpgradeToJava17")
}
dependencies:
    rewrite(platform("org.openrewrite.recipe:rewrite-recipe-bom:2.6.2"))
    rewrite("org.openrewrite.recipe:rewrite-migrate-java")

Project not public unfortunately.

To reproduce:

Run on this file:

import java.math.BigDecimal;
import javax.persistence.Embeddable;
import lombok.*;

@Data
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class Money {

  @NonNull
  String currency;

  @NonNull
  BigDecimal value;
}

./gradlew rewriteRun with plugin 6.6.4 will produce this:

import java.math.BigDecimal;
import javax.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@Data
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class Money {

  @NonNull
  String currency;

  @NonNull
  BigDecimal value;
}

But 6.6.5 and up will produce this, leaving out the Lombok imports, so the code won't compile, and it will do this across hundreds of files:

import java.math.BigDecimal;
import javax.persistence.Embeddable;

@Data
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class Money {

  @NonNull
  String currency;

  @NonNull
  BigDecimal value;
}

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

./gradlew rewriteRun

What did you expect to see?

Version of code with lombok imports.

What did you see instead?

Version of code with no lombok imports.

What is the full stack trace of any errors you encountered?

There is no exception thrown.

Are you interested in contributing a fix to OpenRewrite?

I might be willing - pretty busy in my role though so not sure when I would get onto it. More likely, since this was introduced last week between 6.6.4 and 6.6.5, that developer will be able to see what has happened quite quickly.

timtebeek commented 8 months ago

Thanks for the detailed report! No idea what might have caused this yet, but definitely something we should fix.

I'm mostly expecting the version of the rewrite-recipe-bom to play in here though, not as much the version of the plugin. Did you bump those in sync? What version did you use with Gradle plugin 6.6.4?

We typically list the version that work well together on the release notes here: https://github.com/openrewrite/rewrite-recipe-bom/releases

We did have someone report a regression to our types in use, which feeds into remove unused imports:

Perhaps the fix is already in the latest snapshots for you? See our snapshot versions too

aholland commented 8 months ago

The latest.integration version of plugin also removes all the Lombok imports I'm afraid. All I change is the version of the plugin from latest.integration to 6.6.4 and it works fine.

timtebeek commented 8 months ago

There was a regression in TypesInUse that was fixed in openrewrite/rewrite v8.13.4+ https://github.com/openrewrite/rewrite/commit/b716dc3e3d1d1d1d456027ecbc1910fff6b383d3

From the releases page it seems rewrite-gradle-plugin 6.7+ would have been the first to pick up that fix; could you try https://github.com/openrewrite/rewrite-gradle-plugin/releases/tag/v6.7.1 ? 🙏🏻

aholland commented 8 months ago

Sorry to report 6.7.1 has exactly the same behaviour as 6.6.5 through 6.7.0. That's not surprising though because yesterday I tried latest.integration which is after 6.7.1.

timtebeek commented 7 months ago

Thanks again for the report @aholland ! We saw a similar issue with Lombok yesterday, which led to the following change (a revert)

This has been releases as part of v6.8.1 of the rewrite-gradle-plugin; would you mind telling us if that indeed fixes it for you too?

timtebeek commented 7 months ago

I'm going to assume the new release has indeed fixed things for you. Let me know if that's any different!