openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.29k stars 340 forks source link

SimplifyBooleanExpression auto-formats after running #502

Closed jbduncan closed 3 years ago

jbduncan commented 3 years ago

I've been experimenting with rewrite for a pet project, but I've noticed that the recipe org.openrewrite.java.cleanup.SimplifyBooleanExpression runs AutoFormatVisitor unconditionally, which causes the formatting of my code to change.

This is a problem for me, because my project uses rewrite-gradle-plugin, and I've tweaked the task rewriteDryRun to fail the build if it would have applied any refactorings. Therefore, when I use SimplifyBooleanExpression, my build fails for the wrong reason.

How to reproduce

  1. git clone https://github.com/jbduncan/guava-graph-utils.git
  2. cd guava-graph-utils
  3. Add   - org.openrewrite.java.cleanup.SimplifyBooleanExpression - spaces included - to the recipeList: section of file config/rewrite.yml.
  4. Run ./gradlew rewriteDryRun with Java 11.

Expected result

The source code remains unchanged and the build passes.

Actual result

The build fails with the message:

Execution failed for task ':rewriteDryRunTest'.
> Not all refactorings have been applied. See '/path/to/guava-graph-utils/build/reports/rewrite/test.patch'.

This is a custom error message that my build produces. The source code can be found at the bottom of build.gradle.kts.

jkschneider commented 3 years ago

Thanks for the report @jbduncan. Auto-formatting by recipes like this at most should format the code being changed. We can work to limit it to that.

jbduncan commented 3 years ago

Great, thanks for tackling this so quickly! I'm looking forward to testing it out in a future version.

Having said that, I wonder if I could use Jitpack or the Maven snapshots repo to try it out earlier... 🤔

jkschneider commented 3 years ago

@jbduncan Yes, all successful CI builds are published to sonatype's snapshot repo: https://oss.sonatype.org/content/repositories/snapshots/org/openrewrite/rewrite-java/7.4.0-SNAPSHOT/.

jbduncan commented 3 years ago

@jkschneider Thank you! I can confirm when I apply rewrite-java 7.4.0-SNAPSHOT to my Gradle build like so:

repositories {
    ...
    maven("https://oss.sonatype.org/content/repositories/snapshots")
}

dependencies {
    ...
    rewrite("org.openrewrite:rewrite-java:7.4.0-SNAPSHOT")
    ...
}

...and then add org.openrewrite.java.cleanup.SimplifyBooleanExpression to my rewrite.yml file, and finally run ./gradlew rewriteDryRun, that it all passes. :tada:

I won't actually commit this snapshot version in my repo to ensure that my build is reproducible, so I'm looking forward to when 7.3.1 or 7.4.0 comes out. :)