[JIRA] (JENKINS-44924) pipeline groovy script - Sort a list with custom comparator or closure not sorting

122 views
Skip to first unread message

andrew.bayer@gmail.com (JIRA)

unread,
Mar 15, 2018, 11:27:02 AM3/15/18
to jenkinsc...@googlegroups.com
Andrew Bayer assigned an issue to Andrew Bayer
 
Jenkins / Bug JENKINS-44924
pipeline groovy script - Sort a list with custom comparator or closure not sorting
Change By: Andrew Bayer
Assignee: Andrew Bayer
Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.3.0#73011-sha1:3c73d0e)
Atlassian logo

andrew.bayer@gmail.com (JIRA)

unread,
Mar 15, 2018, 11:28:02 AM3/15/18
to jenkinsc...@googlegroups.com
Andrew Bayer started work on Bug JENKINS-44924
 
Change By: Andrew Bayer
Status: Reopened In Progress

jakub@pawlinski.pl (JIRA)

unread,
Mar 26, 2018, 12:40:03 PM3/26/18
to jenkinsc...@googlegroups.com
Jakub Pawlinski commented on Bug JENKINS-44924
 
Re: pipeline groovy script - Sort a list with custom comparator or closure not sorting

Just lost two hours thinking that I'm doing something wrong.

the org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field java.lang.Integer Name is ** anything but helpful.

tried all possible sorts with closure, with comparator, with OrderBy, finally falled back to Script Console to learn that each attempt I made works fine.

Decided to submit a bug, but found its already here.

Would love to have proper "@NonCPS" requirement messages/exceptions or even a list to refer to rather than integer error. 

scm_issue_link@java.net (JIRA)

unread,
Mar 28, 2018, 2:30:02 PM3/28/18
to jenkinsc...@googlegroups.com

Code changed in jenkins
User: Andrew Bayer
Path:
src/main/resources/org/jenkinsci/plugins/scriptsecurity/sandbox/whitelists/generic-whitelist
http://jenkins-ci.org/commit/script-security-plugin/15c8d24fc2e46b705d461b72f0d9908ed7530c68
Log:
JENKINS-44924 Whitelist DefaultGroovyMethods for sort, unique, etc

scm_issue_link@java.net (JIRA)

unread,
Mar 28, 2018, 2:30:03 PM3/28/18
to jenkinsc...@googlegroups.com

Code changed in jenkins
User: Andrew Bayer
Path:
src/main/resources/org/jenkinsci/plugins/scriptsecurity/sandbox/whitelists/generic-whitelist

http://jenkins-ci.org/commit/script-security-plugin/ffbfc0adda6af7c787c0569bc2196d2343c3a259
Log:
Merge pull request #191 from abayer/add-more-dgm-methods

JENKINS-44924 Whitelist DefaultGroovyMethods for sort, unique, etc

scof@rambler.ru (JIRA)

unread,
May 22, 2018, 3:31:03 AM5/22/18
to jenkinsc...@googlegroups.com

Andrew Bayer IS there any plans to close the issue. And wonder why is it considered a "Minor"?? The trivial correct groovy code just doesn't (silently!) work and that's a "small problem"?? Amazing...

scof@rambler.ru (JIRA)

unread,
May 22, 2018, 3:31:05 AM5/22/18
to jenkinsc...@googlegroups.com
Roman Sinyakov edited a comment on Bug JENKINS-44924
[~abayer] IS Is there any plans to close the issue . ? And wonder why is it considered a "Minor"?? The trivial correct groovy code just doesn't (silently!) work and that's a "small problem"?? Amazing...

vivek.pandey@gmail.com (JIRA)

unread,
Nov 15, 2018, 5:33:03 PM11/15/18
to jenkinsc...@googlegroups.com
Vivek Pandey updated an issue
 
Change By: Vivek Pandey
Labels: groovy pipeline triaged-2018-11
This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

issac1993@gmail.com (JIRA)

unread,
Jan 31, 2019, 12:58:03 PM1/31/19
to jenkinsc...@googlegroups.com
Daniel Issac commented on Bug JENKINS-44924
 
Re: pipeline groovy script - Sort a list with custom comparator or closure not sorting

Any updates on this.?I agree with Roman Sinyakov.This seems to be a serious issue which needs to be fixed.

joerg.baeuerle@berthold.com (JIRA)

unread,
Feb 5, 2019, 8:48:03 AM2/5/19
to jenkinsc...@googlegroups.com

Yes, I also agree. This has to be fixed. I've just spent two days of my life trying to figure out what was wrong with my pipeline script. My Groovy code worked perfectly in Jenkin's Script Console, but once put in a Jenkinsfile strange things happened. In my case it was not sort() but toSorted() which has the same problem. The following code snipplet

