Groovy collection.find() returning boolean in Workflow scripts?!

91 views
Skip to first unread message

Kenneth Baltrinic

unread,
Jan 5, 2015, 8:10:15 PM1/5/15
to jenkins...@googlegroups.com
I am trying to do a very simple file parse operation within a Jenkins workflow script and getting some very bizarre results.  This is with the workflow 1.1 set of plugins and Jenkins 1.596.  

To demonstrate the issue I created a new workflow job with the following script:

def str="1:one|2:two|3:three"
def i = "2"
def opts = str.split(/\|/)
def opt = opts.find { o -> o.contains("${i}:") }
echo "len: ${opts.length}"
echo "opt: ${opt}"

The output of which is 

Started by user anonymous
Running: Print Message
len: 3
Running: Print Message
opt: false
Running: End of Workflow
Finished: SUCCESS

However, running effectively the same script in the Jenkins script console (or any other groovy environment I have tried) produces the expected result:

def str="1:one|2:two|3:three"
def i = "2"
def opts = str.split(/\|/)
def opt = opts.find { o -> o.contains("${i}:") }
println "len: ${opts.length}"
println "opt: ${opt}"

returns:

len: 3
opt: 2:two

Am I missing something really obvious or should I open a bug report for this?

Thomas Goeppel

unread,
Jan 6, 2015, 5:19:11 AM1/6/15
to jenkins...@googlegroups.com
Kenneth,

CPS doesn't always behave like a Groovy programmer would expect. Obviously, there is a price to pay for the double nature of CPS as a job description, and scripting language (README.md refers to this as "continuation passing style transformation of the script").

However, List.find seems to return the first result of the closure (in your case 'false'), and this is plain wrong. Yes, I would file a bug. Either this should work, or 'find' shouldn't be part of CPS.

Best,
Thomas

Kenneth Baltrinic

unread,
Jan 6, 2015, 7:10:59 AM1/6/15
to jenkins...@googlegroups.com
Opened JENKINS-26307 for this.
Reply all
Reply to author
Forward
0 new messages