{"record":{"id":"a760cc5305076a34","repo":"grafana/k6","slug":"failed-to-read-template-file-s-w","errorCode":null,"errorMessage":"failed to read template file %s: %w","messagePattern":"failed to read template file (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/templates/templates.go","lineNumber":87,"sourceCode":"\tcase MinimalTemplate:\n\t\treturn tm.minimalTemplate, nil\n\tcase ProtocolTemplate:\n\t\treturn tm.protocolTemplate, nil\n\tcase BrowserTemplate:\n\t\treturn tm.browserTemplate, nil\n\t}\n\n\t// Then check if it's a file path\n\tif isFilePath(tpl) {\n\t\ttplPath, err := filepath.Abs(tpl)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to get absolute path for template %s: %w\", tpl, err)\n\t\t}\n\n\t\t// Read the template content using the provided filesystem\n\t\tcontent, err := fsext.ReadFile(tm.fs, tplPath)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to read template file %s: %w\", tpl, err)\n\t\t}\n\n\t\ttmpl, err := template.New(filepath.Base(tplPath)).Parse(string(content))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse template file %s: %w\", tpl, err)\n\t\t}\n\n\t\treturn tmpl, nil\n\t}\n\n\t// Check if there's a file with this name in current directory\n\texists, err := fsext.Exists(tm.fs, fsext.JoinFilePath(\".\", tpl))\n\tif err == nil && exists {\n\t\treturn nil, fmt.Errorf(\"invalid template type %q, did you mean ./%s?\", tpl, tpl)\n\t}\n\n\treturn nil, fmt.Errorf(\"invalid template type %q\", tpl)\n}","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/cmd/templates/templates.go#L69-L105","documentation":"After resolving a custom `k6 new -t <path>` template to an absolute path, k6 reads it through fsext.ReadFile; any read failure (file not found, permission denied, path is a directory) is wrapped as 'failed to read template file <path>'. This is the most common custom-template error — the path resolved, but could not be read.","triggerScenarios":"k6 new -t ./templates/my.js when the file does not exist (note: k6 resolves relative to cwd, so the typo'd directory silently produces this); file exists but is not readable by the k6 user; path points at a directory (paths with separators skip the built-in name checks).","commonSituations":"Wrong relative path from a different working directory; templates committed in another repo/location; permission-restricted mounts in CI; assuming -t accepts URLs (it does not — only local paths).","solutions":["Verify the file exists and is readable: ls -l ./templates/my.js, and check you are in the directory you think","Use an absolute path: k6 new -t /home/me/k6-templates/my.js","If you meant a built-in template, use -t minimal, -t protocol, or -t browser (no path separators)"],"exampleFix":"# before (file actually lives in ./templates/, not ./template/)\nk6 new -t template/my.js new-script.js\n\n# after\nk6 new -t ./templates/my.js new-script.js","handlingStrategy":"validation","validationCode":"# verify the template file is readable before calling k6 new\ntpl=\"$1\"\nif ! [ -f \"$tpl\" ]; then echo \"template not found: $tpl\" >&2; exit 1; fi\nif ! [ -r \"$tpl\" ]; then echo \"template not readable: $tpl\" >&2; exit 1; fi\nk6 new -t \"$tpl\" new-script.js","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check paths with ls/test before running — k6 resolves -t relative to the current directory only","Use absolute template paths in scripts and CI to avoid cwd surprises","Remember built-in names are minimal, protocol, browser — a bare name with a dot (script.js) is never built-in"],"tags":["k6","k6-new","template","file-not-found","filesystem"],"backgroundTag":"file-not-found","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}