List<String> foo = ["hello","hi","hey"].toSorted { a, b -> a.length() <=> b.length() }
println foo.toString()

taken straight from the toSorted() specification in the Groovy Docs.

In Jenkin's Script Console the output was

[hi, hey, hello]

which is correct. When run from a Jenkinsfile the build's output, however, was

-1

 This is not acceptable and certainly not a "Minor" issue, because it costs a lot of time and money to hunt down these kinds of bugs!

joerg.baeuerle@berthold.com (JIRA)

unread,
Feb 5, 2019, 8:49:02 AM2/5/19
to jenkinsc...@googlegroups.com
Joerg Baeuerle edited a comment on Bug JENKINS-44924
Yes, I also agree. This has to be fixed. I've just spent two days of my life trying to figure out what was wrong with my pipeline script. My Groovy code worked perfectly in Jenkin's Script Console, but once put in a Jenkinsfile strange things happened. In my case it was not {{sort()}} but {{toSorted()}} which has the same problem. The following code snipplet snippet
{code
:groovy }

List<String> foo = ["hello","hi","hey"].toSorted { a, b -> a.length() <=> b.length() }
println foo.toString()


In Jenkin's Script Console the output was
{code :java }
[hi, hey, hello]
{code}

which is correct. When run from a Jenkinsfile the build's output, however, was
{code :java }
-1
{code}

 This is not acceptable and certainly not a "Minor" issue, because it costs a lot of time and money to hunt down these kinds of bugs!

joerg.baeuerle@berthold.com (JIRA)

unread,
Feb 5, 2019, 8:56:06 AM2/5/19
to jenkinsc...@googlegroups.com
Joerg Baeuerle edited a comment on Bug JENKINS-44924
Yes, I also agree. This has to be fixed. I've just spent two days of my life trying to figure out what was wrong with my pipeline script. My Groovy code worked perfectly in Jenkin's Script Console, but once put in a Jenkinsfile strange things happened. In my case it was not {{sort()}} but {{toSorted()}} which has the same problem. The following code snippet
{code
:java }
List<String> def foo = ["hello","hi","hey"].toSorted { a, b -> a.length() <=> b.length() }

