golang/go · error

no Go source files

Error message

no Go source files

What it means

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 .).

Source

Thrown at src/cmd/go/internal/imports/scan.go:99

		m := imports
		if strings.HasSuffix(name, "_test.go") {
			m = testImports
		}
		for _, p := range list {
			q, err := strconv.Unquote(p)
			if err != nil {
				continue
			}
			m[q] = true
		}
	}
	if numFiles == 0 {
		return nil, nil, ErrNoGo
	}
	return keys(imports), keys(testImports), nil
}

var ErrNoGo = fmt.Errorf("no Go source files")

func keys(m map[string]bool) []string {
	list := make([]string, 0, len(m))
	for k := range m {
		list = append(list, k)
	}
	sort.Strings(list)
	return list
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Check that the directory actually contains .go files: ls *.go (note files starting with _ or . are excluded by default).
  2. Run go list -f '{{.GoFiles}} {{.IgnoredGoFiles}}' . to see included vs excluded files.
  3. If files have build constraints, ensure your target platform matches (GOOS/GOARCH).
  4. For test-only packages, use go test instead of go build.
  5. If using cgo, ensure CGO_ENABLED=1 is set.
Defensive patterns

Strategy: validation

Validate before calling

// Check for Go files matching build constraints before scanning.
func hasBuildableGoFiles(dir string, tags map[string]bool) bool {
    entries, _ := os.ReadDir(dir)
    for _, e := range entries {
        name := e.Name()
        if !e.Type().IsRegular() { continue }
        if !strings.HasSuffix(name, ".go") { continue }
        if strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") { continue }
        return true
    }
    return false
}

Type guard

// Type guard: does the directory contain buildable Go files?
func hasGoFiles(dir string) bool {
    entries, _ := os.ReadDir(dir)
    for _, e := range entries {
        if e.Type().IsRegular() &&
           strings.HasSuffix(e.Name(), ".go") &&
           !strings.HasPrefix(e.Name(), "_") &&
           !strings.HasPrefix(e.Name(), ".") {
            return true
        }
    }
    return false
}

Try / catch

// Compare against the sentinel error
if errors.Is(err, imports.ErrNoGo) {
    // Expected for non-package directories
    return nil // or skip this directory
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/cc5c758ab736c6a8. Report an issue: GitHub.