golangci/golangci-lint · error

context loading failed: %w

Error message

context loading failed: %w

What it means

golangci-lint's contextBuilder.Build loads packages, type info, and linter context for the run; any failure there is wrapped as "context loading failed". This aggregates root causes such as package load errors, bad Go environment, or issues parsing go.mod/go.work.

Source

Thrown at pkg/commands/run.go:377

	c.printStats(issues)

	c.setExitCodeIfIssuesFound(issues)

	c.fileCache.PrintStats(c.log)

	return nil
}

// runAnalysis executes the linters that have been enabled in the configuration.
func (c *runCommand) runAnalysis(ctx context.Context) ([]*result.Issue, error) {
	lintersToRun, err := c.dbManager.GetOptimizedLinters()
	if err != nil {
		return nil, err
	}

	lintCtx, err := c.contextBuilder.Build(ctx, c.log.Child(logutils.DebugKeyLintersContext), lintersToRun)
	if err != nil {
		return nil, fmt.Errorf("context loading failed: %w", err)
	}

	runner, err := lint.NewRunner(c.log.Child(logutils.DebugKeyRunner), c.cfg,
		c.goenv, c.lineCache, c.fileCache, c.dbManager, lintCtx)
	if err != nil {
		return nil, err
	}

	return runner.Run(ctx, lintersToRun)
}

func (c *runCommand) setOutputToDevNull() (savedStdout, savedStderr *os.File) {
	savedStdout, savedStderr = os.Stdout, os.Stderr
	devNull, err := os.Open(os.DevNull)
	if err != nil {
		c.log.Warnf("Can't open null device %q: %s", os.DevNull, err)
		return
	}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Run `go build ./...` in the same directory first; fix whatever compile/load error Go reports.
  2. Run `go mod tidy` and, if vendoring, `go mod vendor` to sync dependencies.
  3. Verify you are in the module root (go.mod exists) or pass the correct package paths.
  4. Check Go toolchain version compatibility (go.mod's go directive vs installed Go).
  5. In CI, ensure private modules are reachable (GOPRIVATE, credentials/netrc).

Example fix

// before (vendor out of sync)
golangci-lint run   # context loading failed: ... inconsistent vendoring
// after
go mod vendor
golangci-lint run
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight: can the packages load at all?
if err := exec.Command("go", "build", "./...").Run(); err != nil {
	return fmt.Errorf("packages do not build; golangci-lint context loading will fail: %w", err)
}
if _, err := os.Stat("go.mod"); err != nil {
	return fmt.Errorf("run golangci-lint from the module root (go.mod missing)")
}

Try / catch

if err := cmd.Run(); err != nil && strings.Contains(err.Error(), "context loading failed") {
	// surface the wrapped cause; usually run `go build ./...` to see the root error
	log.Fatalf("fix package load errors first: %v", err)
}

Prevention

When it happens

Trigger: Running golangci-lint in a directory where the packages cannot be loaded: syntax/type errors in packages, missing go.mod, unsupported Go version mismatch, GOPATH/GOFLAGS misconfiguration, or build constraints excluding all files.

Common situations: Running from the wrong subdirectory (no go.mod); Go version newer than the code/toolchain expects; vendor directory out of sync (go mod vendor not run after dependency changes); GOPRIVATE/modules not downloadable in CI.

Related errors


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