golang/go · warning

copying diff output: %w

Error message

copying diff output: %w

What it means

copyAndDetectDiff failed while streaming the tool's stdout to os.Stdout via io.Copy. The %w wraps the underlying I/O error (e.g. EPIPE). Fires only in -diff mode.

Source

Thrown at src/cmd/go/internal/vet/vet.go:362

		if prev, ok := out[f.Name]; ok && !bytes.Equal(prev, content) {
			return fmt.Errorf("inconsistent fixes to file %v", f.Name)
		}
		out[f.Name] = content
	}
	return nil
}

// copyAndDetectDiff copies the tool's stdout to the go command's stdout
// and sets exit status 1 if any output was produced (meaning diffs exist).
// This is used in -diff mode to implement the convention that "go fix -diff"
// exits non-zero when the diff is not empty, consistent with gofmt -d
// and go mod tidy -diff.
func copyAndDetectDiff(r io.Reader) error {
	stdouterrMu.Lock()
	defer stdouterrMu.Unlock()
	n, err := io.Copy(os.Stdout, r)
	if err != nil {
		return fmt.Errorf("copying diff output: %w", err)
	}
	if n > 0 {
		base.SetExitStatus(1)
	}
	return nil
}

// printJSONDiagnostics parses JSON (from the tool's stdout) and
// prints it (to stderr) in "file:line: message" form.
// It also ensures that we exit nonzero if there were diagnostics.
func printJSONDiagnostics(r io.Reader) error {
	stdout, err := io.ReadAll(r)
	if err != nil {
		return err
	}
	if len(stdout) > 0 {
		// unitchecker emits a JSON map of the form:
		// output maps Package ID -> Analyzer.Name -> (error | []Diagnostic);

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Ensure the downstream consumer of stdout reads to EOF
  2. Avoid piping into head/tail; redirect to a file instead (go vet -diff > out.txt)
Defensive patterns

Strategy: fallback

Try / catch

// Tolerate SIGPIPE when streaming vet diff output
// (run with stdout to a file or a reader that drains to EOF)
go vet -diff ./... > vet.diff 2>vet.err

Prevention

When it happens

Trigger: The downstream consumer of `go vet -diff` stdout closes early, producing a broken pipe on write.

Common situations: Piping `go vet -diff` into head/tail/more that exits before EOF; terminal disconnect; stdout redirected to a full/truncated file.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/ec3a02442322c61a. Report an issue: GitHub.