theonedev/onedev · error · RuntimeException

Error evaluating groovy script:

Error message

Error evaluating groovy script:

What it means

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.

Source

Thrown at server-core/src/main/java/io/onedev/server/util/GroovyUtils.java:117

    }
    
    public static Object evalScript(String scriptContent, Map<String, Object> variables) {
    	try {
	    	Class<?> scriptClass = compile(scriptContent);
			Script script;
			try {
				Object instance = scriptClass.getDeclaredConstructor().newInstance();
				if (!(instance instanceof Script))
					return scriptClass;
				else 
					script = (Script) instance;					
			} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
				throw new RuntimeException(e);
			}
			script.setBinding(getBinding(variables));
			return script.run();
		} catch (RuntimeException e) {
			throw new RuntimeException("Error evaluating groovy script:\n\n" + scriptContent, e);
		}
    }
    
    public static Object evalScript(String scriptContent) {
    	return evalScript(scriptContent, new HashMap<>());
    }
    
	public static String evalTemplate(String template, Map<String, Object> bindings) {
		// Make a copy of the bindings as the template engine will modify the bindings
		var bindingsCopy = new HashMap<>(bindings);
		try {
			return new SimpleTemplateEngine().createTemplate(template).make(bindingsCopy).toString();
		} catch (CompilationFailedException | ClassNotFoundException | IOException e) {
			throw new RuntimeException(e);
		}
	}	
}

View on GitHub (pinned to d44925c47c)

Solutions

  1. Inspect the cause chain — the nested exception pinpoints compile vs runtime failure and the script line.
  2. Validate the script syntax in a standalone Groovy console before embedding it.
  3. Ensure every variable the script reads is present in the variables map passed to evalScript.
  4. Add explicit imports for classes used and confirm they exist on the server classpath.

Example fix

// before: missing variable in binding
Object r = GroovyUtils.evalScript("return value * 2"); // 'value' undefined -> RuntimeException
// after
Map<String, Object> vars = new HashMap<>();
vars.put("value", 21);
Object r = GroovyUtils.evalScript("return value * 2", vars);
Defensive patterns

Strategy: try-catch

Try / catch

try {
    return GroovyUtils.evalScript(content, variables);
} catch (RuntimeException e) {
    Throwable cause = ExceptionUtils.getRootCause(e);
    log.error("Inline script failed: {}", cause.getMessage(), cause);
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/184822835a638567. Report an issue: GitHub.