{"record":{"id":"dd6331df1d0739ee","repo":"golangci/golangci-lint","slug":"cgo-preprocessing-failed","errorCode":null,"errorMessage":"cgo preprocessing failed","messagePattern":"cgo preprocessing failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/goanalysis/runner_loadingpackage.go","lineNumber":176,"sourceCode":"\t\tTypes:        make(map[ast.Expr]types.TypeAndValue),\n\t\tInstances:    make(map[*ast.Ident]types.Instance),\n\t\tDefs:         make(map[*ast.Ident]types.Object),\n\t\tUses:         make(map[*ast.Ident]types.Object),\n\t\tImplicits:    make(map[ast.Node]types.Object),\n\t\tSelections:   make(map[*ast.SelectorExpr]*types.Selection),\n\t\tScopes:       make(map[ast.Node]*types.Scope),\n\t\tFileVersions: make(map[*ast.File]string),\n\t}\n\n\timporter := func(path string) (*types.Package, error) {\n\t\tif path == unsafePkgName {\n\t\t\treturn types.Unsafe, nil\n\t\t}\n\t\tif path == \"C\" {\n\t\t\t// go/packages doesn't tell us that cgo preprocessing\n\t\t\t// failed. When we subsequently try to parse the package,\n\t\t\t// we'll encounter the raw C import.\n\t\t\treturn nil, errors.New(\"cgo preprocessing failed\")\n\t\t}\n\t\timp := pkg.Imports[path]\n\t\tif imp == nil {\n\t\t\treturn nil, nil\n\t\t}\n\t\tif len(imp.Errors) > 0 {\n\t\t\treturn nil, imp.Errors[0]\n\t\t}\n\t\treturn imp.Types, nil\n\t}\n\n\tvar goVersion string\n\tif pkg.Module != nil && pkg.Module.GoVersion != \"\" {\n\t\tgoVersion = \"go\" + strings.TrimPrefix(pkg.Module.GoVersion, \"go\")\n\t} else {\n\t\tvar err error\n\t\tgoVersion, err = goutil.CleanRuntimeVersion()\n\t\tif err != nil {","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/goanalysis/runner_loadingpackage.go#L158-L194","documentation":"When loading packages, a 'C' pseudo-package import means cgo preprocessing should have produced generated Go code. go/packages does not report cgo preprocessing failures, so the runner detects the raw C import during type checking and throws this error to signal that cgo failed before parsing. It is an internal safeguard in the loader's importer.","triggerScenarios":"A package imports \"C\" but cgo preprocessing failed (missing C headers, invalid cgo directives, CGO_ENABLED issues, missing C compiler), so the raw C file content is encountered during type resolution.","commonSituations":"Building cgo code in environments without gcc/clang; missing #cgo pkg-config dependencies or headers; CGO_ENABLED=0 while analyzing cgo packages; corrupted build cache after toolchain changes.","solutions":["Run 'go build' on the failing package to see the real cgo error and fix it (missing header, bad directive)","Verify a C compiler is installed and CGO_ENABLED=1 (unset CGO_ENABLED=0)","Clear the build cache with 'go clean -cache' and retry","Install missing C libraries/pkg-config files referenced by #cgo directives"],"exampleFix":"// before\nCGO_ENABLED=0 golangci-lint run\n\n// after\nCGO_ENABLED=1 golangci-lint run","handlingStrategy":"validation","validationCode":"if out, err := exec.Command(\"go\", \"build\", \"./...\").CombinedOutput(); err != nil {\n    return fmt.Errorf(\"fix build before linting (cgo issue?): %v\\n%s\", err, out)\n}","typeGuard":null,"tryCatchPattern":"if strings.Contains(err.Error(), \"cgo preprocessing failed\") {\n    // ensure CGO_ENABLED=1 and a C compiler is available, then retry\n}","preventionTips":["Install gcc/clang in CI images that lint cgo code","Do not set CGO_ENABLED=0 for projects importing \"C\"","Run 'go build ./...' before running the linter","Run 'go clean -cache' after toolchain upgrades"],"tags":["cgo","build","go-analysis","packages"],"backgroundTag":"cgo-preprocessing-failed","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}