Skip to content

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

@jenkins-infra-bot

Description

@jenkins-infra-bot

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) {

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

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.


Originally reported by awitt, imported from: @Library annotation does not work in load()'d scripts
  • status: Open
  • priority: Minor
  • component(s): workflow-cps-global-lib-plugin
  • resolution: Unresolved
  • votes: 1
  • watchers: 4
  • imported: 20251212-090250
Raw content of original issue

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) {

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

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions