{"record":{"id":"9e7e2797d30fa90b","repo":"alibaba/spring-ai-alibaba","slug":"got-error-when-creating-file","errorCode":null,"errorMessage":"Got error when creating file","messagePattern":"Got error when creating file","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"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","lineNumber":287,"sourceCode":"\tprivate void renderAndWriteTemplates(List<String> templateNames, List<Map<String, Object>> models, Path projectRoot,\n\t\t\tProjectDescription projectDescription) {\n\t\t// todo: may to standardize the code format via the IdentifierGeneratorFactory\n\t\tPath fileRoot = ContributorFileUtil.createDirectory(projectRoot, projectDescription);\n\t\tfor (int i = 0; i < templateNames.size(); i++) {\n\t\t\tString templateName = templateNames.get(i);\n\t\t\tString template;\n\t\t\ttry {\n\t\t\t\ttemplate = templateRenderer.render(templateName, models.get(i));\n\t\t\t}\n\t\t\tcatch (IOException e) {\n\t\t\t\tthrow new RuntimeException(\"Got error when rendering template\" + templateName, e);\n\t\t\t}\n\t\t\tPath file;\n\t\t\ttry {\n\t\t\t\tfile = Files.createFile(fileRoot.resolve(templateName));\n\t\t\t}\n\t\t\tcatch (IOException e) {\n\t\t\t\tthrow new RuntimeException(\"Got error when creating file\", e);\n\t\t\t}\n\t\t\ttry (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(file))) {\n\t\t\t\twriter.print(template);\n\t\t\t}\n\t\t\tcatch (IOException e) {\n\t\t\t\tthrow new RuntimeException(\"Got error when writing template \" + templateName, e);\n\t\t\t}\n\t\t}\n\t}\n\n}\n","sourceCodeStart":269,"sourceCodeEnd":299,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/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#L269-L299","documentation":"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.","triggerScenarios":"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.","commonSituations":"Re-running generation into an existing output directory without cleaning it; templateName contains path characters like '/'; read-only output directory or container filesystem.","solutions":["Delete or clean the output directory before regenerating the project.","Ensure the fileRoot output directory exists and is writable by the process.","Check that templateName has no path separators or illegal filename characters."],"exampleFix":"// before\nFiles.createFile(fileRoot.resolve(templateName));\n// after\nPath target = fileRoot.resolve(templateName);\nFiles.createDirectories(target.getParent());\nFiles.deleteIfExists(target);\nfile = Files.createFile(target);","handlingStrategy":"fallback","validationCode":"Path out = fileRoot.resolve(templateName);\nif (Files.exists(out)) Files.delete(out);\nFiles.createDirectories(out.getParent());","typeGuard":null,"tryCatchPattern":"try { generator.generate(spec); } catch (RuntimeException e) { if (e.getMessage().equals(\"Got error when creating file\")) { FileUtils.deleteDirectory(outputDir); retry(); } }","preventionTips":["Always generate into a clean, empty output directory","Ensure the output directory exists and is writable","Reject template names containing path separators or illegal filename chars"],"tags":["file-io","file-already-exists","code-generation"],"backgroundTag":"file-already-exists","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}