{"record":{"id":"ab875502796718f7","repo":"golang/go","slug":"s-invalid-cgo-line-s","errorCode":null,"errorMessage":"%s: invalid #cgo line: %s","messagePattern":"(.+?): invalid #cgo line: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modindex/build.go","lineNumber":444,"sourceCode":"\t\torig := line\n\n\t\t// Line is\n\t\t//\t#cgo [GOOS/GOARCH...] LDFLAGS: stuff\n\t\t//\n\t\tline = strings.TrimSpace(line)\n\t\tif len(line) < 5 || line[:4] != \"#cgo\" || (line[4] != ' ' && line[4] != '\\t') {\n\t\t\tcontinue\n\t\t}\n\n\t\t// #cgo (nocallback|noescape) <function name>\n\t\tif fields := strings.Fields(line); len(fields) == 3 && (fields[1] == \"nocallback\" || fields[1] == \"noescape\") {\n\t\t\tcontinue\n\t\t}\n\n\t\t// Split at colon.\n\t\tline, argstr, ok := strings.Cut(strings.TrimSpace(line[4:]), \":\")\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"%s: invalid #cgo line: %s\", filename, orig)\n\t\t}\n\n\t\t// Parse GOOS/GOARCH stuff.\n\t\tf := strings.Fields(line)\n\t\tif len(f) < 1 {\n\t\t\treturn fmt.Errorf(\"%s: invalid #cgo line: %s\", filename, orig)\n\t\t}\n\n\t\tcond, verb := f[:len(f)-1], f[len(f)-1]\n\t\tif len(cond) > 0 {\n\t\t\tok := false\n\t\t\tfor _, c := range cond {\n\t\t\t\tif ctxt.matchAuto(c, nil) {\n\t\t\t\t\tok = true\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t\tif !ok {","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modindex/build.go#L426-L462","documentation":"First #cgo validation failure in Context.saveCgo (modindex/build.go): a line beginning with '#cgo' was found in the import \"C\" comment, but it contained no colon (':') to separate the verb/conditions from its arguments. The %s is the source filename and the second %s is the offending raw line.","triggerScenarios":"Writing a cgo directive without a colon, e.g. `// #cgo CFLAGS -O2` instead of `// #cgo CFLAGS: -O2`. saveCgo calls strings.Cut(line[4:], ':') and reaches the !ok branch.","commonSituations":"Translating a Makefile-style flag list into cgo comments and forgetting the colon syntax; mis-pasting a cgo line from documentation.","solutions":["Add the required colon between the verb and the arguments: `#cgo CFLAGS: -O2`.","Confirm no stray characters precede the colon; the form is `#cgo [GOOS/GOARCH...] VERB: args`.","Rebuild with CGO_ENABLED=1 to re-exercise the cgo parser after the edit."],"exampleFix":"// before\n// #cgo CFLAGS -O2 -Wall\n// after\n// #cgo CFLAGS: -O2 -Wall","handlingStrategy":"validation","validationCode":"// Lint cgo lines for a missing colon before invoking the build.\nvar cgoLineRe = regexp.MustCompile(`^#cgo\\s+([^:]*):(.*)$`)\n\nfunc lintCgoColon(line string) error {\n    t := strings.TrimSpace(line)\n    if !strings.HasPrefix(t, \"#cgo\") {\n        return nil\n    }\n    if !cgoLineRe.MatchString(t) && !isCgoNoCallbackNoescape(t) {\n        return fmt.Errorf(\"cgo line missing colon: %s\", line)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Memorize the grammar: `#cgo [conds] VERB: args`.","Keep a cgo snippet cheat-sheet in repo docs for new contributors.","Add a pre-commit grep that flags `#cgo` lines lacking a colon."],"tags":["cgo","parsing","build-directives"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}