openrewrite / rewrite

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

Gradle parser fails with a trailing comma in method call #4614

Open mccartney opened 1 month ago

mccartney commented 1 month ago

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

    @Test
    void trailingComma() {
        rewriteRun(
          buildGradle(
            """
              plugins {
                  id 'java-library'
              }
              dependencies {
                  implementation platform("commons-lang:commons-lang:2.6", )
              }
              """
          )
        );
    }

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

java.lang.AssertionError: Source file was parsed into an LST that contains non-whitespace characters in its whitespace. This is indicative of a bug in the parser. 
plugins {
    id 'java-library'
}
dependencies {
    implementation platform("commons-lang:commons-lang:2.6"~~(non-whitespace)~~>, <~~)
}
    at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:323)
    at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:132)
    at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:127)
    at org.openrewrite.gradle.GradleParserTest.trailingComma(GradleParserTest.java:199)

Context

shanman190 commented 1 month ago

I think this one needs to be verified with Gradle natively also before trying to fix the parser. I'd almost expect for Gradle to fail as well which is what I'd want to see verified.

mccartney commented 1 month ago

@shanman190 Thanks for looking into this. I've set up a dedicated Gradle project for reproducing the issue with simple gradlew init: https://github.com/mccartney/rewrite-repro-4614-gradle/commit/f5b959e3b386b12ce84df29d5a5456ab5ee31b56 The Gradle build passes with and without this commit.

Also - the example has been extracted from real-life Gradle configuration file.

mccartney commented 3 weeks ago

Also checked Groovy program in tio.run:

class Example {
   static void main(String[] args) {
      println('Hello World',);
   }
}

and it worked fine.

mccartney commented 3 weeks ago
    @Test
    void trailingCommaInMethodCall() {
        rewriteRun(
          groovy(
            """
              System.out.println("Anwil Wloclawek", )
              """
          )
        );
    }

fails in rewrite-groovy the same way as original report.