{"record":{"id":"bc49c00622fd7d57","repo":"grafana/k6","slug":"error-writing-error-message-to-stderr-w","errorCode":null,"errorMessage":"error writing error message to stderr: %w","messagePattern":"error writing error message to stderr: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/new.go","lineNumber":79,"sourceCode":"\t\tProjectID:  c.projectID,\n\t}\n\n\t// First render the template to a buffer to validate it\n\tvar buf strings.Builder\n\tif err := templates.ExecuteTemplate(&buf, tmpl, argsStruct); err != nil {\n\t\treturn fmt.Errorf(\"failed to execute template %s: %w\", c.templateType, err)\n\t}\n\n\t// Only create the file after template rendering succeeds\n\tfd, err := c.gs.FS.Create(target)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tdefer func() {\n\t\tif cerr := fd.Close(); cerr != nil {\n\t\t\tif _, werr := fmt.Fprintf(c.gs.Stderr, \"error closing file: %v\\n\", cerr); werr != nil {\n\t\t\t\terr = fmt.Errorf(\"error writing error message to stderr: %w\", werr)\n\t\t\t} else {\n\t\t\t\terr = cerr\n\t\t\t}\n\t\t}\n\t}()\n\n\t// Write the rendered content to the file\n\tif _, err := io.WriteString(fd, buf.String()); err != nil {\n\t\treturn err\n\t}\n\n\tif _, err := fmt.Fprintf(c.gs.Stdout, \"New script created: %s (%s template).\\n\", target, c.templateType); err != nil {\n\t\treturn err\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/new.go#L61-L97","documentation":"The rarest error in `k6 new`: inside the deferred close of the newly created script file, the file failed to close AND writing the close-error message to stderr also failed, so the original close error is replaced by this stderr-write failure. It signals a deeply broken I/O environment (both the target file descriptor and stderr unusable), not a template or usage problem.","triggerScenarios":"stderr is closed or points to an exhausted descriptor/pipe (e.g. `k6 new s.js 2>&-`, or a parent process that closed its read end of the stderr pipe) while the created file also fails to close (fd pressure, NFS/overlayfs issues, disk detachment).","commonSituations":"Malformed process supervision setups that close stderr; sandboxed containers with fd limits (ulimit -n) exhausted; running under wrappers that pipe stderr into a process that exits early.","solutions":["Restore a working stderr (don't run with 2>&-; ensure the consuming pipe stays open) and re-run","Check fd limits and I/O health: `ulimit -n`, disk space, and mount status of the target directory","Verify the target file's state afterwards (size, readability) — with both errors the file may be incompletely closed but its content was already written via io.WriteString","If it recurs only under a specific supervisor/wrapper, run k6 directly once to isolate the environment issue"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"# Ensure stderr is a writable fd before running k6 (guard the double-failure path)\nif ! : 2>/dev/null; then echo 'stderr closed'; exit 1; fi\nulimit -n 1024 >/dev/null 2>&1 || true   # sane fd ceiling for wrapper processes\nk6 new script.js","typeGuard":null,"tryCatchPattern":"#k6 new script.js || {\n#  echo 'k6 new failed; check stderr pipe and fd limits' >&2\n#  exit 1\n#}","preventionTips":["Never invoke k6 with stderr closed (2>&-) in supervisors or scripts","Keep stderr's consumer (pipe/file/log) alive for the whole k6 run","Raise fd limits in container environments prone to exhaustion"],"tags":["filesystem","stderr","io","edge-case"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}