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), nilView on GitHub (pinned to ed7a235d2d)
Solutions
- Run `go env` and `go build ./...` manually to reproduce the underlying toolchain failure
- Check `run.build-tags` and other loader args in golangci-lint config for invalid flags
- Clear the module cache if corrupt: `go clean -modcache`, then `go mod download`
- 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
- Warm the module cache (`go mod download`) before linting in fresh CI containers
- Validate `run.build-tags` and loader args against your Go version's accepted flags
- Avoid concurrent runs sharing one GOPATH/pkg/mod during first download
- Keep golang.org/x/tools/go/packages and golangci-lint versions compatible
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
- context loading failed: %w
- failed to load packages: %w
- package %s: %w
- %v: %w
- the configuration contains invalid elements
AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02).
Data as JSON: /api/errors/ddb3e7fe8d9a216c.
Report an issue: GitHub.