{"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/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/templates/templates.go#L69-L105","documentation":"After resolving a --template value to an absolute path, k6 new reads the file through its filesystem abstraction (fsext.ReadFile). If the file does not exist or is not readable, the error is wrapped as \"failed to read template file <tpl>\".","triggerScenarios":"k6 new --template=./templates/grpc.js script.js where the file is missing or lacks read permission; a path with a wrong directory component; template files not present in a container image.","commonSituations":"Typoed template paths; templates moved between branches; restrictive container filesystems where the template file was never copied in.","solutions":["Verify the path exists and is readable: ls -l <path>","Prefix local files with ./ or use an absolute path so k6 treats the value as a file path rather than a template name","Copy the template into the container/workspace before running k6 new"],"exampleFix":"# before\nk6 new --template=mytpl.js script.js   # file missing\n# after\nk6 new --template=./mytpl.js script.js # and ensure ./mytpl.js exists","handlingStrategy":"validation","validationCode":"tpl=./templates/grpc.js\n[ -f \"$tpl\" ] && [ -r \"$tpl\" ] || { echo \"template missing or unreadable: $tpl\" >&2; exit 2; }\nk6 new --template=\"$tpl\" script.js","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check template files into the repo alongside the scripts using them","Prefix local template files with ./ so k6 classifies them as paths","Fail CI fast with an existence/readability check before k6 new"],"tags":["cli","templates","filesystem"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}