golangci/golangci-lint · error

failed to load packages: %w

Error message

failed to load packages: %w

What it means

ContextBuilder.Build loads the packages to analyze via the package loader. If that load fails (compile errors, bad build flags, missing modules, etc.), the error is wrapped as "failed to load packages". The library throws it because it cannot build the lint context without successfully loading the target packages.

Source

Thrown at pkg/lint/context.go:39

	loadGuard *load.Guard
}

func NewContextBuilder(cfg *config.Config, pkgLoader *PackageLoader,
	pkgCache *cache.Cache, loadGuard *load.Guard,
) *ContextBuilder {
	return &ContextBuilder{
		cfg:       cfg,
		pkgLoader: pkgLoader,
		pkgCache:  pkgCache,
		loadGuard: loadGuard,
	}
}

func (cl *ContextBuilder) Build(ctx context.Context, log logutils.Log, linters []*linter.Config) (*linter.Context, error) {
	pkgs, deduplicatedPkgs, err := cl.pkgLoader.Load(ctx, linters)
	if err != nil {
		return nil, fmt.Errorf("failed to load packages: %w", err)
	}

	if len(deduplicatedPkgs) == 0 {
		return nil, fmt.Errorf("%w: running `go mod tidy` may solve the problem", exitcodes.ErrNoGoFiles)
	}

	ret := &linter.Context{
		Packages: deduplicatedPkgs,

		// At least `unused` linters works properly only on original (not deduplicated) packages,
		// see https://github.com/golangci/golangci-lint/pull/585.
		OriginalPackages: pkgs,

		Cfg:       cl.cfg,
		Log:       log,
		PkgCache:  cl.pkgCache,
		LoadGuard: cl.loadGuard,
	}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Run `go build ./...` (or `go vet ./...`) to see the underlying compile/type error and fix it first.
  2. Run `go mod tidy` to resolve missing or inconsistent module requirements.
  3. Check the `run.build-tags`, `run.skip-dirs`, and packages patterns in config for typos or tags that exclude/break loading.
  4. Ensure the installed Go version satisfies the go directive in go.mod.
  5. Read the wrapped cause (%w) after this message for the exact package-load failure.

Example fix

// before: module deps missing, loader fails
// terminal
lint ./...
# failed to load packages: no required module provides package "github.com/x/y"

// after
go mod tidy
lint ./...
Defensive patterns

Strategy: try-catch

Validate before calling

out, err := exec.Command("go", "build", "./...").CombinedOutput()
if err != nil {
    return fmt.Errorf("project does not build, skipping lint: %s", out)
}

Try / catch

ctx, err := builder.Build(ctx, log, linters)
if err != nil {
    if strings.Contains(err.Error(), "failed to load packages") {
        log.Errorf("package load failed: %v", err) // inspect wrapped cause
        return err
    }
    return err
}

Prevention

When it happens

Trigger: runAnalysis -> Build -> pkgLoader.Load returns an error: Go source with syntax/type errors, invalid build tags/constraints, missing go.mod entries, bad GOFLAGS/packages pattern in configuration, or packages that fail to type-check under the selected Go version.

Common situations: Running the linter on a project that does not compile, referencing packages outside the module, stale vendor directory, mismatched Go version in go.mod vs installed toolchain, or wrong path-prefix/paths args in config.

Related errors


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