{"record":{"id":"4de6d35577c48ecb","repo":"grafana/k6","slug":"failed-to-get-absolute-path-for-template-s-w","errorCode":null,"errorMessage":"failed to get absolute path for template %s: %w","messagePattern":"failed to get absolute path for template (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/templates/templates.go","lineNumber":81,"sourceCode":"}\n\n// GetTemplate selects the appropriate template based on the type\nfunc (tm *TemplateManager) GetTemplate(tpl string) (*template.Template, error) {\n\t// First check built-in templates\n\tswitch tpl {\n\tcase MinimalTemplate:\n\t\treturn tm.minimalTemplate, nil\n\tcase ProtocolTemplate:\n\t\treturn tm.protocolTemplate, nil\n\tcase BrowserTemplate:\n\t\treturn tm.browserTemplate, nil\n\t}\n\n\t// Then check if it's a file path\n\tif isFilePath(tpl) {\n\t\ttplPath, err := filepath.Abs(tpl)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to get absolute path for template %s: %w\", tpl, err)\n\t\t}\n\n\t\t// Read the template content using the provided filesystem\n\t\tcontent, err := fsext.ReadFile(tm.fs, tplPath)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to read template file %s: %w\", tpl, err)\n\t\t}\n\n\t\ttmpl, err := template.New(filepath.Base(tplPath)).Parse(string(content))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse template file %s: %w\", tpl, err)\n\t\t}\n\n\t\treturn tmpl, nil\n\t}\n\n\t// Check if there's a file with this name in current directory\n\texists, err := fsext.Exists(tm.fs, fsext.JoinFilePath(\".\", tpl))","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/templates/templates.go#L63-L99","documentation":"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>\".","triggerScenarios":"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.","commonSituations":"CI jobs where the workspace directory is removed or replaced concurrently; shells whose cwd was deleted before invoking k6 new.","solutions":["cd into an existing directory (verify with pwd) and re-run","Pass an absolute path for the template so Abs has nothing to resolve","Fix the surrounding script/cleanup logic that deletes the working directory mid-run"],"exampleFix":"# before (cwd was deleted)\nk6 new --template=./mytpl.js script.js\n# after\ncd \"$HOME\" && k6 new --template=/abs/path/mytpl.js script.js","handlingStrategy":"validation","validationCode":"# Ensure the cwd is resolvable and pass an absolute template path\npwd >/dev/null 2>&1 || { echo 'cwd unavailable — cd to an existing directory first' >&2; exit 2; }\nk6 new --template=\"$(pwd)/tpl/mytpl.js\" script.js","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass absolute template paths in automation","Never delete the workspace directory a concurrently running k6 uses","cd to a stable directory before invoking k6 new"],"tags":["cli","templates","filesystem"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}