grafana/k6 · error

failed to execute template

Error message

failed to execute template %s: %w

What it means

Wrapped error in 'k6 new': rendering the selected template to a buffer failed (templates.ExecuteTemplate). Usually a malformed template file (custom path) or missing expected fields — k6 renders before writing to avoid leaving broken files.

Solutions

  1. Validate the custom template's Go template syntax
  2. Use a built-in template name if the custom one fails
  3. Check the wrapped error for the exact template parse/execute failure
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/cmd/new.go:67 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: 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 01ffac6f24)