ahmetb/kubectx · warning

write error: %w

Error message

write error: %w

What it means

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.

Source

Thrown at cmd/kubens/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. Ensure the downstream consumer of the pipe stays open until the command completes.
  2. Redirect to a file instead of a short-lived pipe: kubens version > version.txt.
  3. If invoking programmatically, pass a healthy io.Writer (os.Stdout or a working buffer).
  4. Ignore SIGPIPE-related failures if truncation by `head` is intentional.

Example fix

// before
kubens version | head -n 0
// after
version=$(kubens version) && echo "$version" | head -n 1
Defensive patterns

Strategy: fallback

Validate before calling

// Go: confirm the writer is usable in tests before invoking Run
var buf bytes.Buffer
if err := (&VersionOp{}).Run(&buf, io.Discard); err != nil {
	t.Fatalf("version write failed: %v", err)
}

Try / catch

if err := versionOp.Run(stdout, stderr); err != nil {
	if errors.Is(err, syscall.EPIPE) {
		return nil // downstream closed the pipe (e.g. `| head`); safe to ignore
	}
	return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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