{"record":{"id":"11b2ab5e164babc8","repo":"cloudflare/cloudflared","slug":"error-generating-s-template-v","errorCode":null,"errorMessage":"error generating %s template: %v","messagePattern":"error generating (.+?) template: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/service_template.go","lineNumber":38,"sourceCode":"}\n\ntype ServiceTemplateArgs struct {\n\tPath      string\n\tExtraArgs []string\n}\n\nfunc (st *ServiceTemplate) ResolvePath() (string, error) {\n\tresolvedPath, err := homedir.Expand(st.Path)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"error resolving path %s: %v\", st.Path, err)\n\t}\n\treturn resolvedPath, nil\n}\n\nfunc (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error {\n\ttmpl, err := template.New(st.Path).Parse(st.Content)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error generating %s template: %v\", st.Path, err)\n\t}\n\tresolvedPath, err := st.ResolvePath()\n\tif err != nil {\n\t\treturn err\n\t}\n\tif _, err = os.Stat(resolvedPath); err == nil {\n\t\treturn errors.New(serviceAlreadyExistsWarn(resolvedPath))\n\t}\n\n\tvar buffer bytes.Buffer\n\terr = tmpl.Execute(&buffer, args)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error generating %s: %v\", st.Path, err)\n\t}\n\tfileMode := os.FileMode(0o644)\n\tif st.FileMode != 0 {\n\t\tfileMode = st.FileMode\n\t}","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/service_template.go#L20-L56","documentation":"This error is returned by ServiceTemplate.Generate when the embedded template content fails to parse. Go's text/template package parses st.Content before execution; a parse failure means the template syntax itself is malformed, so no file can ever be rendered. The message includes st.Path (the template's logical identifier, e.g. the launchd plist path) and the underlying parse error.","triggerScenarios":"Calling Generate() on a ServiceTemplate whose Content field contains invalid template syntax — e.g. an unclosed {{ if }}, {{ range }} without {{ end }}, or malformed {{ .Field }} pipelines. For the launchd service this happens inside installLaunchd when the built-in plist template constant was modified or corrupted.","commonSituations":"Hand-editing a custom plist template with unbalanced template actions; accidentally copying template text containing literal {{ }} from docs; build-time string corruption; a new template field referenced with a typo like {{ .Servcie }}</code> combined with bad syntax so it fails at parse rather than execution.","solutions":["Fix the template syntax in the ServiceTemplate.Content (check that every {{ if }}/{{ range }} has a matching {{ end }})","If using a custom template, validate it with template.New(path).Parse(content) in isolation to see the exact line/column of the parse error","Restore the stock cloudflared service template if it was modified","Call Generate only after successfully parsing the template once at startup to fail fast"],"exampleFix":"// before (broken template)\nvar tpl = `...{{ if .Ephemeral }}...{{ end }` // missing closing brace\n// after\nvar tpl = `...{{ if .Ephemeral }}...{{ end }}`","handlingStrategy":"validation","validationCode":"// Pre-validate template syntax before calling Generate\nfunc validateTemplate(path, content string) error {\n\t_, err := template.New(path).Parse(content)\n\treturn err\n}\nif err := validateTemplate(st.Path, st.Content); err != nil {\n\treturn fmt.Errorf(\"template %s is malformed: %w\", st.Path, err)\n}","typeGuard":"func templateParses(content string) bool {\n\t_, err := template.New(\"probe\").Parse(content)\n\treturn err == nil\n}","tryCatchPattern":"if err := st.Generate(args); err != nil {\n\tif strings.HasPrefix(err.Error(), \"error generating\") && strings.Contains(err.Error(), \"parse\") {\n\t\t// malformed template: log content, fix template, do not retry\n\t}\n\treturn err\n}","preventionTips":["Validate every template with Parse at program start or in unit tests","Count {{ }} delimiters and matching {{ end }} tokens when hand-editing templates","Keep templates as checked-in constants with tests that render them against sample args","Avoid building template strings by concatenation at runtime"],"tags":["go","template","syntax","macos","launchd"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}