-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn user when forgetting def
on a variable
#994
Conversation
plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/LoggingInvoker.java
Outdated
Show resolved
Hide resolved
…sing from some tests which could equally well have used lexical variables
@@ -137,6 +138,23 @@ private void maybeRecord(Class<?> clazz, Supplier<String> message) { | |||
Class<?> clazz = classOf(lhs); | |||
maybeRecord(clazz, () -> clazz.getName() + "." + name); | |||
delegate.setProperty(lhs, name, value); | |||
if (value != null) { | |||
var owner = findOwner(lhs); | |||
if (owner instanceof CpsScript) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to work as desired also in libraries: assigning to an implicit field of a vars/*.groovy
produces a warning, while assigning to a field of a named class in src/**/*.groovy
does not.
# TODO perhaps create a https://jenkins.io/redirect/pipeline-field-set/ | ||
LoggingInvoker.field_set=\ | ||
Did you forget the `def` keyword? \ | ||
{0} seems to be setting a field named {1} (to a value of type {2}) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it's worth, "WorkflowScript seems to be setting a field..." sounds strange to me. I don't really have any better recommendations though. Maybe "Something is setting WorkflowScript.xyz (to ...)", but then I would expect users to ask "what something?" and there is no easy way for us to answer that question here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least in the cases I know about, it is code in the named script which is setting the field. I could try to make the SourceLocation
available here (which would give you a line number) though it would be a bit more work since that is only available in groovy-cps
whereas the TaskListener
is only available in the plugin.
As to the naming WorkflowScript
, rather than say Jenkinsfile
, it would be nice to update that, though I would worry about the implications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, my concern is mainly that users may not realize that WorkflowScript
means Jenkinsfile. I agree that we should always mention the real class name, but maybe it could make sense to add (your Jenkinsfile)
or something if the class name is WorkflowScript
, although that is not necessarily accurate depending on how the Pipeline is configured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most simple declarative pipeline. Removed all global libraries (but I found zero reference of resolveStrategy or delgate anywhere in the code)
pipeline {
agent none
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
Looks I'm not the only one https://issues.jenkins.io/browse/JENKINS-75353
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, did not think to check Declarative syntax, will try to fix today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved.
Oh, and it might be worth adding a system property to disable this. Previously, when security fixes broke |
(Or I guess you could also check if the property matches a field defined on the script if |
@jglick I couldn't get rid of the warnings in some specific cases. I've boiled it down to: pipeline {
agent none
stages {
stage('FOO') {
matrix {
axes {
axis {
name 'OS'
values 'Windows', 'Linux'
}
}
stages {
stage('BAR') {
agent any
steps {
script {
echo "${OS}"
}
}
}
}
}
}
}
} Leading to:
That's with 4043.va_fb_de6a_a_8b_f5 |
@PayBas From a quick look, I think that anyone using the |
@dwnusbaum it's not a big deal. We can ignore it. But some of our developers were wondering if they missed something somewhere, which it turns out they didn't. Keep up the good work! |
Following up #925: not only avoiding the symptom, but warning about the mistake. (Though not from
@NonCPS
methods.)Note that this uses a distinct logging system from either #211 or #280, since in this case at the point at which the warning is printed we have easy access to a
TaskListener
(thoughPropertyAccessBlock
would have additional access to aSourceLocation
which may be helpful).