{"record":{"id":"cc5c758ab736c6a8","repo":"golang/go","slug":"no-go-source-files","errorCode":null,"errorMessage":"no Go source files","messagePattern":"no Go source files","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/imports/scan.go","lineNumber":99,"sourceCode":"\t\tm := imports\n\t\tif strings.HasSuffix(name, \"_test.go\") {\n\t\t\tm = testImports\n\t\t}\n\t\tfor _, p := range list {\n\t\t\tq, err := strconv.Unquote(p)\n\t\t\tif err != nil {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tm[q] = true\n\t\t}\n\t}\n\tif numFiles == 0 {\n\t\treturn nil, nil, ErrNoGo\n\t}\n\treturn keys(imports), keys(testImports), nil\n}\n\nvar ErrNoGo = fmt.Errorf(\"no Go source files\")\n\nfunc keys(m map[string]bool) []string {\n\tlist := make([]string, 0, len(m))\n\tfor k := range m {\n\t\tlist = append(list, k)\n\t}\n\tsort.Strings(list)\n\treturn list\n}\n","sourceCodeStart":81,"sourceCodeEnd":109,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/imports/scan.go#L81-L109","documentation":"ErrNoGo is returned when a directory scan finds zero Go source files matching the current build constraints. This exported sentinel error means either the directory genuinely has no .go files, or all .go files were excluded by build tag filtering, cgo filtering (import \"C\" without the cgo tag), or filename conventions (files prefixed with _ or .).","triggerScenarios":"Calling imports.ScanDir on a directory where every .go file either doesn't match the build tags, contains import \"C\" without the cgo tag enabled, starts with _ or ., or simply doesn't exist. The numFiles counter stays at 0 and ErrNoGo is returned.","commonSituations":"Running go build on a directory containing only _test.go files. Packages whose only Go files have build constraints excluding the current platform (e.g., //go:build linux on macOS). Empty directories in a module. Documentation-only directories. CGO_ENABLED=0 hiding all cgo files.","solutions":["Check that the directory actually contains .go files: ls *.go (note files starting with _ or . are excluded by default).","Run go list -f '{{.GoFiles}} {{.IgnoredGoFiles}}' . to see included vs excluded files.","If files have build constraints, ensure your target platform matches (GOOS/GOARCH).","For test-only packages, use go test instead of go build.","If using cgo, ensure CGO_ENABLED=1 is set."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Check for Go files matching build constraints before scanning.\nfunc hasBuildableGoFiles(dir string, tags map[string]bool) bool {\n    entries, _ := os.ReadDir(dir)\n    for _, e := range entries {\n        name := e.Name()\n        if !e.Type().IsRegular() { continue }\n        if !strings.HasSuffix(name, \".go\") { continue }\n        if strings.HasPrefix(name, \"_\") || strings.HasPrefix(name, \".\") { continue }\n        return true\n    }\n    return false\n}","typeGuard":"// Type guard: does the directory contain buildable Go files?\nfunc hasGoFiles(dir string) bool {\n    entries, _ := os.ReadDir(dir)\n    for _, e := range entries {\n        if e.Type().IsRegular() &&\n           strings.HasSuffix(e.Name(), \".go\") &&\n           !strings.HasPrefix(e.Name(), \"_\") &&\n           !strings.HasPrefix(e.Name(), \".\") {\n            return true\n        }\n    }\n    return false\n}","tryCatchPattern":"// Compare against the sentinel error\nif errors.Is(err, imports.ErrNoGo) {\n    // Expected for non-package directories\n    return nil // or skip this directory\n}","preventionTips":["Check for .go files before building a directory.","Use go test instead of go build for test-only packages.","Verify build tags match your target platform (GOOS/GOARCH).","Ensure CGO_ENABLED matches your package's needs."],"tags":["go","go-build","imports","scan","build-tags","cgo"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}