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

Closures return early if they include a method call

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Won't Fix
    • Component/s: workflow-cps-plugin
    • Labels:
    • Environment:
      Jenkins 2.32.1, Pipeline Groovy 2.29, Groovy 2.4.7, latest Pipeline plugins (as of 03/23/17)
    • Similar Issues:

      Description

      Problem

      It seems that in a pipeline, Groovy closures are returned early if you call a method inside them. The closure then only returns the return value of the called method. I provided a minimal sample to reproduce the bug below.

      Steps to Reproduce

      • Execute the following code in a Jenkins pipeline
        class Foo {
            Closure<String> cl = { String a ->
                return "Say: ${this.bar(a)}"
            }
            String bar(String a) {
                return "Hello ${a}."
            }
        }
        Foo f = new Foo()
        String output = f.getCl()("World")
        echo(output)
        
      • It does not matter if the code is executed in the sandbox or not
      • The expected output, as tested with Groovy 2.4.7 locally, is:
        • Say: Hello World.
      • Jenkins however outputs:
        • Hello World.

        Attachments

          Activity

          Hide
          marbec Mark J. Becker added a comment -

          Can confirm, that this works. Thanks.

          class Foo {
              Closure<String> cl = { String a ->
                  return "Say: ${this.bar(a)}"
              }
              
              @NonCPS
              String bar(String a) {
                  return "Hello ${a}."
              }
          }
          Foo f = new Foo()
          String output = f.getCl()("World")
          echo(output)
          
          Show
          marbec Mark J. Becker added a comment - Can confirm, that this works. Thanks. class Foo { Closure< String > cl = { String a -> return "Say: ${ this .bar(a)}" } @NonCPS String bar( String a) { return "Hello ${a}." } } Foo f = new Foo() String output = f.getCl()( "World" ) echo(output)
          Hide
          jglick Jesse Glick added a comment -

          You may not call CPS-transformed code (such as bar in this example) from inside a GString expression. Use @NonCPS or use string concatenation.

          Show
          jglick Jesse Glick added a comment - You may not call CPS-transformed code (such as bar in this example) from inside a GString expression. Use @NonCPS or use string concatenation.

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            marbec Mark J. Becker
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: