openrewrite / rewrite-maven-plugin

OpenRewrite's Maven plugin.
https://openrewrite.github.io/rewrite-maven-plugin/plugin-info.html
Apache License 2.0
141 stars 72 forks source link

Exceptions cannot be traced back to failing recipe #664

Closed Bananeweizen closed 5 months ago

Bananeweizen commented 11 months ago

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

Running rewrite with several hundred recipes enabled, one of them failing.

What did you expect to see?

The name of the failing recipe.

What did you see instead?

[ERROR] The recipe produced an error. Please report this to the recipe author.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
...

The nested exception rethrown by the maven plugin looks like:

Error while visiting plugins\com.etas.esdl\src\com\etas\esdl\sysconst\SystemConstantProfileSerializer.java: org.openrewrite.analysis.trait.util.TraitErrorsException: TraitErrors:
[ERROR]         - Method must be created from class org.openrewrite.java.tree.J$MethodDeclaration but was class org.openrewrite.java.tree.J$Lambda
[ERROR]         - No parent Method found
[ERROR]         - Field must be declared in a class, interface, or anonymous class
[ERROR]   org.openrewrite.analysis.trait.util.TraitErrors.doThrow(TraitErrors.java:43)
[ERROR]   fj.data.Either$RightProjection.on(Either.java:531)
[ERROR]   fj.data.Validation.on(Validation.java:146)
[ERROR]   org.openrewrite.analysis.trait.expr.VarAccessBase$1.visitVariable(VarAccess.java:187)
[ERROR]   org.openrewrite.analysis.trait.expr.VarAccessBase$1.visitVariable(VarAccess.java:175)
[ERROR]   org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5758)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
[ERROR]   ...
[ERROR] -> [Help 2]

That contains the exception and the file, but still not the recipe. Also note that the stack trace looks shortened at the "...", and that is not Maven, but probably rewrites own code doing this.

Enabling Maven exception stack traces via -x doesn't help either. (it only leads to some more exceptions being shown, but those are related to unsuccessful dependency resolution, e.g. in the Maven Aether components).

The reported exceptions are created as runtime exceptions from the Markup.Error class. Eventually that would be a good place to add the current recipe as additional field. In that case this issue would need additional changes in the rewrite core project.

Are you interested in contributing a fix to OpenRewrite?

Yes, but some core contributor would need to judge the general direction of how to fix this first.

timtebeek commented 11 months ago

Sorry to hear! For context, the code that shortens that stacktrace is likely here. We can look into not shortening that stacktrace when -x is passed, but I'm not yet sure what would be needed for that to work. We can also look to change sanitizeStackTrace to at least contain the Class<?> until for instance.

For your immediate needs: there's only five recipes that use that trait analysis. You might want to exclude those if they trip over SystemConstantProfileSerializer.java, and report the issue with that class on https://github.com/openrewrite/rewrite-analysis/

timtebeek commented 10 months ago

Had another quick look to see if there's an easy way to extract the relevant error details in the Maven plugin. The recipe errors are likely recorded as a data table entry here: https://github.com/openrewrite/rewrite/blob/48677c38a156204a912f177e73f68e0e12e58a09/rewrite-core/src/main/java/org/openrewrite/RecipeScheduler.java#L315-L321 We don't yet have and option in the Maven plugin to export data tables, but that is available through the Moderne CLI if you want to explore that further.

Bananeweizen commented 5 months ago

BTW: The issue that led to this discussion is the same as in https://github.com/openrewrite/rewrite-java-security/issues/125. Closing this one, as finding exceptions in the Maven plugin is meanwhile easier.