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

Groovy closure composition doesn't work properly in scripted pipeline

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • groovy-plugin
    • None
    • Jenkins 2.89.4

      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):

          [JENKINS-50338] Groovy closure composition doesn't work properly in scripted pipeline

          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. 

          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. 

          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.

          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.

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

              Created:
              Updated: