golangci/golangci-lint · error

failed to load with go/packages: %w

Error message

failed to load with go/packages: %w

What it means

loadPackages calls `packages.Load` from golang.org/x/tools/go/packages; when that returns a top-level error (as opposed to per-package errors), it is wrapped as "failed to load with go/packages". This means the driver itself failed before/while building the package graph, not that an individual package had lint findings.

Source

Thrown at pkg/lint/package.go:88

	l.prepareBuildContext()

	conf := &packages.Config{
		Mode:       loadMode,
		Tests:      l.cfg.Run.AnalyzeTests,
		Context:    ctx,
		BuildFlags: l.makeBuildFlags(),
		Logf:       l.debugf,
		// TODO: use fset, parsefile, overlay
	}

	args := buildArgs(l.args)

	l.debugf("Built loader args are %s", args)

	pkgs, err := packages.Load(conf, args...)
	if err != nil {
		return nil, fmt.Errorf("failed to load with go/packages: %w", err)
	}

	if loadMode&packages.NeedSyntax == 0 {
		// Needed e.g. for go/analysis loading.
		fset := token.NewFileSet()
		packages.Visit(pkgs, nil, func(pkg *packages.Package) {
			pkg.Fset = fset
			l.loadGuard.AddMutexForPkg(pkg)
		})
	}

	l.debugPrintLoadedPackages(pkgs)

	if err := l.parseLoadedPackagesErrors(pkgs); err != nil {
		return nil, err
	}

	return l.filterTestMainPackages(pkgs), nil

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Run `go env` and `go build ./...` manually to reproduce the underlying toolchain failure
  2. Check `run.build-tags` and other loader args in golangci-lint config for invalid flags
  3. Clear the module cache if corrupt: `go clean -modcache`, then `go mod download`
  4. Upgrade or repair the Go toolchain to satisfy the module's go directive

Example fix

// before (.golangci.yml)
run:
  build-tags:
    - '-broken flag'   # invalid arg passed to go/packages
// after
run:
  build-tags:
    - integ
Defensive patterns

Strategy: retry

Validate before calling

// Pre-flight: verify the toolchain and args work with plain go/packages
// go version && go env GOMODCACHE GOPROXY
// go list ./... > /dev/null || echo 'go/packages will fail'

Try / catch

pkgs, err := loadPackages(ctx, mode)
if err != nil {
    if errors.Is(err, context.DeadlineExceeded) {
        // retry once with longer timeout / warm module cache
    }
    return fmt.Errorf("failed to load with go/packages: %w", err)
}

Prevention

When it happens

Trigger: `packages.Load(conf, args...)` returns err: invalid loader args/build flags, Go command unavailable or failing, corrupted module cache, context cancellation, or unsupported go.mod version.

Common situations: Misconfigured build tags or GOOS/GOARCH via config `run.build-tags`; missing/broken Go installation on CI; corrupt GOPATH/pkg/mod cache after interrupted downloads; Go version too old for the module's go directive.

Related errors


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