grafana/k6 · error

failed to read template file

Error message

failed to read template file %s: %w

What it means

After resolving a custom `k6 new -t <path>` template to an absolute path, k6 reads it through fsext.ReadFile; any read failure (file not found, permission denied, path is a directory) is wrapped as 'failed to read template file <path>'. This is the most common custom-template error — the path resolved, but could not be read.

Solutions

  1. Verify the file exists and is readable: ls -l ./templates/my.js, and check you are in the directory you think
  2. Use an absolute path: k6 new -t /home/me/k6-templates/my.js
  3. If you meant a built-in template, use -t minimal, -t protocol, or -t browser (no path separators)

Example fix

# before (file actually lives in ./templates/, not ./template/)
k6 new -t template/my.js new-script.js

# after
k6 new -t ./templates/my.js new-script.js
Defensive patterns

Strategy: validation

Validate before calling

# verify the template file is readable before calling k6 new
tpl="$1"
if ! [ -f "$tpl" ]; then echo "template not found: $tpl" >&2; exit 1; fi
if ! [ -r "$tpl" ]; then echo "template not readable: $tpl" >&2; exit 1; fi
k6 new -t "$tpl" new-script.js

Prevention

When it happens

Trigger: k6 new -t ./templates/my.js when the file does not exist (note: k6 resolves relative to cwd, so the typo'd directory silently produces this); file exists but is not readable by the k6 user; path points at a directory (paths with separators skip the built-in name checks).

Common situations: Wrong relative path from a different working directory; templates committed in another repo/location; permission-restricted mounts in CI; assuming -t accepts URLs (it does not — only local paths).

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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

Appendix: source

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

	case MinimalTemplate:
		return tm.minimalTemplate, nil
	case ProtocolTemplate:
		return tm.protocolTemplate, nil
	case BrowserTemplate:
		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)
}

View on GitHub (pinned to 01ffac6f24)