grafana/k6 · error

error retrieving template: %w

Error message

error retrieving template: %w

What it means

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.

Source

Thrown at internal/cmd/new.go:55

	}

	fileExists, err := fsext.Exists(c.gs.FS, target)
	if err != nil {
		return err
	}
	if fileExists && !c.overwriteFiles {
		return fmt.Errorf("%s already exists. Use the `--force` flag to overwrite it", target)
	}

	// Initialize template manager and validate template before creating any files
	tm, err := templates.NewTemplateManager(c.gs.FS)
	if err != nil {
		return fmt.Errorf("error initializing template manager: %w", err)
	}

	tmpl, err := tm.GetTemplate(c.templateType)
	if err != nil {
		return fmt.Errorf("error retrieving template: %w", err)
	}

	// Prepare template arguments
	argsStruct := templates.TemplateArgs{
		ScriptName: target,
		ProjectID:  c.projectID,
	}

	// First render the template to a buffer to validate it
	var buf strings.Builder
	if err := templates.ExecuteTemplate(&buf, tmpl, argsStruct); err != nil {
		return fmt.Errorf("failed to execute template %s: %w", c.templateType, err)
	}

	// Only create the file after template rendering succeeds
	fd, err := c.gs.FS.Create(target)
	if err != nil {
		return err

View on GitHub (pinned to 93accf6570)

Solutions

  1. Use one of the advertised choices: `--template minimal`, `--template protocol`, or `--template browser`
  2. For custom templates, pass a valid relative/absolute path that exists and is readable from the current working directory
  3. Run `k6 new --help` to re-read the exact choices supported by your binary version

Example fix

# before
$ k6 new --template minmal script.js
# error: error retrieving template: ...

# after
$ k6 new --template minimal script.js
Defensive patterns

Strategy: type-guard

Validate before calling

#!/usr/bin/env bash
builtin_templates=(minimal protocol browser)
tpl="${TPL:-minimal}"
if [[ ! " ${builtin_templates[*]} " == *" $tpl "* && ! -f "$tpl" && ! -f "$PWD/$tpl" ]]; then
  echo "--template must be one of: ${builtin_templates[*]} or an existing template file path"; exit 1
fi
k6 new --template "$tpl" script.js

Type guard

function isValidTemplateChoice(v, cwd) {
  return ["minimal", "protocol", "browser"].includes(v) || fs.existsSync(path.resolve(cwd, v));
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/0af339886aebcbad. Report an issue: GitHub.