{"record":{"id":"a5c33b0a96124cf7","repo":"grafana/k6","slug":"failed-to-encode-output-version-details-w","errorCode":null,"errorMessage":"failed to encode/output version details: %w","messagePattern":"failed to encode/output version details: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/version.go","lineNumber":233,"sourceCode":"\tgs     *state.GlobalState\n\tisJSON bool\n}\n\nfunc (c *versionCmd) run(cmd *cobra.Command, _ []string) error {\n\tif !c.isJSON {\n\t\troot := cmd.Root()\n\t\troot.SetArgs([]string{\"--version\"})\n\t\t_ = root.Execute()\n\t\treturn nil\n\t}\n\n\tdetails, err := versionDetailsWithExtensions(ext.GetAll())\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get version details with extensions: %w\", err)\n\t}\n\n\tif err := json.NewEncoder(c.gs.Stdout).Encode(details); err != nil {\n\t\treturn fmt.Errorf(\"failed to encode/output version details: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc getCmdVersion(gs *state.GlobalState) *cobra.Command {\n\tversionCmd := &versionCmd{gs: gs}\n\n\tcmd := &cobra.Command{\n\t\tUse:    \"version\",\n\t\tShort:  \"Show application version\",\n\t\tLong:   `Show the application version and exit.`,\n\t\tHidden: true,\n\t\tRunE:   versionCmd.run,\n\t}\n\n\tcmd.Flags().BoolVar(&versionCmd.isJSON, \"json\", false, \"if set, output version information will be in JSON format\")\n","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/version.go#L215-L251","documentation":"The JSON branch of the version command encodes the collected details directly to stdout with json.NewEncoder(c.gs.Stdout).Encode. The data is always JSON-encodable, so failure means the write to stdout failed — broken pipe, closed file descriptor, or terminal I/O error — wrapped as \"failed to encode/output version details\".","triggerScenarios":"'k6 version --json | head -1' where the reader exits before k6 finishes writing; stdout redirected to a closed descriptor; running in a sandbox or GUI context where stdout is not a valid writable stream.","commonSituations":"Piping version JSON to short-lived readers; scripting environments that revoke stdout; processes spawned without a valid standard output.","solutions":["Redirect to a file: k6 version --json > version.json","Ensure the reader of the pipe consumes all output before exiting","Run the command in a normal terminal to confirm the binary itself is fine"],"exampleFix":"# before\nk6 version --json | jq .version   # reader may close the pipe early\n# after\nk6 version --json > version.json && jq .version version.json","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"# Capture to a file so a closed pipe cannot truncate the write\nk6 version --json > version.json 2>err.log || { echo 'stdout write failed:'; cat err.log; k6 version; }","preventionTips":["Redirect version JSON to a file rather than into short-lived pipes","Consume the full stream before exiting the reader process","Verify stdout is a valid descriptor in headless or sandboxed runners"],"tags":["json","io","version","broken-pipe"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}