grafana/k6 · error

failed to get absolute path for template

Error message

failed to get absolute path for template %s: %w

What it means

For `k6 new -t <path>`, values containing a path separator are resolved to an absolute path with filepath.Abs before being read. Abs essentially never fails unless the OS cannot determine the current working directory (deleted cwd, permission trouble), so this error is environmental; the wrapped %w carries the underlying OS error.

Solutions

  1. Run k6 from an existing directory you can read: cd to a stable path and retry
  2. Pass an absolute template path so resolution depends less on cwd
  3. Fix the environment: recreate the deleted directory or restart the shell/container — the wrapped error names the OS cause
Defensive patterns

Strategy: validation

Validate before calling

# ensure the working directory still resolves before running k6
if ! pwd -P >/dev/null 2>&1; then
  echo "current directory is gone — switching to a stable one" >&2
  cd /tmp || exit 1
fi
k6 new -t "$HOME/templates/my.js" new-script.js

Prevention

When it happens

Trigger: Running k6 from a directory that was deleted out from under the process (classic getcwd ENOENT); unusual container/sandbox setups where the cwd is inaccessible.

Common situations: Shell scripts that cd into temp dirs which a parallel cleanup removes; container overlays where the workdir is pruned mid-session.

Related errors


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

Appendix: source

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

}

// GetTemplate selects the appropriate template based on the type
func (tm *TemplateManager) GetTemplate(tpl string) (*template.Template, error) {
	// First check built-in templates
	switch tpl {
	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))

View on GitHub (pinned to 01ffac6f24)