{"record":{"id":"184822835a638567","repo":"theonedev/onedev","slug":"error-evaluating-groovy-script-184822","errorCode":null,"errorMessage":"Error evaluating groovy script:\n\n","messagePattern":"Error evaluating groovy script:\n\n","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/util/GroovyUtils.java","lineNumber":117,"sourceCode":"    }\n    \n    public static Object evalScript(String scriptContent, Map<String, Object> variables) {\n    \ttry {\n\t    \tClass<?> scriptClass = compile(scriptContent);\n\t\t\tScript script;\n\t\t\ttry {\n\t\t\t\tObject instance = scriptClass.getDeclaredConstructor().newInstance();\n\t\t\t\tif (!(instance instanceof Script))\n\t\t\t\t\treturn scriptClass;\n\t\t\t\telse \n\t\t\t\t\tscript = (Script) instance;\t\t\t\t\t\n\t\t\t} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {\n\t\t\t\tthrow new RuntimeException(e);\n\t\t\t}\n\t\t\tscript.setBinding(getBinding(variables));\n\t\t\treturn script.run();\n\t\t} catch (RuntimeException e) {\n\t\t\tthrow new RuntimeException(\"Error evaluating groovy script:\\n\\n\" + scriptContent, e);\n\t\t}\n    }\n    \n    public static Object evalScript(String scriptContent) {\n    \treturn evalScript(scriptContent, new HashMap<>());\n    }\n    \n\tpublic static String evalTemplate(String template, Map<String, Object> bindings) {\n\t\t// Make a copy of the bindings as the template engine will modify the bindings\n\t\tvar bindingsCopy = new HashMap<>(bindings);\n\t\ttry {\n\t\t\treturn new SimpleTemplateEngine().createTemplate(template).make(bindingsCopy).toString();\n\t\t} catch (CompilationFailedException | ClassNotFoundException | IOException e) {\n\t\t\tthrow new RuntimeException(e);\n\t\t}\n\t}\t\n}","sourceCodeStart":99,"sourceCodeEnd":134,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/util/GroovyUtils.java#L99-L134","documentation":"GroovyUtils.evalScript compiles the given script content, instantiates it, sets a binding from the variables map, and runs it. Any RuntimeException during compile or run is rethrown as \"Error evaluating groovy script:\\n\\n<scriptContent>\" with the original as cause, so failures point at the inline script text itself.","triggerScenarios":"Calling evalScript(content) or evalScript(content, variables) where the inline Groovy fails to compile (syntax) or fails at runtime (missing binding variable, NPE, thrown exception, unknown class/method).","commonSituations":"Inline scripts in job commands/conditions with typos, scripts expecting variables never put into the map, scripts using imports unavailable on the server, or Groovy version incompatibilities with script syntax.","solutions":["Inspect the cause chain — the nested exception pinpoints compile vs runtime failure and the script line.","Validate the script syntax in a standalone Groovy console before embedding it.","Ensure every variable the script reads is present in the variables map passed to evalScript.","Add explicit imports for classes used and confirm they exist on the server classpath."],"exampleFix":"// before: missing variable in binding\nObject r = GroovyUtils.evalScript(\"return value * 2\"); // 'value' undefined -> RuntimeException\n// after\nMap<String, Object> vars = new HashMap<>();\nvars.put(\"value\", 21);\nObject r = GroovyUtils.evalScript(\"return value * 2\", vars);","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    return GroovyUtils.evalScript(content, variables);\n} catch (RuntimeException e) {\n    Throwable cause = ExceptionUtils.getRootCause(e);\n    log.error(\"Inline script failed: {}\", cause.getMessage(), cause);\n}","preventionTips":["Compile-test inline scripts in a standalone Groovy console.","Ensure every referenced variable is present in the bindings map.","Use explicit imports for non-java.lang classes."],"tags":["groovy","scripting","runtime"],"backgroundTag":"script-evaluation-failed","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}