github/github-mcp-server · info
failed to write CSV header: %w
Error message
failed to write CSV header: %w
What it means
csv.Writer.Write failed while emitting the header row into a bytes.Buffer. The Go csv writer only returns errors from the underlying writer or on field-count/quoting invariants, and a bytes.Buffer's Write never returns an error, so this branch is effectively unreachable defensive code. Reaching it implies memory exhaustion or buffer corruption rather than a data problem.
Source
Thrown at pkg/github/csv_output.go:135
doc := csvDocument(value)
if len(doc.metadata) == 0 && len(doc.rows) == 0 {
return "", nil
}
var buf bytes.Buffer
writeCSVMetadata(&buf, doc.metadata)
if len(doc.rows) == 0 {
return buf.String(), nil
}
headers := csvHeaders(doc.rows)
if len(headers) == 0 {
return buf.String(), nil
}
writer := csv.NewWriter(&buf)
if err := writer.Write(headers); err != nil {
return "", fmt.Errorf("failed to write CSV header: %w", err)
}
for _, row := range doc.rows {
record := make([]string, len(headers))
for i, header := range headers {
record[i] = row[header]
}
if err := writer.Write(record); err != nil {
return "", fmt.Errorf("failed to write CSV row: %w", err)
}
}
writer.Flush()
if err := writer.Error(); err != nil {
return "", fmt.Errorf("failed to flush CSV: %w", err)
}
return buf.String(), nil
}View on GitHub (pinned to 0ea1f775a7)
Solutions
- If converting very large result sets, request smaller pages (lower per_page) so the CSV fits comfortably in memory
- Check container memory limits and raise them if the output is legitimately huge
- Otherwise treat as a defensive invariant and report upstream with the error string
Defensive patterns
Strategy: try-catch
Try / catch
if err := writer.Write(headers); err != nil {
// bytes.Buffer writes cannot fail: treat as fatal invariant, log and abort conversion
return "", fmt.Errorf("failed to write CSV header: %w", err)
} Prevention
- Bound CSV output size via pagination/filters to stay within memory limits
- Monitor container memory when exporting large result sets to CSV
When it happens
Trigger: Converting a JSON document with rows to CSV when the in-memory buffer write fails — practically only under severe memory pressure (OOM-adjacent) or with a corrupted buffer in modified builds.
Common situations: Extremely large CSV conversions on memory-constrained containers; not reproducible under normal conditions.
Related errors
- failed to write CSV row: %w
- failed to flush CSV: %w
- failed to unmarshal JSON text: %w
- failed to marshal discussions: %w
- failed to marshal discussion: %w
AI-assisted analysis of github/github-mcp-server@0ea1f775a7 (2026-08-15).
Data as JSON: /api/errors/885f3b262abf2ad1.
Report an issue: GitHub.