rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Jenkinsfile with annotated List with array initializer not print idempotent

Open Bananeweizen opened this issue 1 month ago • 2 comments

What version of OpenRewrite are you using?

Any maven plugin after 6.19, including current SNAPSHOT. The problem is caused by d9ced301292550dd6d1026deca910c16e60d3ea1, I verified using a SNAPSHOT created before and after that commit.

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

import groovy.transform.Field

@Field def foo

// list of pull requests created
@Field List<String> newPullRequests = []

pipeline {
    stages {
        stage('Setup') {
            steps {
                script {
                    // empty
                }
            }
        }
    }
}

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

[WARNING] There were problems parsing some source files
[WARNING] There were problems parsing Jenkinsfile
[WARNING] java.lang.IllegalStateException: Jenkinsfile is not print idempotent.
diff --git a/C:/dev/etasdev/git/rewrite/Jenkinsfile b/C:/dev/etasdev/git/rewrite/Jenkinsfile
index 6c3731e..322561c 100644
--- a/C:/dev/etasdev/git/rewrite/Jenkinsfile
+++ b/C:/dev/etasdev/git/rewrite/Jenkinsfile
@@ -2,8 +2,7 @@

 @Field def foo

-// list of pull requests created
-@Field List<String> newPullRequests = []
+newPullRequests = []

 pipeline {
     stages {

  org.openrewrite.Parser.requirePrintEqualsInput(Parser.java:52)

Be aware that this snippet works fine if the comments are removed.

Are you interested in contributing a fix to OpenRewrite?

Sorry, no. Missing time.

Bananeweizen avatar Nov 03 '25 08:11 Bananeweizen

hi! Looks like this might not be specific to the change in the Maven plugin, as it already fails in the groovy parser:

    @Test
    void jenkinsFile() {
        rewriteRun(
          groovy(
            """
              import groovy.transform.Field

              @Field def foo

              // list of pull requests created
              @Field List<String> newPullRequests = []

              pipeline {
                  stages {
                      stage('Setup') {
                          steps {
                              script {
                                  // empty
                              }
                          }
                      }
                  }
              }
              """
          )
        );
    }

timtebeek avatar Nov 03 '25 10:11 timtebeek

Looks like we'd need special handling for @Field, as well as improve the handling when there's successive fields. A little more complicated than I can fit in right now, so I'll put it on the backlog.

timtebeek avatar Nov 03 '25 11:11 timtebeek