{"record":{"id":"e180b5f29d8457b4","repo":"RicoSuter/NSwag","slug":"the-template-directory-codegenerators","errorCode":null,"errorMessage":"The template directory \\\"{CodeGenerators.OpenApiToTypeScriptClientCommand.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":333,"sourceCode":"                SwaggerGenerators.AspNetCoreToOpenApiCommand.Project = ConvertToAbsolutePath(\r\n                    SwaggerGenerators.AspNetCoreToOpenApiCommand.Project);\r\n                SwaggerGenerators.AspNetCoreToOpenApiCommand.MSBuildProjectExtensionsPath = ConvertToAbsolutePath(\r\n                    SwaggerGenerators.AspNetCoreToOpenApiCommand.MSBuildProjectExtensionsPath);\r\n\r\n                SwaggerGenerators.AspNetCoreToOpenApiCommand.WorkingDirectory = ConvertToAbsolutePath(\r\n                    SwaggerGenerators.AspNetCoreToOpenApiCommand.WorkingDirectory);\r\n            }\r\n\r\n            if (CodeGenerators.OpenApiToTypeScriptClientCommand != null)\r\n            {\r\n                CodeGenerators.OpenApiToTypeScriptClientCommand.ExtensionCode = ConvertToAbsolutePath(\r\n                    CodeGenerators.OpenApiToTypeScriptClientCommand.ExtensionCode);\r\n                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","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NSwagDocumentBase.cs#L315-L351","documentation":"During ConvertToAbsolutePaths, NSwag converts the TypeScript client generator's TemplateDirectory to an absolute path and validates it exists. If set but not found on disk, this InvalidOperationException is thrown while serializing the document with absolute paths.","triggerScenarios":"CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory is non-null and non-empty but the directory does not exist (relative path resolved against the wrong base directory, path typo, or directory deleted).","commonSituations":"Documents using custom TypeScript templates moved between machines or run from a different working directory, so relative template paths no longer resolve; typos in the templateDirectory setting.","solutions":["Correct the templateDirectory value to point at an existing directory.","Use an absolute path, or ensure the document's base directory/working directory matches where the relative path resolves.","Remove the templateDirectory setting if default templates are intended."],"exampleFix":"// before (nswag.json)\n\"templateDirectory\": \"./templates\"\n// after\n\"templateDirectory\": \"C:/project/templates\"","handlingStrategy":"validation","validationCode":"var td = doc.CodeGenerators?.OpenApiToTypeScriptClientCommand?.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(\"template directory\") && ex.Message.Contains(\"does not exist\"))\n{ Console.Error.WriteLine($\"Check templateDirectory in nswag.json: {ex.Message}\"); throw; }","preventionTips":["Use absolute paths for templateDirectory in shared documents.","Copy template directories along with the document between machines.","Run the CLI from the directory the relative paths assume, or set the document path accordingly.","Validate referenced directories exist before running generation in CI."],"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"}