rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Gradle parser fails with a trailing comma in method call

Open mccartney opened this issue 1 year ago • 6 comments

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

  • AFAIK this is a valid Groovy-flavor Gradle file.
  • Obviously removing the comma makes the test pass.

mccartney avatar Oct 26 '24 19:10 mccartney

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.

shanman190 avatar Oct 26 '24 23:10 shanman190

@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 avatar Oct 27 '24 08:10 mccartney

Also checked Groovy program in tio.run:

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

and it worked fine.

mccartney avatar Nov 01 '24 17:11 mccartney

    @Test
    void trailingCommaInMethodCall() {
        rewriteRun(
          groovy(
            """
              System.out.println("Anwil Wloclawek", )
              """
          )
        );
    }

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

mccartney avatar Nov 01 '24 17:11 mccartney

As a quick update: from the Java side we're now modeling the trailing comma as a marker; that might help here as well

  • https://github.com/openrewrite/rewrite/pull/4869

timtebeek avatar Jan 09 '25 09:01 timtebeek

This is the place where likely the marker logic needs to be added.

greg-at-moderne avatar Apr 30 '25 13:04 greg-at-moderne

Proposed fix in: https://github.com/openrewrite/rewrite/pull/5711

e5LA avatar Jul 03 '25 20:07 e5LA