{"record":{"id":"a558e59be6e18877","repo":"grafana/k6","slug":"failed-to-parse-template-file-s-w","errorCode":null,"errorMessage":"failed to parse template file %s: %w","messagePattern":"failed to parse template file (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/templates/templates.go","lineNumber":92,"sourceCode":"\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}\n\n// isFilePath checks if the given string looks like a file path by detecting path separators\n// We assume that built-in template names don't contain path separators\nfunc isFilePath(path string) bool {\n\treturn strings.ContainsRune(path, filepath.Separator) || strings.ContainsRune(path, '/')","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/cmd/templates/templates.go#L74-L110","documentation":"The custom template file was read successfully but does not parse as a Go text/template (k6 new templates use {{.Field}} actions such as {{.Name}}). Syntax errors — an unclosed {{, mismatched actions, unknown functions or pipelines — are wrapped with the offending file's path. Parse happens before any output is generated, so nothing is written on failure.","triggerScenarios":"k6 new -t ./my.tpl where my.tpl contains '{{' with no closing braces, '{{.Name | nosuchfunc}}', a stray '{{end}}', or JavaScript template literals that accidentally open a {{ action.","commonSituations":"Hand-editing a copied built-in template; JS code containing '{{' sequences (e.g. raw template strings) that Go's parser tries to interpret; merging template fragments from different sources.","solutions":["Fix the template syntax: every {{ must have a matching }}, and actions must be valid text/template expressions","For literal braces in output, escape them: {{\"{{\"}} and {{\"}}\"}}","Start from a working base: run `k6 new x.js` with a built-in template, copy the generated file, edit minimally, then pass -t ./copy"],"exampleFix":"// before (my.tpl): unclosed action + literal braces\nexport const opts = {{ vus: 1 };\n\n// after: no template action, plain literal braces\nexport const opts = { vus: 1 };","handlingStrategy":"validation","validationCode":"// parse-check the template with Go before using it (exit non-zero on bad syntax)\npackage main\n\nimport (\n  \"fmt\"\n  \"os\"\n  \"text/template\"\n)\n\nfunc main() {\n  b, err := os.ReadFile(os.Args[1])\n  if err != nil { fmt.Println(err); os.Exit(1) }\n  if _, err := template.New(\"t\").Parse(string(b)); err != nil { fmt.Println(err); os.Exit(1) }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Every {{ needs a matching }}; run a parse check on custom templates in CI before k6 new uses them","Escape literal braces as {{\"{{\"}} / {{\"}}\"}} instead of leaving bare braces in JS template literals","Derive custom templates from a working built-in output (run k6 new, copy, edit minimally) rather than from scratch"],"tags":["k6","k6-new","template","go-template","parse-error"],"backgroundTag":"template-parse-error","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"}