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

Documentation should clarify distinction between global variable access vs. plain class references

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      Documentation says "Internally, scripts in the vars directory are instantiated as a singleton on-demand, when used first."

      Here is a very basic example to demonstrate it's not the case:

      // vars/acme.groovy
      
      def setFoo(v) {
          this.foo = v;
      }
      def getFoo() {
          return this.foo;
      }
      
      // src/com/foo/Zot.groovy
      
      package com.foo
      
      def printAcmeFoo() {
          try {
              echo acme.foo
          } catch (e){
              echo "acme.foo is undefined"
          }
      }
      

      And here is a pipeline:

      import com.foo.Zot
      
      node{
          acme.foo = "5"
          echo acme.foo;
          
          Zot z = new Zot()    
          z.printAcmeFoo()
      }
      

      Actually it shows 2 issues:
      1/ scripts in the vars directory are not singletons
      2/ when accessing undefined field pipeline will hang indefinitely

        Attachments

          Issue Links

            Activity

            Hide
            mbidewel Mark added a comment -

            I believe this issue may also be the root cause of JENKINS-36673 as well.

            Show
            mbidewel Mark added a comment - I believe this issue may also be the root cause of JENKINS-36673 as well.
            Hide
            jglick Jesse Glick added a comment -

            While there may be some bugs here, the quoted scripts are definitely wrong. acme here is a global variable, meaning it is only accessible unqualified from Script code, such as the main Pipeline; you could pass this to Zot if you wanted to access it. So the quoted Zot is doing something, I am not sure what, but certainly not what you intended.

            Show
            jglick Jesse Glick added a comment - While there may be some bugs here, the quoted scripts are definitely wrong. acme here is a global variable, meaning it is only accessible unqualified from Script code, such as the main Pipeline; you could pass this to Zot if you wanted to access it. So the quoted Zot is doing something, I am not sure what, but certainly not what you intended.
            Hide
            vehovmar Martin Vehovsky added a comment - - edited

            Now that is peculiar, because documentation doesn't say a word about the fact that global variables shouldn't be available in the shared code. So I say they are definitely not wrong.

            In Zot i simply want to access global variable acme. And I am able to access it. It's just not the same instance of the acme as it was in the pipeline.

            Show
            vehovmar Martin Vehovsky added a comment - - edited Now that is peculiar, because documentation doesn't say a word about the fact that global variables shouldn't be available in the shared code . So I say they are definitely not wrong. In Zot i simply want to access global variable acme . And I am able to access it. It's just not the same instance of the acme as it was in the pipeline .
            Hide
            jglick Jesse Glick added a comment -

            Documentation was clarified as part of JENKINS-31155. So again, the quoted examples are just incorrect. There may be a bug using other examples.

            Show
            jglick Jesse Glick added a comment - Documentation was clarified as part of JENKINS-31155 . So again, the quoted examples are just incorrect. There may be a bug using other examples.
            Hide
            segator Isaac Aymerich added a comment -

            I have the same problem but without 100 cpu usage, the job is hang trying to set a a variable in a singleton
            the setting is from inside the same file

            print 'before set singleton'
            devops.setBuild(build)
            print 'after set singleton'

            never "after set singleton" shown

            Show
            segator Isaac Aymerich added a comment - I have the same problem but without 100 cpu usage, the job is hang trying to set a a variable in a singleton the setting is from inside the same file print 'before set singleton' devops.setBuild(build) print 'after set singleton' never "after set singleton" shown
            Hide
            jglick Jesse Glick added a comment -

            Isaac Aymerich whatever your issue is (there are no steps to reproduce supplied) it sounds more like JENKINS-38021, i.e., misbehavior in response to an erroneous script.

            Show
            jglick Jesse Glick added a comment - Isaac Aymerich whatever your issue is (there are no steps to reproduce supplied) it sounds more like JENKINS-38021 , i.e., misbehavior in response to an erroneous script.

              People

              Assignee:
              jglick Jesse Glick
              Reporter:
              vehovmar Martin Vehovsky
              Votes:
              3 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: