Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-59911

Result of assignment expression is null rather than assigned value

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None

      Hello Guys,

      I found some weird behavior difference in executing the same groovy logic in pipeline from the original groovy 2.4.12 (used in the latest LTS Jenkins by default).

      The next logic is working in different ways for pipeline and script console:

      def b = 3
      def a = b = 4
      print("${a} ${b}")
      
      • Groovy console print output: "4 4"
      • Pipeline echo output: "null 4"

      Who can tell why? I just can't catch the difference.

          [JENKINS-59911] Result of assignment expression is null rather than assigned value

          jglick maybe you can help with this question?

          Sergei Parshev added a comment - jglick maybe you can help with this question?

          Jesse Glick added a comment -

          Jesse Glick added a comment - A bug in https://github.com/cloudbees/groovy-cps presumably.

          Oh, I thought it's just a "feature" in realization like "don't use `.each`" and stuff... Ok, good to know, thank you)

          Sergei Parshev added a comment - Oh, I thought it's just a "feature" in realization like "don't use `.each`" and stuff... Ok, good to know, thank you)

          Sergei Parshev added a comment - - edited

          Yeah, I added a test to groovy-cps:

          @Test
          void justatest() {
              assert evalCPS('''
                  def a = 3;
                  def b = 4;
                  a = b = 6;
                  return "${a}, ${b}";
              ''')=="6, 6";
          }
          

          and it's failed:

          [ERROR] Failures:
          [ERROR]   CpsTransformerTest.justatest:50->AbstractGroovyCpsTest.evalCPS:50 assert resultInCps==sh.evaluate(script)
                 |          | |  |        |
                 null, 6    | |  6, 6
                            | |                       def a = 3;
                            | |                       def b = 4;
                            | |                       a = b = 6;
                            | |                       return "${a}, ${b}";
                            | |
                            | groovy.lang.GroovyShell@6ae42248
                            false
          

          Sergei Parshev added a comment - - edited Yeah, I added a test to groovy-cps: @Test void justatest() { assert evalCPS(''' def a = 3; def b = 4; a = b = 6; return "${a}, ${b}" ; ''')== "6, 6" ; } and it's failed: [ERROR] Failures: [ERROR] CpsTransformerTest.justatest:50->AbstractGroovyCpsTest.evalCPS:50 assert resultInCps==sh.evaluate(script) | | | | | null, 6 | | 6, 6 | | def a = 3; | | def b = 4; | | a = b = 6; | | return "${a}, ${b}"; | | | groovy.lang.GroovyShell@6ae42248 false

          Jan Gałda added a comment -

          Any updates on this? I also faced this issue but in a different situation.

           

          If assignment is a last line of a method, the method returns always null, while in regular groovy it returns actual value.

          Simplest example:

          def abc() {
            def a = 'should return this'
          } 

          Above method returns "should return this" in groovy, but null while used in pipeline.

          Above example is simple and easy to workaround, but it is just to expose that developer cannot trust really basic features.

          Jan Gałda added a comment - Any updates on this? I also faced this issue but in a different situation.   If assignment is a last line of a method, the method returns always null , while in regular groovy it returns actual value. Simplest example: def abc() { def a = 'should return this ' } Above method returns "should return this" in groovy, but null while used in pipeline. Above example is simple and easy to workaround, but it is just to expose that developer cannot trust really basic features.

            Unassigned Unassigned
            sparshev Sergei Parshev
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: