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

Groovy closure composition doesn't work properly in scripted pipeline

    Details

    • Type: Bug
    • Status: Open (View Workflow)
    • Priority: Minor
    • Resolution: Unresolved
    • Component/s: groovy-plugin
    • Labels:
      None
    • Environment:
      Jenkins 2.89.4
    • Similar Issues:

      Description

      Closure composition works as expected in a regular Groovy shell, and also in the Jenkins script console; but if I run it in a pipeline I get odd results. My assumption is that however closures work in Pipeline, the leftshift / rightshift operators aren't overloaded to perform composition, but that's just a guess and I really don't know what I'm talking about.

      I think it'd be cool if it worked the same way

      Example (adapted from the Groovy docs):

      def plus2  = { it + 2 }
      def times3 = { it * 3 }
      
      def times3plus2 = plus2 << times3
      println "${times3plus2(3)} == 11"
      println "${times3plus2(4)} == ${plus2(times3(4))}"
      
      def plus2times3 = times3 << plus2
      println "${plus2times3(3)} == 15"
      println "${plus2times3(5)} == ${times3(plus2(5))}"
      
      // reverse composition
      println "${times3plus2(3)} == ${(times3 >> plus2)(3)}"
      

      Pipeline output (incorrect):

      Script console (correct):

        Attachments

          Activity

          Hide
          haridsv Hari Dara added a comment -

          I hit this same issue trying to use composition after verifying that it worked in script console. There is actually nothing odd about the result, it just stops at the first closure in the chain. This is true even for rightshift composition. 

          Show
          haridsv Hari Dara added a comment - I hit this same issue trying to use composition after verifying that it worked in script console. There is actually nothing odd about the result, it just stops at the first closure in the chain. This is true even for rightshift composition. 
          Hide
          haridsv Hari Dara added a comment -

          The composition is implemented by org.codehaus.groovy.runtime.ComposedClosure  and I can see that this is the type that gets created, so it may not be a problem with leftshit and rightshift not being implemented.

          Show
          haridsv Hari Dara added a comment - The composition is implemented by org.codehaus.groovy.runtime.ComposedClosure   and I can see that this is the type that gets created, so it may not be a problem with leftshit and rightshift not being implemented.

            People

            • Assignee:
              vjuranek vjuranek
              Reporter:
              grdryn Gerard Ryan
            • Votes:
              3 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: