alibaba/spring-ai-alibaba · error · RuntimeException

Got error when creating file

Error message

Got error when creating file

What it means

After rendering, renderAndWriteTemplates calls Files.createFile(fileRoot.resolve(templateName)); an IOException here (file already exists, path invalid, permission denied) is wrapped in this RuntimeException with no template name in the message. It aborts project generation because the output file could not be created.

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:287

	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. Delete or clean the output directory before regenerating the project.
  2. Ensure the fileRoot output directory exists and is writable by the process.
  3. Check that templateName has no path separators or illegal filename characters.

Example fix

// before
Files.createFile(fileRoot.resolve(templateName));
// after
Path target = fileRoot.resolve(templateName);
Files.createDirectories(target.getParent());
Files.deleteIfExists(target);
file = Files.createFile(target);
Defensive patterns

Strategy: fallback

Validate before calling

Path out = fileRoot.resolve(templateName);
if (Files.exists(out)) Files.delete(out);
Files.createDirectories(out.getParent());

Try / catch

try { generator.generate(spec); } catch (RuntimeException e) { if (e.getMessage().equals("Got error when creating file")) { FileUtils.deleteDirectory(outputDir); retry(); } }

Prevention

When it happens

Trigger: generate() -> renderAndWriteTemplates: Files.createFile fails, typically because the target file already exists in the output directory (NO_SUCH_FILE/ALREADY_EXISTS), the parent directory is missing, or the process lacks write permission.

Common situations: Re-running generation into an existing output directory without cleaning it; templateName contains path characters like '/'; read-only output directory or container filesystem.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


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