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

Closure delegation broken in shared libraries

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • groovy-plugin
    • None
    • Jenkins 2.115
      Pipeline: API 2.26
      Pipeline: Groovy 2.47

      Closures in a shared library with a resolveStrategy of DELEGATE_FIRST can only look up names in the delegate, as if the strategy were set to DELEGATE_ONLY.

      For example, I have a shared library with the following file:

      vars/testlib.groovy
      def foo() {
          echo 'foo'
      }
      
      def bar(body) {
          body.delegate = [:]
          body.resolveStrategy = Closure.DELEGATE_FIRST
          body()
      }
      

      And a Pipeline script as follows:

      Jenkinsfile
      library 'testlib'
      
      testlib.foo()
      testlib.bar {
          testlib.foo()
      }
      

      I get the following output:

      [Pipeline] echo
      foo
      [Pipeline] End of Pipeline
      java.lang.NullPointerException: Cannot invoke method foo() on null object
      

      For some reason, the closure being passed to testlib.bar doesn't see testlib anymore. This only happens if the resolution strategy favors the delegate; if I use OWNER_ONLY or OWNER_FIRST it works. It also works if I provide testlib in the delegate, either by setting it in the map or by just setting body.delegate = body.owner, and it works if I avoid the resolution by just referring to owner.testlib.foo in the closure. Furthermore, this only happens with library code; if I just make a test class in the Jenkinsfile it works fine.

      It seems as though if the resolution strategy is to check the delegate, and the delegate doesn't provide that property, it immediately fails without bothering to check the owner next.

            vjuranek vjuranek
            mrozekma Michael Mrozek
            Votes:
            3 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: