{"record":{"id":"f46b9f75ac882cbb","repo":"golang/go","slug":"objective-c-source-files-not-allowed-when-not-usin","errorCode":null,"errorMessage":"Objective-C source files not allowed when not using cgo or SWIG: %s","messagePattern":"Objective-C source files not allowed when not using cgo or SWIG: (.+?)","errorType":"exception","errorClass":"PackageError","httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/load/pkg.go","lineNumber":2115,"sourceCode":"\t\t// and HFiles are okay (they might be used by the SFiles).\n\t\t// Also Sysofiles are okay (they might not contain object\n\t\t// code; see issue #16050).\n\t}\n\n\t// The gc toolchain only permits C source files with cgo or SWIG.\n\tif len(p.CFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() && cfg.BuildContext.Compiler == \"gc\" {\n\t\tsetError(fmt.Errorf(\"C source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.CFiles, \" \")))\n\t\treturn\n\t}\n\n\t// C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,\n\t// regardless of toolchain.\n\tif len(p.CXXFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {\n\t\tsetError(fmt.Errorf(\"C++ source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.CXXFiles, \" \")))\n\t\treturn\n\t}\n\tif len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {\n\t\tsetError(fmt.Errorf(\"Objective-C source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.MFiles, \" \")))\n\t\treturn\n\t}\n\tif len(p.FFiles) > 0 && !p.UsesCgo() && !p.UsesSwig() {\n\t\tsetError(fmt.Errorf(\"Fortran source files not allowed when not using cgo or SWIG: %s\", strings.Join(p.FFiles, \" \")))\n\t\treturn\n\t}\n}\n\n// An EmbedError indicates a problem with a go:embed directive.\ntype EmbedError struct {\n\tPattern string\n\tErr     error\n}\n\nfunc (e *EmbedError) Error() string {\n\treturn fmt.Sprintf(\"pattern %s: %v\", e.Pattern, e.Err)\n}\n","sourceCodeStart":2097,"sourceCodeEnd":2133,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/load/pkg.go#L2097-L2133","documentation":"A Go package contains Objective-C source files (.m) but neither cgo nor SWIG is active. Like the C++ check, this restriction applies to all compiler toolchains. The check is len(p.MFiles) > 0 && !p.UsesCgo() && !p.UsesSwig(). Objective-C files are typically used for macOS/iOS framework integration.","triggerScenarios":"Adding .m files to a package directory without setting up cgo or SWIG integration. Having leftover Objective-C files from a previous macOS-specific build configuration. Cross-compiling away from macOS while .m files remain in the source tree.","commonSituations":"Integrating macOS frameworks (Foundation, UIKit, AppKit) via Objective-C without proper cgo setup. Accidental inclusion of .m files in a cross-platform package. Removing cgo integration but forgetting to delete .m files. Third-party macOS bindings that ship .m files.","solutions":["Enable cgo and add a proper // #cgo preamble in a .go file that wraps the Objective-C code.","Ensure CGO_ENABLED=1, especially on macOS (go env CGO_ENABLED).","Remove .m files if Objective-C integration is not needed for the current build target.","Consider SWIG for complex Objective-C framework bridging."],"exampleFix":"// before — .m file present but no cgo\n// mypackage/\n//   main.go\n//   delegate.m\n\n// after — add cgo wrapping ObjC code\n// mypackage/main.go\npackage main\n\n/*\n#cgo darwin LDFLAGS: -framework Foundation\n#import <Foundation/Foundation.h>\n#include \"delegate.h\"\n*/\nimport \"C\"\n// mypackage/delegate.m remains","handlingStrategy":"validation","validationCode":"// Check that Objective-C files are accompanied by cgo or SWIG before building.\nfunc checkObjcCgoRequirement(pkgDir string) error {\n    entries, _ := os.ReadDir(pkgDir)\n    var hasM, hasCgo, hasSwig bool\n    for _, e := range entries {\n        name := e.Name()\n        if strings.HasSuffix(name, \".m\") {\n            hasM = true\n        }\n        if strings.HasSuffix(name, \".swig\") || strings.HasSuffix(name, \".swigcxx\") {\n            hasSwig = true\n        }\n        if strings.HasSuffix(name, \".go\") {\n            data, _ := os.ReadFile(filepath.Join(pkgDir, name))\n            if bytes.Contains(data, []byte(\"import \\\"C\\\"\")) {\n                hasCgo = true\n            }\n        }\n    }\n    if hasM && !hasCgo && !hasSwig {\n        return fmt.Errorf(\"Objective-C files present without cgo or SWIG\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set up cgo with #cgo LDFLAGS for macOS frameworks whenever adding .m files.","Ensure CGO_ENABLED=1 on macOS when building with Objective-C.","Remove .m files if macOS framework integration is not needed for the target platform.","Use #import for framework headers in the cgo preamble."],"tags":["go","go-build","cgo","objective-c","macos","native-code"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}