{"record":{"id":"1e2e4b6203db5bdb","repo":"gitleaks/gitleaks","slug":"template-path-cannot-be-empty","errorCode":null,"errorMessage":"template path cannot be empty","messagePattern":"template path cannot be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"report/template.go","lineNumber":21,"sourceCode":"import (\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"text/template\"\n\n\t\"github.com/Masterminds/sprig/v3\"\n)\n\ntype TemplateReporter struct {\n\ttemplate *template.Template\n}\n\nvar _ Reporter = (*TemplateReporter)(nil)\n\nfunc NewTemplateReporter(templatePath string) (*TemplateReporter, error) {\n\tif templatePath == \"\" {\n\t\treturn nil, errors.New(\"template path cannot be empty\")\n\t}\n\n\tfile, err := os.ReadFile(templatePath)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error reading file: %w\", err)\n\t}\n\ttemplateText := string(file)\n\n\t// TODO: Add helper functions like escaping for JSON, XML, etc.\n\tt := template.New(\"custom\")\n\n\tfuncMap := sprig.TxtFuncMap()\n\tdelete(funcMap, \"env\")\n\tdelete(funcMap, \"expandenv\")\n\tdelete(funcMap, \"getHostByName\")\n\n\tt = t.Funcs(funcMap)\n\tt, err = t.Parse(templateText)","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/gitleaks/gitleaks/blob/b58d3f102cf3a2c84cb7f923d05c25c9b1aed84b/report/template.go#L3-L39","documentation":"The template reporter (report/template.go) renders findings through a Go text/template loaded from a user-supplied file; NewTemplateReporter requires a non-empty path because there is no default template to fall back on. It is invoked when report-format template is selected, so an empty --report-template is always a caller mistake, and the empty check runs before os.ReadFile would produce a confusing 'no such file' error.","triggerScenarios":"Running gitleaks git/detect with --report-format template but omitting --report-template; passing an empty string via an unset environment variable interpolated into the CLI invocation.","commonSituations":"CI pipelines that copy a report-format=template line from docs without the companion --report-template flag; scripts where the template path variable is conditionally empty; switching from json format and forgetting the extra required flag.","solutions":["Pass a template file: --report-template=report.tmpl alongside --report-format template.","Create the template using text/template syntax; sprig functions are available (funcMap is sprig.TxtFuncMap).","If you did not want a custom layout, switch back to --report-format json, sarif, csv, or junit."],"exampleFix":"# before\ngitleaks detect --source . --report-format template --report-path out.txt\n\n# after\ngitleaks detect --source . --report-format template --report-template=./report.tmpl --report-path out.txt","handlingStrategy":"validation","validationCode":"# bash: require the template file when template format is selected\nif [[ \"$REPORT_FORMAT\" == \"template\" ]]; then\n  [[ -n \"$REPORT_TEMPLATE\" && -f \"$REPORT_TEMPLATE\" ]] || { echo \"error: --report-template <file> is required for template format\" >&2; exit 2; }\nfi\ngitleaks detect --source . --report-format=\"$REPORT_FORMAT\" --report-template=\"${REPORT_TEMPLATE:-}\" --report-path=out.txt","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set --report-template together with --report-format template.","Keep the template under version control next to the config.","Prefer built-in formats (json, sarif, csv, junit) unless a custom layout is truly needed."],"tags":["gitleaks","reporting","template","cli-flags","validation"],"backgroundTag":null,"analyzedSha":"b58d3f102cf3a2c84cb7f923d05c25c9b1aed84b","analyzedAt":"2026-08-15T22:17:28.846Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}