-
Bug
-
Resolution: Unresolved
-
Minor
-
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:
def foo() {
echo 'foo'
}
def bar(body) {
body.delegate = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body()
}
And a Pipeline script as follows:
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.