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

Groovy PowerAssertions don't show a useful message when being CPS transformed

      PowerAssertions are a really useful feature of the groovy programming language, as they give you the full information on what is not as it is expected in the error message - the full assert statement with all information about the values.

      However, when running CPS transformed, the output is less useful, as it only states what the assertion was that failed, without the values.
      Take for example the following pipeline script:

      node {
          def a = 4
          stage('without cps') {
              nonCps(a)
          }
      
          stage('with cps') {
              def b = [1, 2, 3]
              try {
                  assert a == b[0]
              } catch (Throwable e) {
                  echo e.class.toString()
                  echo e.message
                  echo e.stackTrace.join('\n')
              }
          }
      }
      
      @NonCPS
      def nonCps(a) {
          def b = [1, 2, 3]
          try {
              assert a == b[0]
          } catch (Throwable e) {
              echo e.class.toString()
              echo e.message
              echo e.stackTrace.join('\n')
          }
      }
      

      Although the code is the same for NonCPS and with CPS,

      NonCPS prints the following:

      assert a == b[0]
             |    |
             |    1
             false
      

      (I.e. the value of the whole expression and the value of b[0],

      while the CPS transformed version prints no information whatsoever:

      assert a == b[0]
      

      N.B.: running the same code in a groovysh actually gives even more information:

      assert a == b[0]
             | |  ||
             4 |  |1
               |  [1, 2, 3]
               false
      

      That one can be worked around by liberally throwing parentheses at the problem, but that again only works in the NonCPS case:

      assert (a == (b[0]))
             ||    ||
             |4    |[1, 2, 3]
             false 1
      

      This makes the assert statement almost useless for writing test jobs for your global library methods, so it would be a really nice addition if the information could be preserved and be added to the message of the PowerAssertionError.

          [JENKINS-41751] Groovy PowerAssertions don't show a useful message when being CPS transformed

          Martin Sander created issue -
          Martin Sander made changes -
          Remote Link New: This issue links to "AssertBlock.java#L47 the line where the sourceCode is passed as the error message (Web Link)" [ 15386 ]
          Jesse Glick made changes -
          Component/s Original: pipeline [ 21692 ]
          Jesse Glick made changes -
          Epic Link New: JENKINS-35390 [ 171183 ]
          Reinhold Füreder made changes -
          Link New: This issue is related to JENKINS-47736 [ JENKINS-47736 ]
          Oleg Nenashev made changes -
          Labels New: JEP-200
          Jesse Glick made changes -
          Assignee New: Jesse Glick [ jglick ]
          Jesse Glick made changes -
          Component/s New: workflow-api-plugin [ 21711 ]
          Component/s Original: workflow-cps-plugin [ 21713 ]
          Jesse Glick made changes -
          Remote Link New: This issue links to "PR 64 (Web Link)" [ 20079 ]
          Jesse Glick made changes -
          Link New: This issue relates to JENKINS-49025 [ JENKINS-49025 ]
          Jesse Glick made changes -
          Resolution New: Fixed [ 1 ]
          Status Original: Open [ 1 ] New: Resolved [ 5 ]

            jglick Jesse Glick
            0x89 Martin Sander
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: