Why do some Groovy closures behave unexpectedly in pipelines, for example `list.each { }` or `sort`?
AdvancedConceptScripted pipeline, Groovy & CPS
Answer
CPS has its own implementations of many collection methods, and historically each, collect, sort with closures, and similar methods behaved differently (only the first element processed, or expected to call ... but wound up catching ... warnings). Modern workflow-cps versions support most of them, but sort with a comparator closure and some Comparator-based APIs still misbehave in CPS. Plain for (x in list) loops are always safe; put complex collection manipulation in @NonCPS helpers.
More on Scripted pipeline, Groovy & CPS
What does a scripted pipeline look like and when would you still write one?IntermediateWhat is CPS and why does Jenkins transform pipeline Groovy?AdvancedWhy does `java.io.NotSerializableException` happen in pipelines?AdvancedWhat does `@NonCPS` do and what are its rules?AdvancedWhat is the `script` block in declarative, and what are its limits?BeginnerHow do you share a variable between stages in declarative?Intermediate