{"record":{"id":"db22c60fa0d3c32c","repo":"RicoSuter/NSwag","slug":"the-template-directory-codegenerators-db22c6","errorCode":null,"errorMessage":"The template directory {CodeGenerators.OpenApiToCSharpControllerCommand.TemplateDirectory}\\\" does not exist","messagePattern":"The template directory (.+?)\\\\\" does not exist","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/NSwagDocumentBase.cs","lineNumber":357,"sourceCode":"                CodeGenerators.OpenApiToCSharpClientCommand.ContractsOutputFilePath = ConvertToAbsolutePath(\r\n                    CodeGenerators.OpenApiToCSharpClientCommand.ContractsOutputFilePath);\r\n                CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory = ConvertToAbsolutePath(\r\n                    CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory);\r\n\r\n                if (CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory != null && !Directory.Exists(CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory))\r\n                {\r\n                    throw new InvalidOperationException($\"The template directory \\\"{CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory}\\\" does not exist\");\r\n                }\r\n            }\r\n\r\n            if (CodeGenerators.OpenApiToCSharpControllerCommand != null)\r\n            {\r\n                CodeGenerators.OpenApiToCSharpControllerCommand.TemplateDirectory = ConvertToAbsolutePath(\r\n                    CodeGenerators.OpenApiToCSharpControllerCommand.TemplateDirectory);\r\n\r\n                if (CodeGenerators.OpenApiToCSharpControllerCommand.TemplateDirectory != null && !Directory.Exists(CodeGenerators.OpenApiToCSharpControllerCommand.TemplateDirectory))\r\n                {\r\n                    throw new InvalidOperationException($\"The template directory {CodeGenerators.OpenApiToCSharpControllerCommand.TemplateDirectory}\\\" does not exist\");\r\n                }\r\n            }\r\n\r\n            foreach (var generator in CodeGenerators.Items.Concat(SwaggerGenerators.Items))\r\n            {\r\n                generator.OutputFilePath = ConvertToAbsolutePath(generator.OutputFilePath);\r\n            }\r\n        }\r\n\r\n        private void ConvertToRelativePaths()\r\n        {\r\n            if (SwaggerGenerators.FromDocumentCommand != null)\r\n            {\r\n                if (!SwaggerGenerators.FromDocumentCommand.Url.StartsWith(\"http://\", StringComparison.OrdinalIgnoreCase)\r\n                    && !SwaggerGenerators.FromDocumentCommand.Url.StartsWith(\"https://\", StringComparison.OrdinalIgnoreCase))\r\n                {\r\n                    SwaggerGenerators.FromDocumentCommand.Url = ConvertToRelativePath(SwaggerGenerators.FromDocumentCommand.Url);\r\n                }\r","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NSwagDocumentBase.cs#L339-L375","documentation":"Same validation for OpenApiToCSharpControllerCommand: a non-null TemplateDirectory that does not exist after absolute-path conversion throws this InvalidOperationException (note the malformed message string with a stray quote — a cosmetic bug, but the condition is identical).","triggerScenarios":"CodeGenerators.OpenApiToCSharpControllerCommand.TemplateDirectory is set but Directory.Exists returns false after ConvertToAbsolutePath — path typo, missing directory, or wrong working directory for a relative path.","commonSituations":"C# controller (ASP.NET Core web API) generation documents with custom templates referenced relatively; running from another directory or machine where the templates folder is absent.","solutions":["Correct templateDirectory so it points to an existing directory.","Use an absolute path in the document, or run from the directory the relative path assumes.","Remove the templateDirectory entry to fall back to built-in templates."],"exampleFix":"// before\n\"templateDirectory\": \"templates/controller\"\n// after\n\"templateDirectory\": \"D:/src/templates/controller\"","handlingStrategy":"validation","validationCode":"var td = doc.CodeGenerators?.OpenApiToCSharpControllerCommand?.TemplateDirectory;\nif (!string.IsNullOrEmpty(td) && !Directory.Exists(Path.GetFullPath(td, basePath)))\n    throw new DirectoryNotFoundException($\"Template directory not found: {td}\");","typeGuard":null,"tryCatchPattern":"try { var json = document.ToJsonWithRelativePaths(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not exist\") && ex.Message.Contains(\"template directory\"))\n{ Console.Error.WriteLine($\"Fix csharpController templateDirectory: {ex.Message}\"); throw; }","preventionTips":["Use absolute templateDirectory paths or align the working directory.","Deploy custom controller templates with the build.","Run a directory-existence pre-check before invoking NSwag generation."],"tags":["filesystem","path","templates","configuration"],"backgroundTag":"directory-not-found","analyzedSha":"63daf8fcc3a25151b62eb4b326a1e8ea048a0d41","analyzedAt":"2026-09-14T11:38:15.205Z","contentChangedAt":"2026-09-14T11:38:15.205Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}