{"record":{"id":"0af339886aebcbad","repo":"grafana/k6","slug":"error-retrieving-template-w","errorCode":null,"errorMessage":"error retrieving template: %w","messagePattern":"error retrieving template: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/new.go","lineNumber":55,"sourceCode":"\t}\n\n\tfileExists, err := fsext.Exists(c.gs.FS, target)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif fileExists && !c.overwriteFiles {\n\t\treturn fmt.Errorf(\"%s already exists. Use the `--force` flag to overwrite it\", target)\n\t}\n\n\t// Initialize template manager and validate template before creating any files\n\ttm, err := templates.NewTemplateManager(c.gs.FS)\n\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","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/new.go#L37-L73","documentation":"Returned by `k6 new` when TemplateManager.GetTemplate(c.templateType) cannot resolve the --template value. Valid values are the built-in choices minimal, protocol, browser, or a relative/absolute path to a custom template file; anything else — a typo or a path that does not exist/cannot be read — fails here, before any file is created.","triggerScenarios":"`k6 new --template minimal2 script.js` (typo), `k6 new --template ./templates/missing.tmpl s.js` (nonexistent custom path), or a custom path with unreadable permissions; the file-existence and template-manager checks have already passed by this point.","commonSituations":"Switching template names between k6 versions (a choice removed/renamed); pointing --template at a repo-relative path from the wrong working directory; typos in CI scaffolding jobs.","solutions":["Use one of the advertised choices: `--template minimal`, `--template protocol`, or `--template browser`","For custom templates, pass a valid relative/absolute path that exists and is readable from the current working directory","Run `k6 new --help` to re-read the exact choices supported by your binary version"],"exampleFix":"# before\n$ k6 new --template minmal script.js\n# error: error retrieving template: ...\n\n# after\n$ k6 new --template minimal script.js","handlingStrategy":"type-guard","validationCode":"#!/usr/bin/env bash\nbuiltin_templates=(minimal protocol browser)\ntpl=\"${TPL:-minimal}\"\nif [[ ! \" ${builtin_templates[*]} \" == *\" $tpl \"* && ! -f \"$tpl\" && ! -f \"$PWD/$tpl\" ]]; then\n  echo \"--template must be one of: ${builtin_templates[*]} or an existing template file path\"; exit 1\nfi\nk6 new --template \"$tpl\" script.js","typeGuard":"function isValidTemplateChoice(v, cwd) {\n  return [\"minimal\", \"protocol\", \"browser\"].includes(v) || fs.existsSync(path.resolve(cwd, v));\n}","tryCatchPattern":null,"preventionTips":["Copy template choices verbatim from `k6 new --help`","For custom templates, prefer absolute paths in CI to avoid cwd drift","Add the template file to the repo and check it exists in the pipeline before k6 new"],"tags":["cli","templates","scaffolding","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}