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
- Run k6 from an existing directory you can read: cd to a stable path and retry
- Pass an absolute template path so resolution depends less on cwd
- 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
- Run k6 from stable, long-lived directories; avoid temp dirs that cleanup jobs may delete
- Pass absolute -t paths to reduce dependence on cwd resolution
- If this fires, treat it as an environment smell — the wrapped OS error says exactly what failed
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
- failed to read template file
- invalid template type
- failed to parse template file
- local or remote path required
- persisting screenshot
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)