title: Jenkins Workflow: Unable to implement trait in Shared Library
body: <h3>Setup</h3>
  <p>Let's assume I have the following configuration on my Jenkins Shared Library:</p>
  <pre><code>vars/
     utils.groovy
  src/
     com.my.corp.utils/
        tools/
           Utilities.groovy
        helpers/
           Workflow.groovy
  </code></pre>
  <h4>Workflow.groovy</h4>
  <pre><code>package com.my.corp.utils.helpers
  // import org.jenkinsci.plugins.workflow.cps.CpsScript
  
  trait Workflow
  {
      def workflow
  }
  </code></pre>
  <h4>Utitities.groovy</h4>
  <pre><code>package com.my.corp.utils.tools
  import com.my.corp.utils.helpers.Workflow
  // import org.jenkinsci.plugins.workflow.cps.CpsScript
  
  class Utilities implements Serializable, Workflow
  {
      def x = 5
      def y = 10
      
      def echo(Map args)
      {
          this.workflow.sh &quot;&quot;&quot;echo 'args: ${args}'&quot;&quot;&quot;
          this.workflow.sh &quot;&quot;&quot;echo 'args.inspect(): ${args.inspect()}'&quot;&quot;&quot;
          this.workflow.sh &quot;&quot;&quot;echo 'this.steps: ${this.workflow}'&quot;&quot;&quot;
          
          this.workflow.echo message: args.inspect()
          this.workflow.sh &quot;&quot;&quot;echo &quot;${this.workflow.class.getName()}; args: ${args}\&quot;&quot;&quot;&quot;
      }
  }
  </code></pre>
  <h4>utils.groovy</h4>
  <pre><code>import com.my.corp.utils.tools.Utilities
  
  def call(Map params)
  {
      this.echo &quot;${this}&quot;
      params.put('workflow', this)
      new Utilities(params).echo(params)
  }
  </code></pre>
  <h3>The Problem</h3>
  <p>When, in the main pipeline, I call <code>utils x: 1, y: 2</code>,
  the following error appears:</p>
  <pre><code>org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed
  </code></pre>
  <h4>Full Trace</h4>
  <p><em>Long Java stacktrace warning!</em></p>
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>14:56:21  org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
  14:56:21  file:/var/lib/jenkins/jobs/Sandbox-Pipeline/builds/74/libs/245eaa38d10b90e40a75ee9676c50aa097d56330cf312d54476ed05f2c731cfa/src/com/my/corp/utils/tools/Utilities.groovy: -1: Unsupported array expression for CPS transformation in this context @ line -1, column -1.
  14:56:21  file:/var/lib/jenkins/jobs/Sandbox-Pipeline/builds/74/libs/245eaa38d10b90e40a75ee9676c50aa097d56330cf312d54476ed05f2c731cfa/src/com/my/corp/utils/tools/Utilities.groovy: -1: Unsupported array expression for CPS transformation in this context @ line -1, column -1.
  14:56:21  2 errors
  14:56:21  
  14:56:21    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309)
  14:56:21    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1107)
  14:56:21    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:624)
  14:56:21    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:602)
  14:56:21    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:579)
  14:56:21    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)
  14:56:21    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)
  14:56:21    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:279)
  14:56:21    at groovy.lang.GroovyClassLoader.recompile(GroovyClassLoader.java:786)
  14:56:21    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:743)
  14:56:21    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:812)
  14:56:21    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell$TimingLoader.loadClass(CpsGroovyShell.java:170)
  14:56:21    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
  14:56:21    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:702)
  14:56:21    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:812)
  14:56:21    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:800)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariable.getValue(UserDefinedGlobalVariable.java:52)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:115)
  14:56:21    at jdk.internal.reflect.GeneratedMethodAccessor7858.invoke(Unknown Source)
  14:56:21    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  14:56:21    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  14:56:21    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
  14:56:21    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
  14:56:21    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
  14:56:21    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
  14:56:21    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:41)
  14:56:21    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
  14:56:21    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
  14:56:21    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163)
  14:56:21    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
  14:56:21    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:158)
  14:56:21    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
  14:56:21    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
  14:56:21    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
  14:56:21    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
  14:56:21    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
  14:56:21    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
  14:56:21    at WorkflowScript.run(WorkflowScript:18)
  14:56:21    at ___cps.transform___(Native Method)
  14:56:21    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
  14:56:21    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
  14:56:21    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
  14:56:21    at jdk.internal.reflect.GeneratedMethodAccessor741.invoke(Unknown Source)
  14:56:21    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  14:56:21    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  14:56:21    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
  14:56:21    at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
  14:56:21    at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
  14:56:21    at jdk.internal.reflect.GeneratedMethodAccessor744.invoke(Unknown Source)
  14:56:21    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  14:56:21    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  14:56:21    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
  14:56:21    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
  14:56:21    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
  14:56:21    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
  14:56:21    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
  14:56:21    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
  14:56:21    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
  14:56:21    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:187)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:420)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:95)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)
  14:56:21    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
  14:56:21    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  14:56:21    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
  14:56:21    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
  14:56:21    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
  14:56:21    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
  14:56:21    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  14:56:21    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  14:56:21    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  14:56:21    at java.base/java.lang.Thread.run(Thread.java:829)</code></pre>
  </div>
  </div>
  </p>
  <h3>However</h3>
  <p>However, if I change the <code>Workflow</code> to be an <code>abstract class</code> instead of <code>trait</code>, and use <code>extends Workflow implements Serializible</code> instead of <code>implements Workflow, Serializible</code> in the <code>Utilities</code> definition, everything works fine!</p>
  <p>Furthermore, when I run the code above (with traits) in <a href="https://www.tutorialspoint.com/execute_groovy_online.php" rel="nofollow noreferrer">Groovy online executor</a>, it works well too.</p>