cilium/cilium · error
formatting generated code: %w
Error message
formatting generated code: %w
What it means
The rendered template output is passed through go/format's format.Source to produce gofmt-clean Go code. This error wraps format.Source failing, which means the generated maps_generated.go content is not syntactically valid Go. The template emitted code the Go parser rejects.
Source
Thrown at tools/dpgen/maps.go:174
b := bytes.Buffer{}
if err := tpl.Execute(&b, struct {
Package string
BTFFile string
OuterMaps []*ebpf.MapSpec
AllMaps []*ebpf.MapSpec
}{
pkg,
mapKVFile,
sorted(outer),
sorted(all),
}); err != nil {
return fmt.Errorf("executing template: %w", err)
}
formatted, err := format.Source(b.Bytes())
if err != nil {
return fmt.Errorf("formatting generated code: %w", err)
}
if _, err := w.Write(formatted); err != nil {
return fmt.Errorf("writing formatted code: %w", err)
}
return nil
}
func renderMapSpecsTest(w io.Writer, pkg string) error {
tpl, err := template.New("mapSpecTest").Parse(mapSpecTestTpl)
if err != nil {
return fmt.Errorf("parsing test template: %w", err)
}
if err := tpl.Execute(w, struct{ Package string }{pkg}); err != nil {
return fmt.Errorf("executing test template: %w", err)
}View on GitHub (pinned to ac7b90affa)
Solutions
- Inspect the wrapped error — format.Source reports the exact line/column of the syntax error
- Dump the unformatted buffer (temporarily skip format.Source) and examine the generated code around the reported position
- Fix mapSpecTpl or the camelCase helper to emit valid Go identifiers for the offending map/typename
- Re-run dpgen maps to regenerate and confirm it formats cleanly
Example fix
// before (helper output "4kHugePages" -> invalid identifier) var Specs4kHugePages ... // syntax error // after: prefix identifiers in camelCase helper or template so output is e.g. Specs4kHugePagesMap
Defensive patterns
Strategy: validation
Validate before calling
// Validate identifiers the generator would emit
for name := range mapNames {
id := camelCase(name)
if id == "" || !token.IsIdentifier(id) {
return fmt.Errorf("map %q renders invalid Go identifier %q", name, id)
}
} Try / catch
if err := runMaps(cmd, args); err != nil {
if strings.Contains(err.Error(), "formatting generated code") {
log.Fatalf("generated code invalid; dump buffer before format.Source to inspect: %v", err)
}
return err
} Prevention
- Ensure camelCase output always starts with a letter or underscore
- After any template change, eyeball the rendered output before format.Source
- Add a test that formats fixture-rendered output with format.Source
When it happens
Trigger: The rendered mapSpecTpl output contains a syntax error — typically caused by template changes emitting invalid Go, malformed identifiers produced by camelCase on unusual map names, or empty/odd map data producing degenerate declarations.
Common situations: Developers editing mapSpecTpl and emitting unbalanced braces or invalid declarations; a map/typename combination whose camelCased identifier is not a valid Go identifier (e.g. starting with a digit); new BPF type names with characters Go cannot represent.
Related errors
- generating config struct: %w
- writing util file: %w
- writing header: %w
- external types needed but types package is empty
- writing imports: %w
AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31).
Data as JSON: /api/errors/a909902dcec53a58.
Report an issue: GitHub.