golangci/golangci-lint · error

package %s: %w

Error message

package %s: %w

What it means

parseLoadedPackagesErrors inspects per-package errors reported by go/packages. When a package's error message contains "no Go files", loading aborts with exitcodes.ErrNoGoFiles wrapped as "package <pkgpath>: no Go files". golangci-lint cannot lint a package that has no Go source files in it.

Source

Thrown at pkg/lint/package.go:123

	return l.filterTestMainPackages(pkgs), nil
}

func (*PackageLoader) parseLoadedPackagesErrors(pkgs []*packages.Package) error {
	for _, pkg := range pkgs {
		var errs []packages.Error
		for _, err := range pkg.Errors {
			// quick fix: skip error related to `go list` invocation by packages.Load()
			// The behavior has been changed between go1.19 and go1.20, the error is now inside the JSON content.
			// https://github.com/golangci/golangci-lint/pull/3414#issuecomment-1364756303
			if strings.Contains(err.Msg, "# command-line-arguments") {
				continue
			}

			errs = append(errs, err)

			if strings.Contains(err.Msg, "no Go files") {
				return fmt.Errorf("package %s: %w", pkg.PkgPath, exitcodes.ErrNoGoFiles)
			}
			if strings.Contains(err.Msg, "cannot find package") {
				// when analyzing not existing directory
				return fmt.Errorf("%v: %w", err.Msg, exitcodes.ErrFailure)
			}
		}

		pkg.Errors = errs
	}

	return nil
}

func (l *PackageLoader) tryParseTestPackage(pkg *packages.Package) (name string, isTest bool) {
	matches := l.pkgTestIDRe.FindStringSubmatch(pkg.ID)
	if matches == nil {
		return "", false
	}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Exclude the offending directory from the run (e.g. `golangci-lint run ./...` minus that path, or `linters.exclusions.paths` for the directory)
  2. Add build tags/GOOS matching the excluded files, or enable CGO_ENABLED=1 for cgo-only packages
  3. If the directory legitimately has no Go code, remove it from the package patterns you pass to golangci-lint
  4. Ensure the directory is not an empty package accidentally included by `./...`

Example fix

// before (.golangci.yml) — fails on platform-specific dirs
// (no exclusions)
// after
linters:
  exclusions:
    paths:
      - internal/windowsonly
Defensive patterns

Strategy: validation

Validate before calling

// Check each target dir has Go files visible under current tags/GOOS before linting:
// for d in $(go list -f '{{.Dir}}' ./... 2>/dev/null); do
//   ls "$d"/*.go > /dev/null 2>&1 || echo "no Go files: $d"
// done

Prevention

When it happens

Trigger: Targeting a package directory whose build yields "build constraints exclude all Go files" or a directory with no .go files for the current platform/tags — e.g. GOOS-specific code excluded by build constraints, or a directory containing only _test.go excluded files, C files, or assets.

Common situations: Running `golangci-lint run ./...` in a repo where a directory holds only non-Go files or platform-specific code (e.g. *_windows.go while linting on Linux with constraints); missing CGO enabling for cgo-only packages.

Related errors


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/2b52a20e44dc396a. Report an issue: GitHub.