{"record":{"id":"b1b9634fca888051","repo":"golang/go","slug":"swig-file-must-not-use-prefix-cgo","errorCode":null,"errorMessage":"SWIG file must not use prefix 'cgo'","messagePattern":"SWIG file must not use prefix 'cgo'","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/work/exec.go","lineNumber":3764,"sourceCode":"\tif _, _, e := BuildToolchain.gc(b, &Action{Mode: \"swigDoIntSize\", Package: p, Objdir: objdir}, \"\", nil, nil, \"\", false, \"\", \"\", srcs); e != nil {\n\t\treturn \"32\", nil\n\t}\n\treturn \"64\", nil\n}\n\n// Determine the size of int on the target system for the -intgosize option\n// of swig >= 2.0.9.\nfunc (b *Builder) swigIntSize(objdir string) (intsize string, err error) {\n\tswigIntSizeOnce.Do(func() {\n\t\tswigIntSize, swigIntSizeError = b.swigDoIntSize(objdir)\n\t})\n\treturn swigIntSize, swigIntSizeError\n}\n\n// Run SWIG on one SWIG input file.\nfunc (b *Builder) swigOne(a *Action, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) error {\n\tif strings.HasPrefix(file, \"cgo\") {\n\t\treturn errors.New(\"SWIG file must not use prefix 'cgo'\")\n\t}\n\n\tp := a.Package\n\tsh := b.Shell(a)\n\n\tcgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _, err := b.CFlags(p)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tvar cflags []string\n\tif cxx {\n\t\tcflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCXXFLAGS)\n\t} else {\n\t\tcflags = str.StringList(cgoCPPFLAGS, pcCFLAGS, cgoCFLAGS)\n\t}\n\n\tbase := swigBase(file, cxx)","sourceCodeStart":3746,"sourceCodeEnd":3782,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/work/exec.go#L3746-L3782","documentation":"Thrown by swigOne in cmd/go/internal/work before invoking swig, when the input .swig file's basename begins with \"cgo\" (e.g. cgo.swig, cgomodule.swig). The cgo tool runs after SWIG and writes intermediate files named cgo_*; a SWIG input with that prefix collides with cgo's output naming scheme, causing silent file overwrites and broken builds. The go command rejects it up front.","triggerScenarios":"Naming a SWIG interface file cgo.swig, cgotypes.swig, or any file whose name starts with the three letters \"cgo\". Renaming an existing .swig file to add a cgo prefix for organizational reasons.","commonSituations":"Teams that prefix all cgo-related files with \"cgo\" for sorting. Code generators that emit filename prefixes based on package features. Copy-paste of SWIG examples that happened to use the cgo prefix.","solutions":["Rename the .swig file so its basename does not start with \"cgo\" — e.g. wrapper.swig, binding.swig, or <package>.swig.","Update any build scripts, Makefiles, or //go:generate directives that reference the old filename.","Add a pre-commit hook or CI check that flags *.swig files beginning with \"cgo\"."],"exampleFix":"# before\n$ ls\nmain.go  cgo.swig\n$ go build ./...\n# -> SWIG file must not use prefix 'cgo'\n\n# after\n$ mv cgo.swig wrapper.swig\n$ go build ./...","handlingStrategy":"validation","validationCode":"func validateSwigFilenames(files []string) error {\n    for _, f := range files {\n        if strings.HasPrefix(filepath.Base(f), \"cgo\") {\n            return fmt.Errorf(\"SWIG file %q must not begin with 'cgo' — rename it\", f)\n        }\n    }\n    return nil\n}","typeGuard":"func swigNameOK(name string) bool { return !strings.HasPrefix(filepath.Base(name), \"cgo\") }","tryCatchPattern":null,"preventionTips":["Reserve the \"cgo\" prefix for the cgo tool's own generated files.","Add a pre-commit check that flags *.swig files beginning with cgo.","Use the package name or a descriptive word as the SWIG filename stem."],"tags":["go-toolchain","swig","cgo","naming","validation"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}