grafana/k6 · error

failed to parse template file

Error message

failed to parse template file %s: %w

What it means

The custom template file was read successfully but does not parse as a Go text/template (k6 new templates use {{.Field}} actions such as {{.Name}}). Syntax errors — an unclosed {{, mismatched actions, unknown functions or pipelines — are wrapped with the offending file's path. Parse happens before any output is generated, so nothing is written on failure.

Solutions

  1. Fix the template syntax: every {{ must have a matching }}, and actions must be valid text/template expressions
  2. For literal braces in output, escape them: {{"{{"}} and {{"}}"}}
  3. Start from a working base: run `k6 new x.js` with a built-in template, copy the generated file, edit minimally, then pass -t ./copy

Example fix

// before (my.tpl): unclosed action + literal braces
export const opts = {{ vus: 1 };

// after: no template action, plain literal braces
export const opts = { vus: 1 };
Defensive patterns

Strategy: validation

Validate before calling

// parse-check the template with Go before using it (exit non-zero on bad syntax)
package main

import (
  "fmt"
  "os"
  "text/template"
)

func main() {
  b, err := os.ReadFile(os.Args[1])
  if err != nil { fmt.Println(err); os.Exit(1) }
  if _, err := template.New("t").Parse(string(b)); err != nil { fmt.Println(err); os.Exit(1) }
}

Prevention

When it happens

Trigger: k6 new -t ./my.tpl where my.tpl contains '{{' with no closing braces, '{{.Name | nosuchfunc}}', a stray '{{end}}', or JavaScript template literals that accidentally open a {{ action.

Common situations: Hand-editing a copied built-in template; JS code containing '{{' sequences (e.g. raw template strings) that Go's parser tries to interpret; merging template fragments from different sources.

Understand the failure class

Related errors


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

Appendix: source

Thrown at internal/cmd/templates/templates.go:92

		return tm.browserTemplate, nil
	}

	// Then check if it's a file path
	if isFilePath(tpl) {
		tplPath, err := filepath.Abs(tpl)
		if err != nil {
			return nil, fmt.Errorf("failed to get absolute path for template %s: %w", tpl, err)
		}

		// Read the template content using the provided filesystem
		content, err := fsext.ReadFile(tm.fs, tplPath)
		if err != nil {
			return nil, fmt.Errorf("failed to read template file %s: %w", tpl, err)
		}

		tmpl, err := template.New(filepath.Base(tplPath)).Parse(string(content))
		if err != nil {
			return nil, fmt.Errorf("failed to parse template file %s: %w", tpl, err)
		}

		return tmpl, nil
	}

	// Check if there's a file with this name in current directory
	exists, err := fsext.Exists(tm.fs, fsext.JoinFilePath(".", tpl))
	if err == nil && exists {
		return nil, fmt.Errorf("invalid template type %q, did you mean ./%s?", tpl, tpl)
	}

	return nil, fmt.Errorf("invalid template type %q", tpl)
}

// isFilePath checks if the given string looks like a file path by detecting path separators
// We assume that built-in template names don't contain path separators
func isFilePath(path string) bool {
	return strings.ContainsRune(path, filepath.Separator) || strings.ContainsRune(path, '/')

View on GitHub (pinned to 01ffac6f24)