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

@Library annotation does not work in load()'d scripts

      Problem Statement

      On a Jenkins master with global definition of a Shared Library named shared-library, And the following files in a repository:

      Jenkinsfile

      node {
      	stage( "setup" ) {
      		checkout scm
      	}
      	stage( "pipeline" ) {
      		load( "Pipeline.groovy" ).run_pipeline()
      	}
      }
      

      Pipeline.groovy

      @Library( 'shared-library@master' ) _
      
      def run_pipeline() {
      	someMethodFromSharedLibrary()
      }
      
      return this
      

      Where someMethodFromSharedLibrary() is a Global Variable in the vars/someMethodFromSharedLibrary.groovy file in the shared library,

      The pipeline errors on the load(...) step of the Jenkinsfile with a "no definition of libraries found" error:

      ERROR: Could not find any definition of libraries [shared-library@master]
      

      Analysis

      This is because of the algorithm in LibraryAdder here:

      LibrariesAction action = build.getAction(LibrariesAction.class);
      if (action != null) {
      

      https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/f371b987a3e79928962043f18a5d95aef7397b76/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryAdder.java#L88-L89

      After the main Jenkinsfile script is processed, a LibrariesAction is added to the build with no library definitions. When the new script Pipeline.groovy is processed during load(..), it sees the action from the previous script and uses the empty set of library definitions to match against actual library requests that are correctly detected in the @Library annotation in the load()'d script.

      This causes the "no definition for libraries" error message. The library definitions in the load()'d script are never considered.

          [JENKINS-60245] @Library annotation does not work in load()'d scripts

          Austin created issue -
          Austin made changes -
          Description Original: h1. Problem Statement

          On a Jenkins master with global definition of a Shared Library named {{shared-library}}, And the following files in a repository:

          {{Jenkinsfile}}
          {code}
          node {
          stage( "setup" ) {
          checkout scm
          }
          stage( "pipeline" ) {
          load( "Pipeline.groovy" ).run_pipeline()
          }
          }
          {code}
          {{Pipeline.groovy}}
          {code}
          @Library( 'shared-library@master' )

          def run_pipeline() {
          someMethodFromSharedLibrary()
          }

          return this
          {code}
          Where {{someMethodFromSharedLibrary()}} is a Global Variable in the {{vars/someMethodFromSharedLibrary.groovy}} file in the shared library,

          The pipeline errors on the {{load(...)}} step of the {{Jenkinsfile}} with a "no definition of libraries found" error:
          {code:java}
          ERROR: Could not find any definition of libraries [shared-library@master]
          {code}
          h1. Analysis

          This is because of the algorithm in {{LibraryAdder}} here:
          {quote}
          {code:java}
          LibrariesAction action = build.getAction(LibrariesAction.class);
          if (action != null) {
          {code}
          – [https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/f371b987a3e79928962043f18a5d95aef7397b76/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryAdder.java#L88-L89]
          {quote}
          After the main {{Jenkinsfile}} script is processed, a {{LibrariesAction}} is added to the build with no library definitions. When the new script {{Pipeline.groovy}} is processed during {{load(..)}}, it sees the action from the previous script and uses the empty set of library definitions to match against actual library requests that are correctly detected in the {{@Library}} annotation in the {{load()}}'d script.

          This causes the "no definition for libraries" error message. The library definitions in the {{load()}}'d script are never considered.
          New: h1. Problem Statement

          On a Jenkins master with global definition of a Shared Library named {{shared-library}}, And the following files in a repository:

          {{Jenkinsfile}}
          {code:java}
          node {
          stage( "setup" ) {
          checkout scm
          }
          stage( "pipeline" ) {
          load( "Pipeline.groovy" ).run_pipeline()
          }
          }
          {code}
          {{Pipeline.groovy}}
          {code:java}
          @Library( 'shared-library@master' ) _

          def run_pipeline() {
          someMethodFromSharedLibrary()
          }

          return this
          {code}
          Where {{someMethodFromSharedLibrary()}} is a Global Variable in the {{vars/someMethodFromSharedLibrary.groovy}} file in the shared library,

          The pipeline errors on the {{load(...)}} step of the {{Jenkinsfile}} with a "no definition of libraries found" error:
          {code:java}
          ERROR: Could not find any definition of libraries [shared-library@master]
          {code}
          h1. Analysis

          This is because of the algorithm in {{LibraryAdder}} here:
          {quote}
          {code:java}
          LibrariesAction action = build.getAction(LibrariesAction.class);
          if (action != null) {
          {code}
          – [https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/f371b987a3e79928962043f18a5d95aef7397b76/src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryAdder.java#L88-L89]
          {quote}
          After the main {{Jenkinsfile}} script is processed, a {{LibrariesAction}} is added to the build with no library definitions. When the new script {{Pipeline.groovy}} is processed during {{load(..)}}, it sees the action from the previous script and uses the empty set of library definitions to match against actual library requests that are correctly detected in the {{@Library}} annotation in the {{load()}}'d script.

          This causes the "no definition for libraries" error message. The library definitions in the {{load()}}'d script are never considered.

          Austin added a comment -

          Austin added a comment - Potential solution submitted in  https://github.com/jenkinsci/workflow-cps-global-lib-plugin/pull/87

          Can anyone merge it or at least close it not to feed false hopes?

          Martin Cimbalek added a comment - Can anyone merge it or at least close it not to feed false hopes?
          Martin Cimbalek made changes -
          Comment [ Can anyone merge it or at least close it not to feed false hopes? ]
          Martin Cimbalek made changes -
          Assignee New: Baptiste Mathus [ batmat ]

          Baptiste Mathus added a comment - - edited

          This PR has conflicts, and unaddressed feedback AFAICT. I'm not a developer on this plugin (despite I got assigned to it)

          Baptiste Mathus added a comment - - edited This PR has conflicts, and unaddressed feedback AFAICT. I'm not a developer on this plugin (despite I got assigned to it)
          Baptiste Mathus made changes -
          Assignee Original: Baptiste Mathus [ batmat ]

          awitt can you please addrss feedback?

          batmat can you please assign anyone who can do something about this, not just unassign yourself? Thanks!

          Martin Cimbalek added a comment - awitt  can you please addrss feedback? batmat  can you please assign anyone who can do something about this, not just unassign yourself? Thanks!

          cimbi I'm sorry, but the issue is that you assigned me to this, not that I unassigned myself. I don't know who to assign this to.

          Baptiste Mathus added a comment - cimbi I'm sorry, but the issue is that you assigned me to this, not that I unassigned myself. I don't know who to assign this to.

            Unassigned Unassigned
            awitt Austin
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: