{"record":{"id":"a93de64debab9d69","repo":"ahmetb/kubectx","slug":"write-error-w-a93de6","errorCode":null,"errorMessage":"write error: %w","messagePattern":"write error: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"cmd/kubens/version.go","lineNumber":18,"sourceCode":"package main\n\nimport (\n\t\"fmt\"\n\t\"io\"\n)\n\nvar (\n\tversion = \"v0.0.0+unknown\" // populated by goreleaser\n)\n\n// VersionOp describes printing version string.\ntype VersionOp struct{}\n\nfunc (_ VersionOp) Run(stdout, _ io.Writer) error {\n\t_, err := fmt.Fprintf(stdout, \"%s\\n\", version)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write error: %w\", err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubens/version.go#L1-L22","documentation":"VersionOp.Run wraps an error from fmt.Fprintf(stdout, ...) as \"write error: %w\". It fires when writing the version string to the provided stdout writer fails, typically because the underlying pipe or descriptor is closed or broken.","triggerScenarios":"The io.Writer passed to VersionOp.Run returns an error on write: closed stdout pipe (e.g. `kubens version | head -0`), broken pipe when the downstream consumer exits early, or a custom in-memory writer that errors.","commonSituations":"Piping kubens version into a command that exits immediately (broken pipe); writing to a closed network/socket-backed writer; tests using a failing writer implementation.","solutions":["Ensure the downstream consumer of the pipe stays open until the command completes.","Redirect to a file instead of a short-lived pipe: kubens version > version.txt.","If invoking programmatically, pass a healthy io.Writer (os.Stdout or a working buffer).","Ignore SIGPIPE-related failures if truncation by `head` is intentional."],"exampleFix":"// before\nkubens version | head -n 0\n// after\nversion=$(kubens version) && echo \"$version\" | head -n 1","handlingStrategy":"fallback","validationCode":"// Go: confirm the writer is usable in tests before invoking Run\nvar buf bytes.Buffer\nif err := (&VersionOp{}).Run(&buf, io.Discard); err != nil {\n\tt.Fatalf(\"version write failed: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := versionOp.Run(stdout, stderr); err != nil {\n\tif errors.Is(err, syscall.EPIPE) {\n\t\treturn nil // downstream closed the pipe (e.g. `| head`); safe to ignore\n\t}\n\treturn err\n}","preventionTips":["Avoid piping version output into commands that exit immediately.","Capture output with command substitution or files instead of fragile pipes.","In tests, use bytes.Buffer writers rather than closed pipes.","Set SIGPIPE handling explicitly in Go programs writing to stdout pipes."],"tags":["io","broken-pipe","stdout"],"backgroundTag":"broken-pipe-write-failure","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}