{"record":{"id":"0ff0ec839ad10afb","repo":"golang/go","slug":"use-of-cgo-in-test-s-not-supported","errorCode":null,"errorMessage":"use of cgo in test %s not supported","messagePattern":"use of cgo in test (.+?) not supported","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modindex/read.go","lineNumber":590,"sourceCode":"\t\t\t\tDir:      p.Dir,\n\t\t\t\tPackages: []string{p.Name, pkg},\n\t\t\t\tFiles:    []string{firstFile, name},\n\t\t\t})\n\t\t}\n\t\t// Grab the first package comment as docs, provided it is not from a test file.\n\t\tif p.Doc == \"\" && !isTest && !isXTest {\n\t\t\tif synopsis := tf.synopsis(); synopsis != \"\" {\n\t\t\t\tp.Doc = synopsis\n\t\t\t}\n\t\t}\n\n\t\t// Record Imports and information about cgo.\n\t\tisCgo := false\n\t\timports := tf.imports()\n\t\tfor _, imp := range imports {\n\t\t\tif imp.path == \"C\" {\n\t\t\t\tif isTest {\n\t\t\t\t\tbadGoFile(name, fmt.Errorf(\"use of cgo in test %s not supported\", name))\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tisCgo = true\n\t\t\t}\n\t\t}\n\t\tif directives := tf.cgoDirectives(); directives != \"\" {\n\t\t\tif err := ctxt.saveCgo(name, p, directives); err != nil {\n\t\t\t\tbadGoFile(name, err)\n\t\t\t}\n\t\t}\n\n\t\tvar fileList *[]string\n\t\tvar importMap, embedMap map[string][]token.Position\n\t\tvar directives *[]build.Directive\n\t\tswitch {\n\t\tcase isCgo:\n\t\t\tallTags[\"cgo\"] = true\n\t\t\tif ctxt.CgoEnabled {","sourceCodeStart":572,"sourceCodeEnd":608,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modindex/read.go#L572-L608","documentation":"Recorded as a bad-Go-file error during IndexPackage.Import when a _test.go file imports the pseudo-package \"C\". cgo in test files is intentionally unsupported by the build system; the offending file is added to InvalidGoFiles and the error is propagated at the end of Import. The %s is the test file name.","triggerScenarios":"A *_test.go file contains `import \"C\"` (and typically associated // #cgo directives). Reached from the imports loop in Import when imp.path == \"C\" && isTest.","commonSituations":"Refactoring production cgo code into a _test.go to exercise C functions; copy-pasting a cgo source file and renaming it to a test file; writing CGo-based integration tests.","solutions":["Move the cgo code into a non-test source file (e.g. helper.go) and call it from the test.","Use a separate package (foo_test with cgo in foo.go) or an external test binary that shells out to a cgo-enabled helper.","For testing C interop, build a small cgo helper binary via go test with a non-_test.go cgo file in the same package."],"exampleFix":"// before (foo_test.go)\npackage foo_test\n// #cgo LDFLAGS: -lm\nimport \"C\"\n// after: move cgo into helper.go (non-test), call from foo_test.go","handlingStrategy":"validation","validationCode":"// Scan _test.go files for `import \"C\"` before build.\nfunc noCgoInTests(pkgDir string) error {\n    entries, _ := os.ReadDir(pkgDir)\n    for _, e := range entries {\n        if !strings.HasSuffix(e.Name(), \"_test.go\") { continue }\n        b, _ := os.ReadFile(filepath.Join(pkgDir, e.Name()))\n        if bytes.Contains(b, []byte(`\"C\"`)) && bytes.Contains(b, []byte(\"import\")) {\n            return fmt.Errorf(\"%s: cgo is not allowed in test files\", e.Name())\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep all `import \"C\"` declarations in non-test .go files.","Expose C-backed helpers from regular sources and consume them in tests."],"tags":["cgo","testing","build-constraints"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}