{"record":{"id":"7575ee283c35a405","repo":"OpenAPITools/openapi-generator","slug":"can-t-load-template-name","errorCode":null,"errorMessage":"can't load template {name}","messagePattern":"can't load template (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"modules/openapi-generator/src/main/java/org/openapitools/codegen/TemplateManager.java","lineNumber":141,"sourceCode":"     * @return The raw template contents\n     */\n    @SuppressWarnings(\"java:S112\")\n    // ignored rule java:S112 as RuntimeException is used to match previous exception type\n    public String readTemplate(String name) {\n        if (name == null || name.contains(\"..\")) {\n            throw new IllegalArgumentException(\"Template location must be constrained to template directory.\");\n        }\n        try (Reader reader = getTemplateReader(name)) {\n            if (reader == null) {\n                throw new RuntimeException(\"no file found\");\n            }\n            try (Scanner s = new Scanner(reader).useDelimiter(\"\\\\A\")) {\n                return s.hasNext() ? s.next() : \"\";\n            }\n        } catch (Exception e) {\n            LOGGER.error(\"{}\", e.getMessage(), e);\n        }\n        throw new RuntimeException(\"can't load template \" + name);\n    }\n\n    @SuppressWarnings({\"squid:S2095\", \"java:S112\"})\n    // ignored rule squid:S2095 as used in the CLI and it's required to return a reader\n    // ignored rule java:S112 as RuntimeException is used to match previous exception type\n    public Reader getTemplateReader(String name) {\n        try {\n            InputStream is = getInputStream(name);\n            return new InputStreamReader(is, StandardCharsets.UTF_8);\n        } catch (IOException e) {\n            LOGGER.error(e.getMessage());\n            throw new RuntimeException(\"can't load template \" + name);\n        }\n    }\n\n    private InputStream getInputStream(String name) throws IOException {\n        if (name == null || name.contains(\"..\")) {\n            throw new IllegalArgumentException(\"Template location must be constrained to template directory.\");","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/OpenAPITools/openapi-generator/blob/fcec517be3cf5b7964296bcba25fbc97541484e7/modules/openapi-generator/src/main/java/org/openapitools/codegen/TemplateManager.java#L123-L159","documentation":"Terminal failure of TemplateManager.readTemplate(name): either getTemplateReader(name) returned null (wrapped 'no file found') or the Scanner/read threw, both caught and logged, after which this RuntimeException is thrown. The template name passed the traversal guard and possibly produced a resolvable path, but the actual read failed. The original exception is logged (with stack trace) but not attached as the cause of the rethrow.","triggerScenarios":"Template exists in the template-dir listing but the file is unreadable (permissions), deleted between resolution and read, or the classpath resource lookup returns a URL whose stream cannot be opened; also the intermediate 'no file found' path when no loader yields a reader.","commonSituations":"Docker images running as non-root with restricted template file permissions; concurrent builds mutating template directories; stale or corrupted generator JARs where resource entries exist but streams fail; NFS/symlink oddities in CI.","solutions":["Check the preceding LOGGER.error line — it prints the underlying cause (null reader vs exception) that this message omits.","Verify file permissions/readability of the template in the -t directory and inside the generator JAR.","Re-download/rebuild the openapi-generator JAR if classpath resources are corrupt.","Ensure the template directory is not being modified concurrently during generation."],"exampleFix":"# before: template dir mounted read-only to another user\n--template-dir /opt/tpl   # /opt/tpl/model.mustache mode 600 owned by other user\n# after\nchmod 644 /opt/tpl/model.mustache  # or chown to the running user","handlingStrategy":"try-catch","validationCode":"// Check readability before generation\nPath p = Paths.get(templateDir, name);\nif (!Files.isReadable(p)) throw new IOException(\"template not readable: \" + p);","typeGuard":null,"tryCatchPattern":"try { contents = manager.readTemplate(name); } catch (RuntimeException e) { log.error(\"template read failed for {}\", name, e); /* surface LOGGER.error output — it holds the root cause */ throw e; }","preventionTips":["Check template file permissions in Docker/CI images.","Keep template directories immutable during builds.","When debugging, read the logged cause line above the rethrow — the wrapper drops the cause."],"tags":["openapi","template","io","code-generation","runtime-exception"],"backgroundTag":"template-read-failed","analyzedSha":"fcec517be3cf5b7964296bcba25fbc97541484e7","analyzedAt":"2026-08-22T11:13:11.613Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}