{"record":{"id":"a909902dcec53a58","repo":"cilium/cilium","slug":"formatting-generated-code-w","errorCode":null,"errorMessage":"formatting generated code: %w","messagePattern":"formatting generated code: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tools/dpgen/maps.go","lineNumber":174,"sourceCode":"\n\tb := bytes.Buffer{}\n\tif err := tpl.Execute(&b, struct {\n\t\tPackage   string\n\t\tBTFFile   string\n\t\tOuterMaps []*ebpf.MapSpec\n\t\tAllMaps   []*ebpf.MapSpec\n\t}{\n\t\tpkg,\n\t\tmapKVFile,\n\t\tsorted(outer),\n\t\tsorted(all),\n\t}); err != nil {\n\t\treturn fmt.Errorf(\"executing template: %w\", err)\n\t}\n\n\tformatted, err := format.Source(b.Bytes())\n\tif err != nil {\n\t\treturn fmt.Errorf(\"formatting generated code: %w\", err)\n\t}\n\n\tif _, err := w.Write(formatted); err != nil {\n\t\treturn fmt.Errorf(\"writing formatted code: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc renderMapSpecsTest(w io.Writer, pkg string) error {\n\ttpl, err := template.New(\"mapSpecTest\").Parse(mapSpecTestTpl)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"parsing test template: %w\", err)\n\t}\n\n\tif err := tpl.Execute(w, struct{ Package string }{pkg}); err != nil {\n\t\treturn fmt.Errorf(\"executing test template: %w\", err)\n\t}","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/tools/dpgen/maps.go#L156-L192","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before (helper output \"4kHugePages\" -> invalid identifier)\nvar Specs4kHugePages ... // syntax error\n// after: prefix identifiers in camelCase helper or template so output is e.g. Specs4kHugePagesMap","handlingStrategy":"validation","validationCode":"// Validate identifiers the generator would emit\nfor name := range mapNames {\n\tid := camelCase(name)\n\tif id == \"\" || !token.IsIdentifier(id) {\n\t\treturn fmt.Errorf(\"map %q renders invalid Go identifier %q\", name, id)\n\t}\n}","typeGuard":null,"tryCatchPattern":"if err := runMaps(cmd, args); err != nil {\n\tif strings.Contains(err.Error(), \"formatting generated code\") {\n\t\tlog.Fatalf(\"generated code invalid; dump buffer before format.Source to inspect: %v\", err)\n\t}\n\treturn err\n}","preventionTips":["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"],"tags":["codegen","go-syntax","format-source"],"backgroundTag":"generated-code-does-not-compile","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}