golangci/golangci-lint · error

failed to load packages: %w

Error message

failed to load packages: %w

What it means

PackageLoader.Load is the entry point for loading Go packages via go/packages; when the underlying loadPackages call returns any error, it is wrapped as "failed to load packages". This is a wrapper error — the real cause is in the wrapped %w chain (toolchain, module resolution, or per-package errors surfaced by parseLoadedPackagesErrors).

Source

Thrown at pkg/lint/package.go:60

func NewPackageLoader(log logutils.Log, cfg *config.Config, args []string, env *goutil.Env, loadGuard *load.Guard) *PackageLoader {
	return &PackageLoader{
		cfg:         cfg,
		args:        args,
		log:         log,
		debugf:      logutils.Debug(logutils.DebugKeyLoader),
		goenv:       env,
		pkgTestIDRe: regexp.MustCompile(`^(.*) \[(.*)\.test\]`),
		loadGuard:   loadGuard,
	}
}

// Load loads packages.
func (l *PackageLoader) Load(ctx context.Context, linters []*linter.Config) (pkgs, deduplicatedPkgs []*packages.Package, err error) {
	loadMode := findLoadMode(linters)

	pkgs, err = l.loadPackages(ctx, loadMode)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to load packages: %w", err)
	}

	return pkgs, l.filterDuplicatePackages(pkgs), nil
}

func (l *PackageLoader) loadPackages(ctx context.Context, loadMode packages.LoadMode) ([]*packages.Package, error) {
	defer func(startedAt time.Time) {
		l.log.Infof("Go packages loading at mode %s took %s", stringifyLoadMode(loadMode), time.Since(startedAt))
	}(time.Now())

	l.prepareBuildContext()

	conf := &packages.Config{
		Mode:       loadMode,
		Tests:      l.cfg.Run.AnalyzeTests,
		Context:    ctx,
		BuildFlags: l.makeBuildFlags(),
		Logf:       l.debugf,

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Read the wrapped cause in the error chain (Go 1.13+ %w) — fix the root error first (e.g. ErrNoGoFiles, module download failure)
  2. Run `go build ./...` in the same directory to confirm the package graph loads with the plain toolchain
  3. Ensure you are inside a valid module (`go.mod` present) and GOFLAGS/GOPROXY allow dependency resolution
  4. Match your Go toolchain version to the one the project requires (go directive in go.mod)

Example fix

// before (shell)
cd ./pkg/somepkg && golangci-lint run   # outside module root context
// after
cd /path/to/module/root && golangci-lint run ./pkg/somepkg/...
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure module context is valid before linting:
// if [ ! -f go.mod ]; then echo 'not a Go module'; exit 1; fi
// go build ./... || { echo 'packages do not build; fix before linting'; exit 1; }

Try / catch

err := golangciLintRun();
var loadErr *LoadError
if errors.As(err, &loadErr) {
    // inspect errors.Unwrap(loadErr) / errors.Is(err, exitcodes.ErrNoGoFiles)
    // fix the root package-loading cause before retrying
}

Prevention

When it happens

Trigger: Any error returned by PackageLoader.loadPackages: `packages.Load` hard failure, or parse errors including ErrNoGoFiles / "cannot find package" conditions detected in parseLoadedPackagesErrors.

Common situations: Running golangci-lint in a directory outside a Go module; unresolvable imports because dependencies are not downloadable (network/proxy issues); invalid Go files in the target package; wrong path passed to `./...` style arguments.

Related errors


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