{"record":{"id":"c713e2cb4b1c30ec","repo":"RicoSuter/NSwag","slug":"the-template-directory-codegenerators-nswagdocumentbase","errorCode":null,"errorMessage":"The template directory \\\"{CodeGenerators.OpenApiToCSharpClientCommand.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":346,"sourceCode":"                CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory = ConvertToAbsolutePath(\r\n                    CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory);\r\n\r\n                if (CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory != null && !Directory.Exists(CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory))\r\n                {\r\n                    throw new InvalidOperationException($\"The template directory \\\"{CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory}\\\" does not exist\");\r\n                }\r\n            }\r\n\r\n            if (CodeGenerators.OpenApiToCSharpClientCommand != null)\r\n            {\r\n                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","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NSwagDocumentBase.cs#L328-L364","documentation":"The same template-directory validation as the TypeScript command, applied to OpenApiToCSharpClientCommand: after converting TemplateDirectory to an absolute path, a non-null value that does not exist on disk throws this InvalidOperationException.","triggerScenarios":"CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory is set (non-null) but Directory.Exists is false after path resolution — bad relative path, wrong base directory, deleted/moved directory.","commonSituations":"C# client customization templates referenced with paths relative to the machine where the document was created; running nswag.json from CI with a different working directory.","solutions":["Fix templateDirectory to an existing directory path.","Use an absolute path or fix the working directory so the relative path resolves correctly.","Delete the setting if custom templates are not needed."],"exampleFix":"// before\n\"templateDirectory\": \"../templates/cs\"\n// after\n\"templateDirectory\": \"/build/repo/templates/cs\"","handlingStrategy":"validation","validationCode":"var td = doc.CodeGenerators?.OpenApiToCSharpClientCommand?.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(\"OpenApiToCSharpClientCommand\") || (ex.Message.Contains(\"template directory\") && ex.Message.Contains(\"does not exist\")))\n{ Console.Error.WriteLine($\"Fix csharpClient templateDirectory: {ex.Message}\"); throw; }","preventionTips":["Prefer absolute templateDirectory paths in checked-in documents.","Ensure template folders are committed/deployed with the project.","Validate all configured directories exist as a pre-flight step in pipelines."],"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"}