{"record":{"id":"d239cd320d457988","repo":"gitleaks/gitleaks","slug":"error-reading-file-w","errorCode":null,"errorMessage":"error reading file: %w","messagePattern":"error reading file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"report/template.go","lineNumber":26,"sourceCode":"\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)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error parsing file: %w\", err)\n\t}\n\treturn &TemplateReporter{template: t}, nil\n}","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/gitleaks/gitleaks/blob/b58d3f102cf3a2c84cb7f923d05c25c9b1aed84b/report/template.go#L8-L44","documentation":"NewTemplateReporter could not read the Go text/template file passed via --report-template. os.ReadFile failed on the path - missing, wrong directory, or unreadable. Unlike error 37 this one wraps the OS error (%w), so errors.Is(err, fs.ErrNotExist) works.","triggerScenarios":"Running gitleaks with --report-template custom.tmpl when the file does not exist at that path, typically a relative path resolved against the wrong working directory.","commonSituations":"Template files kept outside the repo, CI steps that skip the template checkout, or typos in the path. The empty-path case is a separate earlier error ('template path cannot be empty').","solutions":["Check the template file exists at the given path from the directory you run gitleaks in","Use an absolute path or ship the template inside the repo","After the read succeeds, parse errors from bad template syntax will surface separately - fix those next"],"exampleFix":"# before\ngitleaks git --report-template report.tmpl --report-path out.txt .\n# report.tmpl lives in templates/\n\n# after\ngitleaks git --report-template templates/report.tmpl --report-path out.txt .","handlingStrategy":"validation","validationCode":"// Verify the template file exists before constructing the reporter.\nif templatePath != \"\" {\n    if _, err := os.Stat(templatePath); err != nil {\n        return fmt.Errorf(\"report template %q missing: %w\", templatePath, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"r, err := report.NewTemplateReporter(p)\nif err != nil {\n    if errors.Is(err, fs.ErrNotExist) {\n        // path problem: check working directory or use an absolute path\n    }\n    return err // other errors: template parse failures\n}","preventionTips":["Keep template files inside the repo and reference them by stable relative or absolute path","Run gitleaks from the repo root in CI so relative paths resolve","Note sprig's env/expandenv/getHostByName are deleted from the func map - do not use them in templates"],"tags":["gitleaks","go","file-io","template","cli"],"backgroundTag":null,"analyzedSha":"b58d3f102cf3a2c84cb7f923d05c25c9b1aed84b","analyzedAt":"2026-08-15T22:17:28.846Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}