Skip to content

Commit

Permalink
Merge pull request #413 from bitwiseman/task/declaration-output
Browse files Browse the repository at this point in the history
[JENKINS-34987] Improved output for local variable declarations with script splitting
  • Loading branch information
bitwiseman authored Feb 2, 2021
2 parents 3ebd553 + 7328248 commit 2d2dbb7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import org.jenkinsci.plugins.workflow.steps.StepDescriptor
import edu.umd.cs.findbugs.annotations.CheckForNull
import edu.umd.cs.findbugs.annotations.NonNull

import java.util.stream.Collectors

import static org.codehaus.groovy.ast.tools.GeneralUtils.*
import static org.jenkinsci.plugins.pipeline.modeldefinition.parser.ASTParserUtils.*
import static org.objectweb.asm.Opcodes.ACC_PUBLIC
Expand Down Expand Up @@ -1270,11 +1272,28 @@ class RuntimeASTTransformer {
result.addAll(pipelineElementHandles)
pipelineElementHandles.clear()
} else {
def declarationNames = []
declarations.each { item ->
def left = item.getLeftExpression()
if (left instanceof VariableExpression) {
declarationNames.add(((VariableExpression)left).getName())
} else if (left instanceof ArgumentListExpression) {
left.each { arg ->
if (arg instanceof VariableExpression) {
declarationNames.add(((VariableExpression) arg).getName())
} else {
declarationNames.add("Unrecognized expression: " + arg.toString())
}
}
} else {
declarationNames.add("Unrecognized declaration structure: " + left.toString())
}
}
throw new IllegalStateException("[JENKINS-34987] SCRIPT_SPLITTING_TRANSFORMATION is an experimental feature of Declarative Pipeline and is incompatible with local variable declarations inside a Jenkinsfile. " +
"Local variable declarations found: " + declarations.join(", ") + ". " +
"As a temporary workaround, you can add the '@Field' annotation to these local variable declarations. " +
"However, use of Groovy variables in Declarative pipeline, with or without the '@Field' annotation, is not recommended or supported. " +
"To use less effective script splitting which allows local variable declarations without changing your pipeline code, set SCRIPT_SPLITTING_ALLOW_LOCAL_VARIABLES=true .")
"As a temporary workaround, you can add the '@Field' annotation to these local variable declarations. " +
"However, use of Groovy variables in Declarative pipeline, with or without the '@Field' annotation, is not recommended or supported. " +
"To use less effective script splitting which allows local variable declarations without changing your pipeline code, set SCRIPT_SPLITTING_ALLOW_LOCAL_VARIABLES=true . " +
"Local variable declarations found: " + declarationNames.sort().join(", ") + ". ")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void stages100WithOutsideVarAndFuncNotAllowed() throws Exception {
RuntimeASTTransformer.SCRIPT_SPLITTING_ALLOW_LOCAL_VARIABLES = false;
expect(Result.FAILURE,"basic/stages100WithOutsideVarAndFunc")
.logContains("add the '@Field' annotation to these local variable declarations")
.logContains("firstVar, secondVar, someVar")
.logNotContains("Method code too large!")
.go();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

def someVar = "Hi there"
def (firstVar, secondVar) = [1, 2]
def someFunc() {
return "This comes from a function"
}
Expand Down

0 comments on commit 2d2dbb7

Please sign in to comment.