Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-42024

Silent error : UnsupportedOperationExceptions are not raised when using each/find

    Details

    • Similar Issues:

      Description

      Currently, putting the following code in a Jenkinsfile will raise a UnsupportedOperationException: Calling public static java.util.List org.codehaus.groovy.runtime.DefaultGroovyMethods.find(java.util.List,groovy.lang.Closure) on a CPS-transformed closure is not yet supported (JENKINS-26481); encapsulate in a @NonCPS method, or use Java-style loops

      node {
          tmp = [ 'A', 'B' ].find { it == 'B' }
          echo tmp
      }
      

      However, if this code is used without @NonCPS in a shared library, it will fail silently and tmp will be false.

        Attachments

          Issue Links

            Activity

            Hide
            jglick Jesse Glick added a comment -

            Such code is illegal, but the automatic detection of it is only straightforward in sandbox mode. (Nothing to do with libraries per se.)

            Show
            jglick Jesse Glick added a comment - Such code is illegal, but the automatic detection of it is only straightforward in sandbox mode. (Nothing to do with libraries per se.)
            Hide
            lucasc Lucas Cimon added a comment -

            OK. Thanks for your answer.

            I have some questions:

            • when and where is the sandbox mode "ON" ? Whenever a Jenkinsfile in executed ? Only when you "Replay" it ?
            • is it possible to detect this outside Jenkins, in a "static code analysis" phase, or maybe when running unit tests ?
            Show
            lucasc Lucas Cimon added a comment - OK. Thanks for your answer. I have some questions: when and where is the sandbox mode "ON" ? Whenever a Jenkinsfile in executed ? Only when you "Replay" it ? is it possible to detect this outside Jenkins, in a "static code analysis" phase, or maybe when running unit tests ?
            Hide
            jglick Jesse Glick added a comment -

            when and where is the sandbox mode "ON" ? Whenever a Jenkinsfile in executed ?

            For standalone Pipeline jobs with inline script, this is a configuration option. For jobs with a script loaded from SCM, currently it is always on (filed separately). For branch projects, it is always on. For folder-scoped libraries, it is on. For global libraries, it is off.

            is it possible to detect this outside Jenkins, in a "static code analysis" phase

            Afraid not.

            Show
            jglick Jesse Glick added a comment - when and where is the sandbox mode "ON" ? Whenever a Jenkinsfile in executed ? For standalone Pipeline jobs with inline script, this is a configuration option. For jobs with a script loaded from SCM, currently it is always on (filed separately). For branch projects, it is always on. For folder-scoped libraries, it is on. For global libraries, it is off. is it possible to detect this outside Jenkins, in a "static code analysis" phase Afraid not.
            Hide
            lucasc Lucas Cimon added a comment -

            Based on this doc: https://github.com/cloudbees/groovy-cps/blob/master/doc/sandbox.md
            it seems it could be possible to write an alternative groovy intepreter
            (maybe taking inspiration from this code: https://github.com/jenkinsci/workflow-cps-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShellFactory.java)
            that would make it possible to exacute Groovy scripts on the CLI in the same manner as in Jenkins pipeline plugin.

            Does this plan sounds feasible to you ?

            Show
            lucasc Lucas Cimon added a comment - Based on this doc: https://github.com/cloudbees/groovy-cps/blob/master/doc/sandbox.md it seems it could be possible to write an alternative groovy intepreter (maybe taking inspiration from this code: https://github.com/jenkinsci/workflow-cps-plugin/blob/master/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShellFactory.java ) that would make it possible to exacute Groovy scripts on the CLI in the same manner as in Jenkins pipeline plugin. Does this plan sounds feasible to you ?
            Hide
            jglick Jesse Glick added a comment -
            Show
            jglick Jesse Glick added a comment - See JENKINS-33925 .
            Hide
            jglick Jesse Glick added a comment -

            As of the fix of JENKINS-26481, each and find should work; some less common methods are not yet supported, but the UnsupportedOperationException listed here is no longer thrown under any circumstances, since it was not maintainable to detect which methods were covered. JENKINS-31314 should rather detect these cases more reliably (and without relying on the sandbox).

            Show
            jglick Jesse Glick added a comment - As of the fix of JENKINS-26481 , each and find should work; some less common methods are not yet supported, but the UnsupportedOperationException listed here is no longer thrown under any circumstances, since it was not maintainable to detect which methods were covered.  JENKINS-31314 should rather detect these cases more reliably (and without relying on the sandbox).
            Hide
            lucasc Lucas Cimon added a comment -

            Thank you

            Show
            lucasc Lucas Cimon added a comment - Thank you

              People

              • Assignee:
                Unassigned
                Reporter:
                lucasc Lucas Cimon
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: