{"record":{"id":"ec3a02442322c61a","repo":"golang/go","slug":"copying-diff-output-w","errorCode":null,"errorMessage":"copying diff output: %w","messagePattern":"copying diff output: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/cmd/go/internal/vet/vet.go","lineNumber":362,"sourceCode":"\t\tif prev, ok := out[f.Name]; ok && !bytes.Equal(prev, content) {\n\t\t\treturn fmt.Errorf(\"inconsistent fixes to file %v\", f.Name)\n\t\t}\n\t\tout[f.Name] = content\n\t}\n\treturn nil\n}\n\n// copyAndDetectDiff copies the tool's stdout to the go command's stdout\n// and sets exit status 1 if any output was produced (meaning diffs exist).\n// This is used in -diff mode to implement the convention that \"go fix -diff\"\n// exits non-zero when the diff is not empty, consistent with gofmt -d\n// and go mod tidy -diff.\nfunc copyAndDetectDiff(r io.Reader) error {\n\tstdouterrMu.Lock()\n\tdefer stdouterrMu.Unlock()\n\tn, err := io.Copy(os.Stdout, r)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"copying diff output: %w\", err)\n\t}\n\tif n > 0 {\n\t\tbase.SetExitStatus(1)\n\t}\n\treturn nil\n}\n\n// printJSONDiagnostics parses JSON (from the tool's stdout) and\n// prints it (to stderr) in \"file:line: message\" form.\n// It also ensures that we exit nonzero if there were diagnostics.\nfunc printJSONDiagnostics(r io.Reader) error {\n\tstdout, err := io.ReadAll(r)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif len(stdout) > 0 {\n\t\t// unitchecker emits a JSON map of the form:\n\t\t// output maps Package ID -> Analyzer.Name -> (error | []Diagnostic);","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/vet/vet.go#L344-L380","documentation":"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.","triggerScenarios":"The downstream consumer of `go vet -diff` stdout closes early, producing a broken pipe on write.","commonSituations":"Piping `go vet -diff` into head/tail/more that exits before EOF; terminal disconnect; stdout redirected to a full/truncated file.","solutions":["Ensure the downstream consumer of stdout reads to EOF","Avoid piping into head/tail; redirect to a file instead (go vet -diff > out.txt)"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Tolerate SIGPIPE when streaming vet diff output\n// (run with stdout to a file or a reader that drains to EOF)\ngo vet -diff ./... > vet.diff 2>vet.err","preventionTips":["Redirect -diff output to a file rather than piping into head/tail","Ensure any downstream consumer reads the stream to completion"],"tags":["go","vet","io","diff"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}