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

groovy.lang.MissingFieldException: No such field: binding for class: WorkflowScript

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Trivial Trivial
    • workflow-cps-plugin
    • None

      I tried to print all pipeline variables in a loop and I got an exception from groovy.

      // put this inside stage block:
      
      for (k in this.binding.variables) {
        try {
          v = this.binding.variables[k]
          println "$k = $v";
          }
        catch(Exception ex) {
          println("Catching the exception: ${ex}");
          }	
      }
      
      groovy.lang.MissingFieldException: No such field: binding for class: WorkflowScript
      	at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:2823)
      	at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:3759)
      	at org.codehaus.groovy.runtime.InvokerHelper.getAttribute(InvokerHelper.java:145)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getField(ScriptBytecodeAdapter.java:306)
      	at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:325)
      	at org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetAttribute(GroovyInterceptor.java:80)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetAttribute(SandboxInterceptor.java:380)
      	at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:322)
      	at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetAttribute(Checker.java:319)
      	at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getAttribute(SandboxInvoker.java:36)
      	at com.cloudbees.groovy.cps.impl.AttributeAccessBlock.rawGet(AttributeAccessBlock.java:20)
      	at WorkflowScript.run(WorkflowScript:41)
      	at ___cps.transform___(Native Method)
      	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
      	at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
      	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
      	at sun.reflect.GeneratedMethodAccessor198.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
      	at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
      	at com.cloudbees.groovy.cps.Next.step(Next.java:74)
      	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
      	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:165)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:328)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:240)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:228)
      	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
      	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:745)

          [JENKINS-42779] groovy.lang.MissingFieldException: No such field: binding for class: WorkflowScript

          Andrew Bayer added a comment -

          I'll defer to kohsuke or jglick, but I don't think binding is supposed to be available at the user level here.

          Andrew Bayer added a comment - I'll defer to kohsuke or jglick , but I don't think binding is supposed to be available at the user level here.

          Jesse Glick added a comment -

          Normally Script.binding is accessible, and there are (or were) in fact some tests using it, so I am not sure why it would not be working. Probably the use of the stage block. this may refer to a Closure rather than a Script.

          Jesse Glick added a comment - Normally Script.binding is accessible, and there are (or were) in fact some tests using it, so I am not sure why it would not be working. Probably the use of the stage block. this may refer to a Closure rather than a Script .

          Michael Rose added a comment - - edited

          I have also run into this issue after updating to version 2.46.1 from version 1.615. The function is in a custom groovy workflow "library" get loaded. The script returns self.

          Michael Rose added a comment - - edited I have also run into this issue after updating to version 2.46.1 from version 1.615. The function is in a custom groovy workflow "library" get loaded. The script returns self.

          Michael Rose added a comment -

          Is there a workaround for this? My parameterized workflows all depended on this functionality.

          Michael Rose added a comment - Is there a workaround for this? My parameterized workflows all depended on this functionality.

          Jesse Glick added a comment -

          Without knowing the root cause (which would probably be 90% of fixing the bug), I have no idea what the direct workaround would be, but I would say that in most cases you should not really need to access binding to begin with. If you were just doing this to enumerate build parameters, use params instead.

          Jesse Glick added a comment - Without knowing the root cause (which would probably be 90% of fixing the bug), I have no idea what the direct workaround would be, but I would say that in most cases you should not really need to access binding to begin with. If you were just doing this to enumerate build parameters, use params instead.

          Michael Rose added a comment -

          That's exactly what I was using it for. You're right I don't need that property. Thanks!

          Michael Rose added a comment - That's exactly what I was using it for. You're right I don't need that property. Thanks!

          Sorin Sbarnea added a comment -

          For example I tried to use this approach in order to find where is the information about collected artefacts stored info. Sadly, my attempt failed due to this.

          Details at https://stackoverflow.com/questions/47573598/how-to-access-collected-artifacts-from-jenkins-pipeline-groovy-script

          Displaying which objects are available is a good start for discovering the undocumented things, like list of artifacts collected.

          Sorin Sbarnea added a comment - For example I tried to use this approach in order to find where is the information about collected artefacts stored info. Sadly, my attempt failed due to this. Details at https://stackoverflow.com/questions/47573598/how-to-access-collected-artifacts-from-jenkins-pipeline-groovy-script Displaying which objects are available is a good start for discovering the undocumented things, like list of artifacts collected.

          Andrew Bayer added a comment -

          So fwiw, I reproduced this with the original without being in a stage. And it works if you go with this.getBinding() instead of this.binding. Why that is...I'm still not exactly sure. So I'm going to leave this open to remind me to dig into that at some point, but it's not exactly a priority.

          Andrew Bayer added a comment - So fwiw, I reproduced this with the original without being in a stage . And it works if you go with this.getBinding() instead of this.binding . Why that is...I'm still not exactly sure. So I'm going to leave this open to remind me to dig into that at some point, but it's not exactly a priority.

          I have noticed that getBinding() only returns the variable steps that point to the DSL. Looking at https://github.com/jenkinsci/workflow-cps-plugin/blob/workflow-cps-2.67/src/main/java/org/jenkinsci/plugins/workflow/cps/DSL.java#L131-L200, you could get all sort of things from that object (would need approve a lot of signatures through Script security though...):

          println "\nfunctions\n***********"
          println this.steps.functions.collect { k,v -> v.getFunctionName()}
          
          println "\nstepClassNames\n***********"
          println this.steps.stepClassNames.collect { k,v -> v.getFunctionName()}
          
          println "\nunreportedAmbiguousFunctions\n***********"
          println this.steps.unreportedAmbiguousFunctions.collect { k,v -> v.getFunctionName() }
          
          println "\nallGlobalVariables\n***********"
          println org.jenkinsci.plugins.workflow.cps.GlobalVariable.forRun(currentBuild.rawBuild).collect { var -> var.getName() }
          
          println "\nallSymbols\n***********"
          println getAllSymbols().collectMany { symbol -> org.jenkinsci.plugins.structs.SymbolLookup.getSymbolValue(symbol) }
          
          @NonCPS
          def getAllSymbols() {
              return org.jvnet.hudson.annotation_indexer.Index.list(
                      org.jenkinsci.Symbol.class,
                      jenkins.model.Jenkins.get().pluginManager.uberClassLoader,
                      Class.class)
          }
          

          Allan BURDAJEWICZ added a comment - I have noticed that getBinding() only returns the variable steps that point to the DSL. Looking at https://github.com/jenkinsci/workflow-cps-plugin/blob/workflow-cps-2.67/src/main/java/org/jenkinsci/plugins/workflow/cps/DSL.java#L131-L200 , you could get all sort of things from that object (would need approve a lot of signatures through Script security though...): println "\nfunctions\n***********" println this .steps.functions.collect { k,v -> v.getFunctionName()} println "\nstepClassNames\n***********" println this .steps.stepClassNames.collect { k,v -> v.getFunctionName()} println "\nunreportedAmbiguousFunctions\n***********" println this .steps.unreportedAmbiguousFunctions.collect { k,v -> v.getFunctionName() } println "\nallGlobalVariables\n***********" println org.jenkinsci.plugins.workflow.cps.GlobalVariable.forRun(currentBuild.rawBuild).collect { var -> var .getName() } println "\nallSymbols\n***********" println getAllSymbols().collectMany { symbol -> org.jenkinsci.plugins.structs.SymbolLookup.getSymbolValue(symbol) } @NonCPS def getAllSymbols() { return org.jvnet.hudson.annotation_indexer.Index.list( org.jenkinsci.Symbol.class, jenkins.model.Jenkins.get().pluginManager.uberClassLoader, Class .class) }

            Unassigned Unassigned
            ssbarnea Sorin Sbarnea
            Votes:
            3 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: