alibaba/spring-ai-alibaba · critical · RuntimeException

Got error when rendering template${templateName}

Error message

Got error when rendering template${templateName}

What it means

WorkflowProjectGenerator.renderAndWriteTemplates renders each template via TemplateRenderer.render and wraps any IOException from rendering in a RuntimeException. The message omits a separator between the literal text and the template name, so the template name is concatenated directly (e.g. '...templatepom.xml'). It indicates the template resource could not be read/rendered, aborting code generation.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/builder/generator/service/generator/workflow/WorkflowProjectGenerator.java:280

		StringBuilder sb = new StringBuilder();
		allImports.forEach(className -> sb.append("import ").append(className).append(";\n"));

		return sb.toString();
	}

	private void renderAndWriteTemplates(List<String> templateNames, List<Map<String, Object>> models, Path projectRoot,
			ProjectDescription projectDescription) {
		// todo: may to standardize the code format via the IdentifierGeneratorFactory
		Path fileRoot = ContributorFileUtil.createDirectory(projectRoot, projectDescription);
		for (int i = 0; i < templateNames.size(); i++) {
			String templateName = templateNames.get(i);
			String template;
			try {
				template = templateRenderer.render(templateName, models.get(i));
			}
			catch (IOException e) {
				throw new RuntimeException("Got error when rendering template" + templateName, e);
			}
			Path file;
			try {
				file = Files.createFile(fileRoot.resolve(templateName));
			}
			catch (IOException e) {
				throw new RuntimeException("Got error when creating file", e);
			}
			try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(file))) {
				writer.print(template);
			}
			catch (IOException e) {
				throw new RuntimeException("Got error when writing template " + templateName, e);
			}
		}
	}

}

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Rebuild the module (./mvnw -pl :spring-ai-alibaba-admin-server-start package) so template resources are on the classpath.
  2. Verify each templateName in templateNames maps to an existing template resource.
  3. Run the generator via the full StudioApplication, not a stripped-down main, so resource loading works.

Example fix

// before
throw new RuntimeException("Got error when rendering template" + templateName, e);
// after
throw new RuntimeException("Got error when rendering template " + templateName, e); // also check the template exists on the classpath
Defensive patterns

Strategy: try-catch

Validate before calling

for (String name : templateNames) {
    if (getClass().getResource("/templates/" + name) == null)
        throw new IllegalStateException("Missing template resource: " + name);
}

Try / catch

try { generator.generate(spec); } catch (RuntimeException e) { if (e.getMessage().startsWith("Got error when rendering template")) { /* rebuild/redeploy templates */ } throw e; }

Prevention

When it happens

Trigger: generate() -> renderAndWriteTemplates: TemplateRenderer.render(templateName, models.get(i)) throws IOException for a template in templateNames, e.g. missing template resource on the classpath or unreadable template file.

Common situations: Template file missing from packaged resources after a partial build; wrong working directory when running the generator standalone; classpath resources not copied in an IDE run configuration.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/ac0e12958cf0857c. Report an issue: GitHub.