openrewrite / rewrite

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

Recipe fails silently #4200

Closed timo-a closed 6 months ago

timo-a commented 6 months ago

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>5.31.0</version>
  <configuration>
    ... 
  </configuration>
</plugin>

What is the way to reproduce the problem?

  1. Checkout https://github.com/timo-a/rewrite-recipe-starter/tree/demo/indexoutofBoundsException (the recipe), specifically https://github.com/timo-a/rewrite-recipe-starter/commit/0372af55b5851b2cff78e227e91836621a90b299.
  2. run publishToMavenLocal in gradle
  3. checkout https://github.com/timo-a/jackson-core/tree/demo/indexoutofBoundsException (the sample code base), specifically https://github.com/timo-a/jackson-core/commit/69d59808406d8d19bfcf935d3cc6908992699b2b
  4. run mvn -P openrewrite org.openrewrite.maven:rewrite-maven-plugin:run -l rewrite.log
  5. check the repo if any files changed. a. If no files changed, decrease the value of the variable limit in RightnameGetter and goto item 2. b. If files have changes, increase the value of the variable limit in RightnameGetter and goto item 2.

As you can observe the recipe works if called with a low enough value for the variable limit.

What's the Problem?

  1. When it doesn't work, there is no indication as to why. There is a "build success" at the end and no files change, same as if there was simply nothing to do. But as we can see with lower values for limit there is something to do. When there are enough findings the recipe simply doesn't do anything and pretends that everything is fine. Instead, OpenRewrite should tell me in the log that the recipe failed and give a clue whether this is because of the recipe or an internal OpenRewrite bug.

  2. Possible Bug in OpenRewrite. If the recipe doesn't work then there are no System.out logs printed after this line: https://github.com/timo-a/rewrite-recipe-starter/blob/0372af55b5851b2cff78e227e91836621a90b299/src/main/java/io/github/timoa/lombok/RightnameGetter.java#L142-L146

         Map<String, String> collect = acc.renameRecords.stream().limit(limit)
                .collect(Collectors.toMap(
                        rr -> String.format("%s.%s %s()", rr.package_, rr.className_, rr.methodName_),
                        rr -> rr.newMethodName_)
        );

    Indicating that this is where the execution stops. I'm not sure why that is. a. I could have programmed it wrong. If I did, I don't understand why, it does work for lower values. Maybe I used the API in a way that it not intended? Then I would appreciate an explanation and if applicable a refined documentation. b. Java 8 can't handle streams longer than 150 elements being collected into maps. Unlikely c. OpenRewrite has a memory bottleneck and fails silently if that happens. Sort of unlikely since the logs stop being printed long before I would expect.

Context

The point of the recipe is to accumulate methods that should be renamed. In a second step these renamings are executed. I've implemented this with a scanning recipe that scans for renaming opportunities and passes those to the visitor of a modified ChangeMethodName that can rename several methods through loops.

Are you interested in contributing a fix to OpenRewrite?

No

timo-a commented 6 months ago

There is definitely a user error involved(duplicates in list due to inner classes), closing this for now.