{"record":{"id":"54cef1c2226dda22","repo":"grafana/k6","slug":"failed-to-execute-template-s-w","errorCode":null,"errorMessage":"failed to execute template %s: %w","messagePattern":"failed to execute template (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/new.go","lineNumber":67,"sourceCode":"\tif err != nil {\n\t\treturn fmt.Errorf(\"error initializing template manager: %w\", err)\n\t}\n\n\ttmpl, err := tm.GetTemplate(c.templateType)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error retrieving template: %w\", err)\n\t}\n\n\t// Prepare template arguments\n\targsStruct := templates.TemplateArgs{\n\t\tScriptName: target,\n\t\tProjectID:  c.projectID,\n\t}\n\n\t// First render the template to a buffer to validate it\n\tvar buf strings.Builder\n\tif err := templates.ExecuteTemplate(&buf, tmpl, argsStruct); err != nil {\n\t\treturn fmt.Errorf(\"failed to execute template %s: %w\", c.templateType, err)\n\t}\n\n\t// Only create the file after template rendering succeeds\n\tfd, err := c.gs.FS.Create(target)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tdefer func() {\n\t\tif cerr := fd.Close(); cerr != nil {\n\t\t\tif _, werr := fmt.Fprintf(c.gs.Stderr, \"error closing file: %v\\n\", cerr); werr != nil {\n\t\t\t\terr = fmt.Errorf(\"error writing error message to stderr: %w\", werr)\n\t\t\t} else {\n\t\t\t\terr = cerr\n\t\t\t}\n\t\t}\n\t}()\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/new.go#L49-L85","documentation":"Returned by `k6 new` when the selected template renders into the validation buffer and text/template execution fails (templates.ExecuteTemplate). Rendering happens fully in memory before the target file is created, so this never leaves a half-written script; failures come from the template itself (bad action syntax in a custom template, output-write errors to the strings.Builder) rather than from user options.","triggerScenarios":"Using --template with a custom file containing invalid text/template actions ({{ .ScriptName } typos, unknown functions) or referencing fields absent from TemplateArgs (ScriptName, ProjectID); built-in templates cannot trigger it in practice.","commonSituations":"Teams maintaining private k6 script templates; template files edited by hand where a brace got mangled; templates written for a different k6 version whose TemplateArgs changed.","solutions":["Open the custom template file and check every {{ ... }} action — balanced braces, defined fields (.ScriptName, .ProjectID), only standard template functions","Render it standalone to see the raw template error: `go run` a snippet calling text/template on the file, or temporarily trim the template to the failing line","Fall back to a built-in template (`--template minimal`) to confirm the rest of the command works","Fix the template, then re-run `k6 new --template ./fixed.tmpl script.js`"],"exampleFix":"# before (custom.tmpl)\nimport http from 'k6/http';\n{{ .ScriptNames }}  # typo: no such field\n\n# after\nimport http from 'k6/http';\n// generated for {{ .ScriptName }}","handlingStrategy":"validation","validationCode":"# Render custom template standalone with the same args k6 uses; fail before k6 new\ngo run ./tools/rendercheck ./my.tmpl '{\"ScriptName\":\"s\",\"ProjectID\":\"\"}' >/dev/null \\\n  || { echo 'template does not render'; exit 1; }\nk6 new --template ./my.tmpl script.js","typeGuard":"// Only .ScriptName and .ProjectID are valid fields in TemplateArgs\nconst allowedFields = [\"ScriptName\", \"ProjectID\"];\nfunction templateFieldsLookValid(tplText) {\n  return ![...tplText.matchAll(/{{\\s*\\.([A-Za-z]+)\\s*}}/g)]\n    .some(m => !allowedFields.includes(m[1]));\n}","tryCatchPattern":null,"preventionTips":["Limit custom templates to the documented fields (.ScriptName, .ProjectID) and standard functions","Keep a rendered golden-file test for custom templates in the repo","Re-test templates after k6 upgrades — TemplateArgs surface can change"],"tags":["cli","templates","text-template","scaffolding"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}