{"record":{"id":"0d8efee9807b8583","repo":"golangci/golangci-lint","slug":"context-loading-failed-w","errorCode":null,"errorMessage":"context loading failed: %w","messagePattern":"context loading failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/run.go","lineNumber":377,"sourceCode":"\tc.printStats(issues)\n\n\tc.setExitCodeIfIssuesFound(issues)\n\n\tc.fileCache.PrintStats(c.log)\n\n\treturn nil\n}\n\n// runAnalysis executes the linters that have been enabled in the configuration.\nfunc (c *runCommand) runAnalysis(ctx context.Context) ([]*result.Issue, error) {\n\tlintersToRun, err := c.dbManager.GetOptimizedLinters()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tlintCtx, err := c.contextBuilder.Build(ctx, c.log.Child(logutils.DebugKeyLintersContext), lintersToRun)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"context loading failed: %w\", err)\n\t}\n\n\trunner, err := lint.NewRunner(c.log.Child(logutils.DebugKeyRunner), c.cfg,\n\t\tc.goenv, c.lineCache, c.fileCache, c.dbManager, lintCtx)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn runner.Run(ctx, lintersToRun)\n}\n\nfunc (c *runCommand) setOutputToDevNull() (savedStdout, savedStderr *os.File) {\n\tsavedStdout, savedStderr = os.Stdout, os.Stderr\n\tdevNull, err := os.Open(os.DevNull)\n\tif err != nil {\n\t\tc.log.Warnf(\"Can't open null device %q: %s\", os.DevNull, err)\n\t\treturn\n\t}","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/commands/run.go#L359-L395","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run `go build ./...` in the same directory first; fix whatever compile/load error Go reports.","Run `go mod tidy` and, if vendoring, `go mod vendor` to sync dependencies.","Verify you are in the module root (go.mod exists) or pass the correct package paths.","Check Go toolchain version compatibility (go.mod's go directive vs installed Go).","In CI, ensure private modules are reachable (GOPRIVATE, credentials/netrc)."],"exampleFix":"// before (vendor out of sync)\ngolangci-lint run   # context loading failed: ... inconsistent vendoring\n// after\ngo mod vendor\ngolangci-lint run","handlingStrategy":"try-catch","validationCode":"// preflight: can the packages load at all?\nif err := exec.Command(\"go\", \"build\", \"./...\").Run(); err != nil {\n\treturn fmt.Errorf(\"packages do not build; golangci-lint context loading will fail: %w\", err)\n}\nif _, err := os.Stat(\"go.mod\"); err != nil {\n\treturn fmt.Errorf(\"run golangci-lint from the module root (go.mod missing)\")\n}","typeGuard":null,"tryCatchPattern":"if err := cmd.Run(); err != nil && strings.Contains(err.Error(), \"context loading failed\") {\n\t// surface the wrapped cause; usually run `go build ./...` to see the root error\n\tlog.Fatalf(\"fix package load errors first: %v\", err)\n}","preventionTips":["Always run `go build ./...` (or `go vet`) before linting in CI.","Run golangci-lint from the module root where go.mod lives.","Keep vendor/ in sync (`go mod vendor`) when vendoring.","Match the Go toolchain version to the go.mod directive."],"tags":["go-packages","build","ci","golangci-lint"],"backgroundTag":"package-load-failed","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}