Fix for shared lib and params variables being null in environment section #529
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello, this is a possible fix for #387.
Analysis of the issue
According to Groovy's metaprogramming rules (https://docs.groovy-lang.org/latest/html/documentation/core-metaprogramming.html) Groovy calls
getProperty
method onClosure
which in turn tries to get this property from the delegate (in our case this is aMap
) and when the property is not found it tries to find the property on the owner object.Here's code snippet from Groovy's source code:
https://github.com/apache/groovy/blob/02d1a3b4f76f1311eb2286188b224cfbbf0324db/src/main/java/groovy/lang/Closure.java#L322
In our case delegate is a
Map
and property retrieval for aMap
is implemented as a call toget
method on thatMap
as shown on following code snippet from Groovy implementation:https://github.com/apache/groovy/blob/master/src/main/java/groovy/lang/MetaClassImpl.java#L2001
The problem here is that
Map
returnsnull
when the property is not found however Closure expectsMissingPropertyException
orMissingFieldException
exceptions to try to get the property from the owner.Proposed fix
Create a wrapper that first checks if the property is defined in a map and if not then it tries to get property from the owner which in this case is an instance of GenericPipelineDeclaration.
PS. I'm not a Groovy developer so possibly a better solution could be found.