{"record":{"id":"86b5dbe47f5fdb26","repo":"ahmetb/kubectx","slug":"write-error-w-86b5db","errorCode":null,"errorMessage":"write error: %w","messagePattern":"write error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kubens/current.go","lineNumber":47,"sourceCode":"\tdefer kc.Close()\n\tif err := kc.Parse(); err != nil {\n\t\treturn fmt.Errorf(\"kubeconfig error: %w\", err)\n\t}\n\n\tctx, err := kc.GetCurrentContext()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get current context: %w\", err)\n\t}\n\tif ctx == \"\" {\n\t\treturn errors.New(\"current-context is not set\")\n\t}\n\tns, err := kc.NamespaceOfContext(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to read namespace of \\\"%s\\\": %w\", ctx, err)\n\t}\n\t_, err = fmt.Fprintln(stdout, ns)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"write error: %w\", err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":29,"sourceCodeEnd":51,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/cmd/kubens/current.go#L29-L51","documentation":"After resolving the namespace, CurrentOp.Run prints it to stdout with fmt.Fprintln. If that write fails, the error is wrapped as \"write error: %w\". Like the kubectx version write error, this signals the output stream rejected the data rather than any kubeconfig problem.","triggerScenarios":"fmt.Fprintln(stdout, ns) fails because stdout is a broken/closed pipe, a full disk, or an otherwise invalid output stream.","commonSituations":"`kubens -c | head -0` style pipelines that close the reader early; CI runners with detached stdout; disk-full conditions when redirecting to a file.","solutions":["Re-run with stdout attached to a terminal to confirm the command itself works","Inspect the wrapped error for EPIPE (fix the pipe consumer) vs ENOSPC (free disk space)","Avoid piping into commands that exit immediately; use a file or tee instead","In scripts, check exit codes and stderr rather than swallowing the failure"],"exampleFix":"// before\nkubens -c | head -0   # broken pipe\n// after\nkubens -c > current-ns.txt","handlingStrategy":"try-catch","validationCode":"if f, ok := stdout.(*os.File); ok {\n    if _, err := f.Stat(); err != nil {\n        return fmt.Errorf(\"stdout unusable: %w\", err)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := op.Run(stdout, stderr); err != nil {\n    if strings.Contains(err.Error(), \"write error:\") {\n        if errors.Is(err, syscall.EPIPE) {\n            return nil // consumer closed the pipe\n        }\n    }\n    return err\n}","preventionTips":["Don't pipe into commands that exit early (head -0, true)","Redirect to a file or terminal for scripted checks","Monitor disk space on CI runners","Handle EPIPE specially in shell wrappers"],"tags":["go","io","broken-pipe"],"backgroundTag":"write-failed-broken-pipe","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}