grafana/k6 · error

error initializing template manager: %w

Error message

error initializing template manager: %w

What it means

Returned by `k6 new` when templates.NewTemplateManager(c.gs.FS) fails to initialize. The manager parses the embedded template registry at construction; failure means the embedded templates could not be prepared — effectively a broken/custom k6 build rather than anything the user supplied (note it runs before any template is selected or any file is created).

Source

Thrown at internal/cmd/new.go:50

func (c *newScriptCmd) run(_ *cobra.Command, args []string) (err error) {
	target := defaultNewScriptName
	if len(args) > 0 {
		target = args[0]
	}

	fileExists, err := fsext.Exists(c.gs.FS, target)
	if err != nil {
		return err
	}
	if fileExists && !c.overwriteFiles {
		return fmt.Errorf("%s already exists. Use the `--force` flag to overwrite it", target)
	}

	// Initialize template manager and validate template before creating any files
	tm, err := templates.NewTemplateManager(c.gs.FS)
	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)
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Re-download/reinstall the official k6 binary for your platform and verify with `k6 version`
  2. If this is a custom xk6 build, rebuild from a clean checkout (make build) — the error is inside template-manager initialization, not your invocation
  3. File an issue upstream with `k6 version` output if a stock binary reproduces it
Defensive patterns

Strategy: validation

Validate before calling

# Smoke the binary's scaffolding path once during provisioning
k6 version >/dev/null && k6 new --force /tmp/_smoke.js >/dev/null || { echo 'k6 new broken on this build'; exit 1; }
rm -f /tmp/_smoke.js

Prevention

When it happens

Trigger: Running `k6 new` with a corrupted or non-stock k6 binary whose embedded template assets fail to parse; custom xk6 builds that modified the templates package; filesystem-level failures of the injected afero FS in tests/embedded scenarios.

Common situations: Essentially unseen with official binaries; surfaces in development forks after editing internal/templates, or when a partially downloaded binary is executed.

Related errors


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