.collect, then .each still results in closure being invoked only for first element

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      Running this Jenkinsfile

      def lines2 = [['Quick', 'Brown']]
      echo "Literal toString():" + lines2
      def result2 = ''
      lines2.each{ result2 += it }
      echo "Literal each: " + result2
      
      @NonCPS def test(){
          def lines = ['Quick Brown'].collect{ it.split('\\s+') }
          echo "NonCps toString()" + lines
          def result = ''
          lines.each{ result += it }
          echo "NonCps each: " + result
      }
      test()
      
      def lines = ['Quick Brown'].collect{ it.split('\\s+') }
      echo "Collect toString():" + lines
      def result = ''
      lines.each{ result += it }
      echo "Collect each: " + result

      Results in

      [Pipeline] echo
      Literal toString():[[Quick, Brown]]
      [Pipeline] echo
      Literal each: [Quick, Brown]
      [Pipeline] echo
      NonCps toString()[[Quick, Brown]]
      [Pipeline] echo
      NonCps each: [Quick, Brown]
      [Pipeline] echo
      Collect toString():[[Quick, Brown]]
      [Pipeline] echo
      Collect each: Quick
      [Pipeline] End of Pipeline
      Finished: SUCCESS
      

      As you can see calling .each on a literal list works correctly, but calling it on a result of .collect still fails like JENKINS-26481

            Assignee:
            Unassigned
            Reporter:
            Jakub Bochenski
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Archived: