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

expression does not see imported classes

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      The following Jenkinsfile snippet shows the fully quallified usage of an imported Type within an `expression` block. The whole Jenkinsfile can be executed without a failure.

      ```groovy

      @Library('SomeManagedLibID') _
      import some.package.MyType
      [...]
      stages {
        stage('Foobar') {
          when {
            expression {
              def xyz = new some.package.MyType()
              [...]
            }
      ```

      Whereas the second snippet uses the short name (because it is imported) ...

      ```groovy

      @Library('SomeManagedLibID') _
      import some.package.MyType
      [...]
      stages {
        stage('Foobar') {
          when {
            expression {
              def xyz = new MyType()
              [...]
            }
      ```

      ... but in this case, the execution fails with a ProxyException:

      ```
      Bitbucket] Build result notified
      hudson.remoting.ProxyException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Script2.groovy: 2: unable to resolve class MyType
      @ line 2, column 10.
      def cg = new MyType()
      ^

      1 error

      at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
      at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
      at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
      at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
      at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
      at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
      at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
      at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.parse(CpsGroovyShell.java:55)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:584)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:623)
      at groovy.lang.GroovyShell.evaluate(GroovyShell.java:594)
      at org.jenkinsci.plugins.workflow.cps.CpsScript.evaluate(CpsScript.java:167)
      at org.jenkinsci.plugins.workflow.cps.CpsScript$evaluate.call(Unknown Source)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
      at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:18)
      at org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.ExpressionConditionalScript.evaluate(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ExpressionConditionalScript.groovy:43)
      at org.jenkinsci.plugins.pipeline.modeldefinition.when.impl.AllOfConditionalScript.evaluate(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AllOfConditionalScript.groovy:44)
      at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateWhen(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:420)
      at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:95)
      at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:223)
      at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:222)
      at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(jar:file:/var/jenkins_home/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:94)
      at __cps.transform__(Native Method)
      at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
      at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
      at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
      at sun.reflect.GeneratedMethodAccessor330.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
      at com.cloudbees.groovy.cps.impl.LocalVariableBlock$LocalVariable.get(LocalVariableBlock.java:39)
      at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
      at com.cloudbees.groovy.cps.impl.LocalVariableBlock.evalLValue(LocalVariableBlock.java:28)
      at com.cloudbees.groovy.cps.LValueBlock$BlockImpl.eval(LValueBlock.java:55)
      at com.cloudbees.groovy.cps.LValueBlock.eval(LValueBlock.java:16)
      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:262)
      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:471)
      at java.util.concurrent.FutureTask.run(FutureTask.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
      Finished: FAILURE
      ```
      It looks like the `expression` does allow to execute groovy code but does not see the imported types.

        Attachments

          Issue Links

            Activity

            Hide
            abayer Andrew Bayer added a comment -

            I've got a PR up for this at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/143, but I'm not sure it's the right solution yet. =)

            Show
            abayer Andrew Bayer added a comment - I've got a PR up for this at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/143,  but I'm not sure it's the right solution yet. =)
            Hide
            ghostcity Stefan Gasterstädt added a comment - - edited

            According to your groovy-unit-test libraryObjectInWhenExpression.groovy, this looks exactly similar to my situation.

            Thank you, that was a fast reaction.

            Show
            ghostcity Stefan Gasterstädt added a comment - - edited According to your groovy-unit-test libraryObjectInWhenExpression.groovy , this looks exactly similar to my situation. Thank you, that was a fast reaction.
            Hide
            abayer Andrew Bayer added a comment -

            New, better PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/144! This will require changing your Jenkinsfile to use the libraries directive, but I think it's the right long-term solution anyway. The original PR is now closed.

            Show
            abayer Andrew Bayer added a comment - New, better PR up at https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/144!  This will require changing your Jenkinsfile to use the libraries directive, but I think it's the right long-term solution anyway. The original PR is now closed.
            Hide
            jglick Jesse Glick added a comment -

            Maybe you should just get rid of scripted expressions. People who need to do fancy things like that can just use scripted Pipeline.

            Show
            jglick Jesse Glick added a comment - Maybe you should just get rid of scripted expressions. People who need to do fancy things like that can just use scripted Pipeline.
            Hide
            ghostcity Stefan Gasterstädt added a comment - - edited

            As far as I understand, scripting is a valid option for declarative Jenkinsfiles. That is what

            steps {
              script { ... }
            }
            

            is made for. Similar, the expression block of when is made for script blocks.

            Don't you think, that if scripting a when-condition is not a suitable option, a declarative Jenkinsfile should not offer

            when {
              expression { ... }
            }
            

            at all?

            Show
            ghostcity Stefan Gasterstädt added a comment - - edited As far as I understand, scripting is a valid option for declarative Jenkinsfiles. That is what steps { script { ... } } is made for. Similar, the expression block of when is made for script blocks. Don't you think, that if scripting a when-condition is not a suitable option, a declarative Jenkinsfile should not offer when { expression { ... } } at all?
            Hide
            abayer Andrew Bayer added a comment -

            This specific case is fixed as part of JENKINS-42753. I've added a test to the relevant PR, and it'll be included in the Declarative 1.2 release. I still don't have an answer for imports when using the libraries directive, but that's a giant pain anyway. =)

            Show
            abayer Andrew Bayer added a comment - This specific case is fixed as part of JENKINS-42753 . I've added a test to the relevant PR, and it'll be included in the Declarative 1.2 release. I still don't have an answer for imports when using the libraries directive, but that's a giant pain anyway. =)

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                ghostcity Stefan Gasterstädt
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: