openrewrite / rewrite

Automated mass refactoring of source code.

Home Page:https://docs.openrewrite.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option to enable recipes to leave a comment indicating auto-generated code

cjobinabo opened this issue · comments

What problem are you trying to solve?

After running several recipes against their code, some users may have difficulty distinguishing between what is theirs and what isn't. Also, in the event that a recipe causes some unexpected issues, users executing several recipes against their code will likely have trouble identifying which recipe was the source of the issue.

Describe the solution you'd like

Having a built-in option users could enable when executing rewrite-run that fingerprints each line of code modified by a recipe would help solve these problems. This fingerprint would be a comment indicating that the lines of code were autogenerated and what recipe generated the code.

Have you considered any alternatives or workarounds?

Additional context

Are you interested in contributing this feature to OpenRewrite?

Hi @cjobinabo ! Thanks for logging an issue; we have this information available through markers on the LST elements, and it's what we use to show why changes were made in the Moderne Platform.

image

It's also used to for instance log what recipes made changes in the Maven plugin:
https://github.com/openrewrite/rewrite-maven-plugin/blob/bbb8fceda21d419965902c26519330ef16858d31/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java#L489-L516

I'm not sure what the best way would be to show any additional information to the user. Especially when recipes are hierarchical, there's a lot more information than would fit well within a comments, and some code changes affect comments & JavaDoc, further complicating the output.

Perhaps there's alternative output formats we could build into the Maven/Gradle plugins that are better suited to annotate changes? This suggestion comes to mind

Thanks for following up on this issue, Tim. The instance log may be enough here. Do you know how we would access the log when running recipes via the Maven plugin?

We store that information in a marker per source file, not per line, but should typically be close enough

public class RecipesThatMadeChanges implements Marker {
@EqualsAndHashCode.Include
UUID id;
Collection<List<Recipe>> recipes;

Also note my suggestion on the above linked issue to produce those markers to a portable SARIF output format: openrewrite/rewrite-gradle-plugin#255 (comment)

We don't yet have a way to surface such output from either the Maven or Gradle plugin though, so we're a little further off from having something that you can immediately use.