golangci/golangci-lint · error

%v: %w

Error message

%v: %w

What it means

In parseLoadedPackagesErrors, when a package error contains "cannot find package", golangci-lint wraps it as "<msg>: failure" (exitcodes.ErrFailure). Per the source comment this happens when analyzing a directory that does not exist — the Go toolchain cannot resolve the given package path.

Source

Thrown at pkg/lint/package.go:127

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
	}

	return matches[1], true
}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Verify the path passed to golangci-lint exists relative to the module root (check for typos)
  2. Run `go mod download` (or use vendoring consistently) so all imports resolve
  3. Run `go build ./...` to see the full "cannot find package" message and fix the missing module/import
  4. Fix CI scripts or Makefiles that reference renamed/moved directories

Example fix

// before (shell)
golangci-lint run ./internal/lint/...
// after  (dir was renamed)
golangci-lint run ./pkg/lint/...
Defensive patterns

Strategy: validation

Validate before calling

// Verify target paths exist before invoking golangci-lint:
// for p in "$@"; do
//   [ -e "$p" ] || { echo "path does not exist: $p"; exit 1; }
// done

Prevention

When it happens

Trigger: Passing a package pattern whose directory does not exist (e.g. `golangci-lint run ./nonexistent/...`), or an import inside the code pointing to a package that cannot be found because a dependency/module is missing.

Common situations: Typo in the path given on the command line; CI scripts referencing a directory that was moved/renamed; missing `go mod download` / vendoring so imports cannot resolve.

Related errors


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