ahmetb/kubectx · error

write error: %w

Error message

write error: %w

What it means

VersionOp.Run prints the build version string to stdout via fmt.Fprintf. If the underlying write to the output stream fails, the error is wrapped as "write error: %w" and returned to the caller. This indicates the destination stream (e.g. a closed or broken pipe, full disk, or redirected file) could not accept the output.

Source

Thrown at cmd/kubectx/version.go:18

package main

import (
	"fmt"
	"io"
)

var (
	version = "v0.0.0+unknown" // populated by goreleaser
)

// VersionOp describes printing version string.
type VersionOp struct{}

func (_ VersionOp) Run(stdout, _ io.Writer) error {
	_, err := fmt.Fprintf(stdout, "%s\n", version)
	if err != nil {
		return fmt.Errorf("write error: %w", err)
	}
	return nil
}

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Re-run the command with stdout connected to a terminal or a valid file to confirm the stream works
  2. Check for a full disk (df -h) or permission issues on the redirect target
  3. Update kubectx if your shell/pipe setup kills the process before the write completes
  4. If writing Go, check the wrapped error (%w) for EPIPE vs EACCES to determine the real cause

Example fix

// before
kubectx version | head -0   // closes pipe early -> write error
// after
kubectx version             // write to a healthy stream
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: check the output stream before running
if f, ok := stdout.(*os.File); ok {
    if _, err := f.Stat(); err != nil {
        return fmt.Errorf("stdout unusable: %w", err)
    }
}

Try / catch

if err := VersionOp{}.Run(stdout, stderr); err != nil {
    var werr *os.PathError
    if errors.As(err, &werr) && errors.Is(werr.Err, syscall.EPIPE) {
        // broken pipe: consumer closed early, treat as non-fatal
        return nil
    }
    return err
}

Prevention

When it happens

Trigger: Calling kubectx version (or VersionOp.Run) with stdout pointing to a stream that errors on write, such as a closed pipe (e.g. `kubectx version | head -0`), a full disk, or an invalid file descriptor.

Common situations: Piping kubectx output into a command that exits early and closes the pipe (SIGPIPE/EPIPE); writing to a redirect target on a full filesystem; CI logs with a broken output stream.

Related errors


AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02). Data as JSON: /api/errors/ecdf624945b14b83. Report an issue: GitHub.