grafana/k6 · error

failed to read template file %s: %w

Error message

failed to read template file %s: %w

What it means

After resolving a --template value to an absolute path, k6 new reads the file through its filesystem abstraction (fsext.ReadFile). If the file does not exist or is not readable, the error is wrapped as "failed to read template file <tpl>".

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 93accf6570)

Solutions

  1. Verify the path exists and is readable: ls -l <path>
  2. Prefix local files with ./ or use an absolute path so k6 treats the value as a file path rather than a template name
  3. Copy the template into the container/workspace before running k6 new

Example fix

# before
k6 new --template=mytpl.js script.js   # file missing
# after
k6 new --template=./mytpl.js script.js # and ensure ./mytpl.js exists
Defensive patterns

Strategy: validation

Validate before calling

tpl=./templates/grpc.js
[ -f "$tpl" ] && [ -r "$tpl" ] || { echo "template missing or unreadable: $tpl" >&2; exit 2; }
k6 new --template="$tpl" script.js

Prevention

When it happens

Trigger: k6 new --template=./templates/grpc.js script.js where the file is missing or lacks read permission; a path with a wrong directory component; template files not present in a container image.

Common situations: Typoed template paths; templates moved between branches; restrictive container filesystems where the template file was never copied in.

Related errors


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