Skip to content
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

Merged
merged 9 commits into from
Feb 27, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.cloudbees.groovy.cps.sandbox.SandboxInvoker;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import groovy.lang.Closure;
import groovy.lang.GroovyClassLoader;
import hudson.ExtensionList;
import java.util.Set;
Expand Down Expand Up @@ -137,6 +138,18 @@ private void maybeRecord(Class<?> clazz, Supplier<String> message) {
Class<?> clazz = classOf(lhs);
maybeRecord(clazz, () -> clazz.getName() + "." + name);
delegate.setProperty(lhs, name, value);
var g = CpsThreadGroup.current();
if (g != null) {
var e = g.getExecution();
if (e != null) {
e.getOwner().getListener().getLogger().println(Messages.LoggingInvoker_field_set(findOwner(lhs).getClass().getName(), name, value.getClass().getName()));
assert false : "TODO look for false positives";
}
}
}

private Object findOwner(Object o) {
return o instanceof Closure<?> c ? findOwner(c.getOwner()) : o;
}

@Override public Object getAttribute(Object lhs, String name) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ SnippetizerLink.OnlineDocsLink.displayName=Online Documentation
SnippetizerLink.ExamplesLink.displayName=Examples Reference
SnippetizerLink.GDSLLink.displayName=IntelliJ IDEA GDSL
Pipeline.Syntax=Pipeline Syntax
LoggingInvoker.field_set=Apparent field set (did you forget `def`?): {0}.{1}={2}
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public static class Execution extends AbstractStepExecutionImpl {
}
r.assertBuildStatusSuccess(r.waitForCompletion(b));
new DepthFirstScanner().allNodes(b.getExecution()).stream().sorted(Comparator.comparing(n -> Integer.valueOf(n.getId()))).forEach(n -> System.out.println(n.getId() + " " + n.getDisplayName()));
r.assertLogContains(Messages.LoggingInvoker_field_set("WorkflowScript", "g", CpsClosure2.class.getName()), b);
});
}

Expand Down
Loading