grafana/k6 · error

failed to execute template %s: %w

Error message

failed to execute template %s: %w

What it means

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.

Source

Thrown at internal/cmd/new.go:67

	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
	}

	defer func() {
		if cerr := fd.Close(); cerr != nil {
			if _, werr := fmt.Fprintf(c.gs.Stderr, "error closing file: %v\n", cerr); werr != nil {
				err = fmt.Errorf("error writing error message to stderr: %w", werr)
			} else {
				err = cerr
			}
		}
	}()

View on GitHub (pinned to 93accf6570)

Solutions

  1. Open the custom template file and check every {{ ... }} action — balanced braces, defined fields (.ScriptName, .ProjectID), only standard template functions
  2. 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
  3. Fall back to a built-in template (`--template minimal`) to confirm the rest of the command works
  4. Fix the template, then re-run `k6 new --template ./fixed.tmpl script.js`

Example fix

# before (custom.tmpl)
import http from 'k6/http';
{{ .ScriptNames }}  # typo: no such field

# after
import http from 'k6/http';
// generated for {{ .ScriptName }}
Defensive patterns

Strategy: validation

Validate before calling

# Render custom template standalone with the same args k6 uses; fail before k6 new
go run ./tools/rendercheck ./my.tmpl '{"ScriptName":"s","ProjectID":""}' >/dev/null \
  || { echo 'template does not render'; exit 1; }
k6 new --template ./my.tmpl script.js

Type guard

// Only .ScriptName and .ProjectID are valid fields in TemplateArgs
const allowedFields = ["ScriptName", "ProjectID"];
function templateFieldsLookValid(tplText) {
  return ![...tplText.matchAll(/{{\s*\.([A-Za-z]+)\s*}}/g)]
    .some(m => !allowedFields.includes(m[1]));
}

Prevention

When it happens

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

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

Related errors


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