grafana/k6 · error

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

Error message

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

What it means

When 'k6 new --template' receives a value containing a path separator, k6 treats it as a file path and first resolves it with filepath.Abs. That call essentially only fails when the current working directory cannot be determined (deleted cwd, permission error on Getwd), and the error is wrapped as "failed to get absolute path for template <tpl>".

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

Solutions

  1. cd into an existing directory (verify with pwd) and re-run
  2. Pass an absolute path for the template so Abs has nothing to resolve
  3. Fix the surrounding script/cleanup logic that deletes the working directory mid-run

Example fix

# before (cwd was deleted)
k6 new --template=./mytpl.js script.js
# after
cd "$HOME" && k6 new --template=/abs/path/mytpl.js script.js
Defensive patterns

Strategy: validation

Validate before calling

# Ensure the cwd is resolvable and pass an absolute template path
pwd >/dev/null 2>&1 || { echo 'cwd unavailable — cd to an existing directory first' >&2; exit 2; }
k6 new --template="$(pwd)/tpl/mytpl.js" script.js

Prevention

When it happens

Trigger: Running 'k6 new --template=./tpl.js script.js' from a directory that was deleted (e.g. a CI step that rm -rf's the workspace while k6 starts); running on filesystems where getting the cwd fails.

Common situations: CI jobs where the workspace directory is removed or replaced concurrently; shells whose cwd was deleted before invoking k6 new.

Related errors


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