openrewrite / openrewrite/rewrite

GroovyTemplate reports error on gradle default syntax

Open
#5,410 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug groovy parser test provided
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

I am using latest version

How are you running OpenRewrite?

Unit test in rewrite-groovy or rewrite-gradle

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

    @Test
    void gradleGroovySyntaxWorks() {
        @Language("groovy")
        String enableDependencyLocking = """
          dependencyLocking {
              lockAllConfigurations()
          }
          """;
        rewriteRun(
          spec -> spec.recipe(toRecipe(() -> new GroovyVisitor<>() {

              @Override
              public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
                  return GroovyTemplate.builder(enableDependencyLocking)
                    .build()
                    .apply(getCursor(), method.getCoordinates().replace());
              }
          })),
          groovy(
            """
            println("foo")
            """,
            enableDependencyLocking
          ));
    }

What did you expect to see?

According to the gradle docs it should be valid syntax.

What did you see instead?

IllegalArgumentException: Could not parse as Java

What is the full stack trace of any errors you encountered?

java.lang.AssertionError: Failed to run recipe at Cursor{MethodInvocation->CompilationUnit->root}

	at org.openrewrite.test.RewriteTest.lambda$defaultExecutionContext$14(RewriteTest.java:640)
	at org.openrewrite.scheduling.RecipeRunCycle.handleError(RecipeRunCycle.java:289)
	at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$8(RecipeRunCycle.java:229)
	at org.openrewrite.scheduling.RecipeStack.reduce(RecipeStack.java:60)
	at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$9(RecipeRunCycle.java:178)
	at org.openrewrite.internal.InMemoryLargeSourceSet.lambda$edit$0(InMemoryLargeSourceSet.java:83)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:244)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:267)
	at org.openrewrite.internal.InMemoryLargeSourceSet.edit(InMemoryLargeSourceSet.java:82)
	at org.openrewrite.scheduling.RecipeRunCycle.editSources(RecipeRunCycle.java:177)
	at org.openrewrite.RecipeScheduler.runRecipeCycles(RecipeScheduler.java:84)
	at org.openrewrite.RecipeScheduler.scheduleRun(RecipeScheduler.java:41)
	at org.openrewrite.Recipe.run(Recipe.java:441)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:377)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:130)
	at org.openrewrite.groovy.GroovyTemplateTest.gradleGroovySyntaxWorks(GroovyTemplateTest.java:68)
	at java.base/java.lang.reflect.Method.invoke(Method.java:569)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.openrewrite.internal.RecipeRunException: java.lang.IllegalArgumentException: Could not parse as Java
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:281)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:154)
	at org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:120)
	at org.openrewrite.groovy.GroovyTemplateTest$2.visitMethodInvocation(GroovyTemplateTest.java:75)
	at org.openrewrite.groovy.GroovyTemplateTest$2.visitMethodInvocation(GroovyTemplateTest.java:69)
	at org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:4065)
	at org.openrewrite.java.tree.J.accept(J.java:58)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
	at org.openrewrite.groovy.GroovyVisitor.lambda$visitCompilationUnit$0(GroovyVisitor.java:44)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:244)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:267)
	at org.openrewrite.groovy.GroovyVisitor.visitCompilationUnit(GroovyVisitor.java:44)
	at org.openrewrite.groovy.tree.G$CompilationUnit.acceptGroovy(G.java:212)
	at org.openrewrite.groovy.tree.G.accept(G.java:47)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:154)
	at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$7(RecipeRunCycle.java:209)
	at io.micrometer.core.instrument.AbstractTimer.recordCallable(AbstractTimer.java:147)
	at org.openrewrite.table.RecipeRunStats.recordEdit(RecipeRunStats.java:67)
	at org.openrewrite.scheduling.RecipeRunCycle.lambda$editSources$8(RecipeRunCycle.java:205)
	... 16 more
Caused by: java.lang.IllegalArgumentException: Could not parse as Java
	at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:279)
	at java.base/java.util.Optional.orElseThrow(Optional.java:403)
	at org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:279)
	at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$parseBlockStatements$9(JavaTemplateParser.java:178)
	at org.openrewrite.java.internal.template.JavaTemplateParser.lambda$cacheIfContextFree$14(JavaTemplateParser.java:302)
	at org.openrewrite.java.internal.template.JavaTemplateParser.cache(JavaTemplateParser.java:323)
	at org.openrewrite.java.internal.template.JavaTemplateParser.cacheIfContextFree(JavaTemplateParser.java:302)
	at org.openrewrite.java.internal.template.JavaTemplateParser.parseBlockStatements(JavaTemplateParser.java:173)

Are you interested in contributing a fix to OpenRewrite?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with GroovyTemplateTest.gradleGroovySyntaxWorks in rewrite-groovy, then trace GroovyTemplate.apply and the JavaTemplateParser entry point named in the stack trace. Reproduce the failure with the supplied test and verify that the Gradle dependencyLocking syntax is accepted without the Java parsing exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.