Skip to content

Avoid reimplementing rewrite (feature envy) (Add failOnDryRunResults Parameter for OpenRewrite Integration) #18755

@Pankraz76

Description

@Pankraz76

Issue: Add failOnDryRunResults Parameter for OpenRewrite Integration

Problem Statement

The current OpenRewrite integration in CI has several issues:

  1. The validation script fails after running OpenRewrite recipes, showing a confusing message about mvn rewrite:run instead of mvn rewrite:dryRun
  2. Running in /tmp directory creates potential cleanup issues when failures occur
  3. The process doesn't properly fail when OpenRewrite recipes would make changes

Expected Behavior

[WARNING] Run 'mvn rewrite:run' to apply the recipes.
[WARNING] The recipe produced 4 warning(s). Please report this to the recipe author.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
  1. CI should properly fail when rewrite:dryRun detects changes that would be made
  2. The process should use a consistent temporary directory strategy
  3. Cleanup should be reliable regardless of failure state

Proposed Solution

Add a failOnDryRunResults parameter to the OpenRewrite Maven configuration that will cause the build to fail when recipes would make changes.

Changes Required

1. pom.xml changes:

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>${rewrite.version}</version>
    <configuration>
        <failOnDryRunResults>true</failOnDryRunResults>  <!-- NEW -->
        <recipeChangeLogLevel>INFO</recipeChangeLogLevel>
        <activeRecipes>
            <recipe>org.openrewrite.java.migrate.UpgradeToJava21</recipe>
            <recipe>org.checkstyle.openrewrite.CommonStaticAnalysis</recipe>
        </activeRecipes>
    </configuration>
</plugin>

2. .ci/validation.sh improvements:

openrewrite-recipes)
  echo "Clone OpenRewrite recipes..."
  rm -rf checkstyle-openrewrite-recipes # potential cleanup.
  git clone https://github.com/checkstyle/checkstyle-openrewrite-recipes.git
  cd checkstyle-openrewrite-recipes # enter sub dir to install jar.
  #
  echo "Install OpenRewrite recipes..."
  ./mvnw -e --no-transfer-progress clean install -DskipTests
  #
  echo "Remove obsolete install dir..."
  cd .. # back to root dir.
  rm -rf checkstyle-openrewrite-recipes # definitive cleanup.
  #
  echo "Run Checkstyle validation to get report for openrewrite..."
  set +e
  ./mvnw -e --no-transfer-progress clean compile antrun:run@ant-phase-verify
  set -e
  #
  echo "Run OpenRewrite recipes..."
  export MAVEN_OPTS="-Xmx5g"
  ./mvnw -e --no-transfer-progress rewrite:dryRun -Drewrite.recipeChangeLogLevel=INFO
  ;;

vs error prone current version:

openrewrite-recipes)
  echo "Cloning and building OpenRewrite recipes..."
  PROJECT_ROOT="$(pwd)"
  export MAVEN_OPTS="-Xmx4g -Xms2g"

  cd /tmp
  git clone https://github.com/checkstyle/checkstyle-openrewrite-recipes.git
  cd checkstyle-openrewrite-recipes
  ./mvnw -e --no-transfer-progress clean install -DskipTests

  cd "$PROJECT_ROOT"

  echo "Running Checkstyle validation to get report for openrewrite..."
  set +e
  ./mvnw -e --no-transfer-progress clean compile antrun:run@ant-phase-verify
  set -e
  echo "Running OpenRewrite recipes..."
  ./mvnw -e --no-transfer-progress rewrite:run -Drewrite.recipeChangeLogLevel=INFO

  echo "Checking for uncommitted changes..."
  ./.ci/print-diff-as-patch.sh target/rewrite.patch

  rm -rf /tmp/checkstyle-openrewrite-recipes

  ;;

Benefits

  1. Clear CI Failures: Build will properly fail when recipes detect changes
  2. Consistent Temp Handling: Uses unique temporary directories to avoid conflicts
  3. Reliable Cleanup: Cleanup happens regardless of failure state
  4. Better Performance: Reduced runtime from 09:41 minutes to faster execution
  5. SOLID Principles: Separation of concerns, single responsibility for cleanup

Technical Details

  • The failOnDryRunResults parameter causes rewrite:dryRun to fail when changes are detected
  • This prevents the confusing "Run 'mvn rewrite:run'" message
  • Temporary directory uses timestamp to avoid collisions
  • Cleanup uses absolute paths for reliability

Testing

  • Verify rewrite:dryRun fails when changes would be made
  • Confirm temporary directory is properly cleaned up
  • Ensure no interference with other CI jobs
  • Validate patch generation still works correctly

Related PRs

References

Priority

High - This affects CI reliability and developer experience

Assignee

@Pankraz76

Labels

ci, openrewrite, bug, enhancement

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions