title: Why @NonCPS is necessary when iterating through the list?
body: <p>I've the following Groovy code:</p>
  
  <pre><code>// @NonCPS
  def printList(params) {
      def jobs = [:]
      println params
      params.split(",").each { param -&gt;
          println "Param: ${param}"
      }
  }
  text = "Foo,Bar,Baz,Qux"
  printList(text)
  </code></pre>
  
  <p>which works as expected when invoking from <code>groovy</code> command:</p>
  
  <pre><code>$ groovy test.groovy 
  Foo,Bar,Baz,Qux
  Param: Foo
  Param: Bar
  Param: Baz
  Param: Qux
  </code></pre>
  
  <p>Although when testing within a non-sandboxed job in Jenkins, I've different results:</p>
  
  <pre><code>[Pipeline] echo
  Foo,Bar,Baz,Qux
  [Pipeline] echo
  Param: Foo
  [Pipeline] End of Pipeline
  </code></pre>
  
  <p>The problem is that only the first item is printed, instead of all.</p>
  
  <p>However the code works as expected in Jenkins only when using the <code>@NonCPS</code> helper method.</p>
  
  <p>Why <code>@NonCPS</code> is necessary in order to iterate through the list of items properly? And is there any workaround without using <code>@NonCPS</code> (especially in sandboxed environment)?</p>