println foo.toString()
{code}
taken straight from the toSorted() specification in the [Groovy Docs|http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#toSorted(java.lang.Iterable,groovy.lang.Closure)].

In Jenkin's Script Console the output was
{code:java}
[hi, hey, hello]
{code}
which is correct. When run from a Jenkinsfile the build's output, however, was
{code:java}
-1
{code}
 This is not acceptable and certainly not a "Minor" issue, because it costs a lot of time and money to hunt down these kinds of bugs!

joerg.baeuerle@berthold.com (JIRA)

unread,
Feb 5, 2019, 8:57:02 AM2/5/19
to jenkinsc...@googlegroups.com
Joerg Baeuerle edited a comment on Bug JENKINS-44924
Yes, I also agree. This has to be fixed. I've just spent two days of my life trying to figure out what was wrong with my pipeline script. My Groovy code worked perfectly in Jenkin's Script Console, but once put in a Jenkinsfile strange things happened. In my case it was not {{sort()}} but {{toSorted()}} which has the same problem , e . The g. with the following code snippet
{code:java}

def foo = ["hello","hi","hey"].toSorted { a, b -> a.length() <=> b.length() }
println foo.toString()
{code}
taken straight from the toSorted() specification in the [Groovy Docs|http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethods.html#toSorted(java.lang.Iterable,groovy.lang.Closure)].

In Jenkin's Script Console the output was
{code:java}
[hi, hey, hello]
{code}
which is correct. When run from a Jenkinsfile the build's output, however, was
{code:java}
-1
{code}
 This is not acceptable and certainly not a "Minor" issue, because it costs a lot of time and money to hunt down these kinds of bugs!

patrick@intrepid.io (JIRA)

unread,
Apr 12, 2019, 3:37:10 PM4/12/19
to jenkinsc...@googlegroups.com

Just want to make sure I'm understanding this correctly. Jenkins has broken custom sorting in the Groovy language, expects people to write their Jenkinsfiles and Pipeline libraries in Groovy, and then marks this as a minor issue? Every on this thread agrees that's unacceptable, yes?

patrick@intrepid.io (JIRA)

unread,
Apr 12, 2019, 3:37:11 PM4/12/19
to jenkinsc...@googlegroups.com
Pat Butkiewicz edited a comment on Bug JENKINS-44924
Just want to make sure I'm understanding this correctly. Jenkins has broken custom sorting in the Groovy language, expects people to write their Jenkinsfiles and Pipeline libraries in Groovy, and then marks this as a minor issue? Every Everyone on this thread agrees that's unacceptable, yes?

andrew.bayer@gmail.com (JIRA)

unread,
Apr 12, 2019, 3:45:09 PM4/12/19
to jenkinsc...@googlegroups.com

If I could have viably fixed this, I would have already - the transformation done to Groovy code to be able to be durable makes a lot of things more or less impossible to get working correctly. We've dealt with as many of them as we could without having to basically write our own implementation of all of Groovy, but there are still edge cases (sorry, but this is a comparative edge case) where we just can't fix them. We're sorry, but this is what it is.

konstanty.karagiorgis@gmail.com (JIRA)

unread,
Jul 16, 2019, 4:56:02 PM7/16/19
to jenkinsc...@googlegroups.com

Could it at least be somehow reflected in https://github.com/jenkinsci/JenkinsPipelineUnit so I could unit test all the hacks I need to do to make my code running on jenkins? What's even the purpose of writing unit tests if I can't guarantee, that the code will be running the same on target environment? It gets even worse, it breaks silently, which wastes people's time and money.

konstanty.karagiorgis@gmail.com (JIRA)

unread,
Jul 16, 2019, 4:59:05 PM7/16/19
to jenkinsc...@googlegroups.com
Konstanty Karagiorgis updated an issue
 
Change By: Konstanty Karagiorgis
Comment:
Could it at least be somehow reflected in [https://github.com/jenkinsci/JenkinsPipelineUnit] so I could unit test all the hacks I need to do to make my code running on jenkins? What's even the purpose of writing unit tests if I can't guarantee, that the code will be running the same on target environment? It gets even worse, it breaks silently, which wastes people's time and money.

neal.macdonald@prismtech.com (JIRA)

unread,
Sep 23, 2019, 6:29:08 AM9/23/19
to jenkinsc...@googlegroups.com
Neal Macdonald commented on Bug JENKINS-44924
 
Re: pipeline groovy script - Sort a list with custom comparator or closure not sorting

I couldn't get a pipeline script to work with map sort() at all until I came across [this|

https://stackoverflow.com/questions/52946282/jenkins-sort-method-behaviour-not-as-expected

]. This might be a workaround for the issue above.

Calling sort from a function with @NonCPS annotation eg:

@NonCPS
def sortExample(items) 
{
    def itemsSorted = items.sort{ it['val'] }
    println(itemsSorted) 
}

node('targetnode')
{
    def m = [ [name: 'abc', val: '123'], [name: 'zwe', val: '934'], [name: 'wxc', val: '789']]
    println m
    sortExample(m)
}

resulted in a full map sort dependent on the value of a specific key:

[Pipeline] echo
[{name=abc, val=123}, {name=zwe, val=934}, {name=wxc, val=789}]
[Pipeline] echo
[{name=abc, val=123}, {name=wxc, val=789}, {name=zwe, val=934}]
This message was sent by Atlassian Jira (v7.13.6#713006-sha1:cc4451f)
Atlassian logo

neal.macdonald@prismtech.com (JIRA)

unread,
Sep 23, 2019, 6:30:03 AM9/23/19
to jenkinsc...@googlegroups.com
Neal Macdonald edited a comment on Bug JENKINS-44924
I couldn't get a pipeline script to work with map sort() at all until I came across [this|

[https://stackoverflow.com/questions/52946282/jenkins-sort-method-behaviour-not-as-expected]


]. This might be a workaround for the issue above.

Calling sort from a function with @NonCPS annotation eg:
{noformat}
@NonCPS
def sortExample(items)
{
    def itemsSorted = items.sort{ it['val'] }
    println(itemsSorted)
}

node('targetnode')
{
    def m = [ [name: 'abc', val: '123'], [name: 'zwe', val: '934'], [name: 'wxc', val: '789']]
    println m
    sortExample(m)
}
{noformat}

resulted in a full map sort dependent on the value of a specific key:
{noformat}

[Pipeline] echo
[{name=abc, val=123}, {name=zwe, val=934}, {name=wxc, val=789}]
[Pipeline] echo
[{name=abc, val=123}, {name=wxc, val=789}, {name=zwe, val=934}]
{noformat}

neal.macdonald@prismtech.com (JIRA)

unread,
Sep 23, 2019, 6:31:03 AM9/23/19
to jenkinsc...@googlegroups.com

lucas.cimon@gmail.com (JIRA)

unread,
Nov 21, 2019, 10:03:04 AM11/21/19
to jenkinsc...@googlegroups.com

lucas.cimon@gmail.com (JIRA)

unread,
Nov 21, 2019, 10:09:04 AM11/21/19
to jenkinsc...@googlegroups.com

lucas.cimon@gmail.com (JIRA)

unread,
Nov 21, 2019, 10:30:08 AM11/21/19
to jenkinsc...@googlegroups.com
Lucas Cimon edited a comment on Bug JENKINS-44924
I found bit of helpful context about this issue : [https://wiki.jenkins.io/display/JENKINS/Pipeline+CPS+method+mismatches#PipelineCPSmethodmismatches-Callingnon-CPS-transformedmethodswithCPS-transformedarguments]

 

Edit: captain obvious... I did not notice the link to this exact issue on the wiki :)


Here is the workaround I used to sort a list of maps base on a String field:
{code:java}
pipeline {
    stages {
        stage("Test") {
            steps {
                script {
                    def propList = [[name: 'B'], [name: 'A']]
                    sortByField(propList, 'name')
                    propList.eachWithIndex { entry, i ->
                     println "${i}: ${entry}"
                    }
                }
            }
        }
    }
}
@NonCPS
static sortByField(list, fieldName) {
    list.sort{ it[fieldName] }
}
{code}

lucas.cimon@gmail.com (JIRA)

unread,
Nov 21, 2019, 10:31:05 AM11/21/19
to jenkinsc...@googlegroups.com
Lucas Cimon edited a comment on Bug JENKINS-44924
I found bit of helpful context about this issue : [https://wiki.jenkins.io/display/JENKINS/Pipeline+CPS+method+mismatches#PipelineCPSmethodmismatches-Callingnon-CPS-transformedmethodswithCPS-transformedarguments]

Edit: captain obvious... I did not notice the link to this exact issue on the wiki :)

Here is the workaround I used to sort a list of maps base Map objects, based on a String field:

{code:java}
pipeline {
    stages {
        stage("Test") {
            steps {
                script {
                    def propList = [[name: 'B'], [name: 'A']]
                    sortByField(propList, 'name')
                    propList.eachWithIndex { entry, i ->
                     println "${i}: ${entry}"
                    }
                }
            }
        }
    }
}
@NonCPS
static sortByField(list, fieldName) {
    list.sort{ it[fieldName] }
}
{code}

ianfixes@gmail.com (JIRA)

unread,
Dec 22, 2019, 10:03:06 PM12/22/19
to jenkinsc...@googlegroups.com
Ian Katz commented on Bug JENKINS-44924

If I could have viably fixed this, I would have already - the transformation done to Groovy code to be able to be durable makes a lot of things more or less impossible to get working correctly. We've dealt with as many of them as we could without having to basically write our own implementation of all of Groovy, but there are still edge cases (sorry, but this is a comparative edge case) where we just can't fix them.

If the sort function can't work under any circumstances, shouldn't the correct behavior be to have it throw some exception rather than just pretend to work?  (Emphasis for meaning, not for tone.)

I lost about a day's work here trying permutations of the following documented descriptions of array sort:

I would have greatly preferred an exception that said "We're sorry, but this is what it is."  Instead I assumed that I wasn't supplying the correct arguments, mutating instead of returning a value, etc.  That was a very unpleasant rabbit hole.

Can you talk more about what makes function impossible to support?  For example, is it related to a threaded library or some other implementation detail that prevents the built-in function from every being translated to CPS?  Is it a lack of support for the "spaceship operator"?  Or is it something inherent to the sort algorithm that would make it fail even if I tried to implement my own array sort function by hand?

jglick@cloudbees.com (JIRA)

unread,
Dec 22, 2019, 11:32:04 PM12/22/19
to jenkinsc...@googlegroups.com

If you are using a reasonably recent version of workflow-cps it should have printed a warning with a link to a wiki page explaining this class of issue and workarounds.

ianfixes@gmail.com (JIRA)

unread,
Dec 23, 2019, 1:57:08 PM12/23/19
to jenkinsc...@googlegroups.com
Ian Katz commented on Bug JENKINS-44924

I'm on 2.190.2 of Jenkins, with BlueOcean 1.21.0. Not sure how to find workflow-cps version.

I didn't see a warning, but my point is that it should produce an error, not a warning. What is the justification for allowing me to call a function that you know in advance will not do its job?

jglick@cloudbees.com (JIRA)

unread,
Dec 24, 2019, 9:22:05 AM12/24/19
to jenkinsc...@googlegroups.com

/pluginManager/installed; text console log in classic view, not Blue Ocean; and because the detector has false positives (it is very complicated).

Reply all
Reply to author
Forward
0 new messages