{"record":{"id":"69865e5481926388","repo":"golangci/golangci-lint","slug":"failed-to-load-packages-w","errorCode":null,"errorMessage":"failed to load packages: %w","messagePattern":"failed to load packages: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/lint/context.go","lineNumber":39,"sourceCode":"\n\tloadGuard *load.Guard\n}\n\nfunc NewContextBuilder(cfg *config.Config, pkgLoader *PackageLoader,\n\tpkgCache *cache.Cache, loadGuard *load.Guard,\n) *ContextBuilder {\n\treturn &ContextBuilder{\n\t\tcfg:       cfg,\n\t\tpkgLoader: pkgLoader,\n\t\tpkgCache:  pkgCache,\n\t\tloadGuard: loadGuard,\n\t}\n}\n\nfunc (cl *ContextBuilder) Build(ctx context.Context, log logutils.Log, linters []*linter.Config) (*linter.Context, error) {\n\tpkgs, deduplicatedPkgs, err := cl.pkgLoader.Load(ctx, linters)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to load packages: %w\", err)\n\t}\n\n\tif len(deduplicatedPkgs) == 0 {\n\t\treturn nil, fmt.Errorf(\"%w: running `go mod tidy` may solve the problem\", exitcodes.ErrNoGoFiles)\n\t}\n\n\tret := &linter.Context{\n\t\tPackages: deduplicatedPkgs,\n\n\t\t// At least `unused` linters works properly only on original (not deduplicated) packages,\n\t\t// see https://github.com/golangci/golangci-lint/pull/585.\n\t\tOriginalPackages: pkgs,\n\n\t\tCfg:       cl.cfg,\n\t\tLog:       log,\n\t\tPkgCache:  cl.pkgCache,\n\t\tLoadGuard: cl.loadGuard,\n\t}","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/lint/context.go#L21-L57","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run `go build ./...` (or `go vet ./...`) to see the underlying compile/type error and fix it first.","Run `go mod tidy` to resolve missing or inconsistent module requirements.","Check the `run.build-tags`, `run.skip-dirs`, and packages patterns in config for typos or tags that exclude/break loading.","Ensure the installed Go version satisfies the go directive in go.mod.","Read the wrapped cause (%w) after this message for the exact package-load failure."],"exampleFix":"// before: module deps missing, loader fails\n// terminal\nlint ./...\n# failed to load packages: no required module provides package \"github.com/x/y\"\n\n// after\ngo mod tidy\nlint ./...","handlingStrategy":"try-catch","validationCode":"out, err := exec.Command(\"go\", \"build\", \"./...\").CombinedOutput()\nif err != nil {\n    return fmt.Errorf(\"project does not build, skipping lint: %s\", out)\n}","typeGuard":null,"tryCatchPattern":"ctx, err := builder.Build(ctx, log, linters)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to load packages\") {\n        log.Errorf(\"package load failed: %v\", err) // inspect wrapped cause\n        return err\n    }\n    return err\n}","preventionTips":["Keep the module compiling (`go build ./...` green) before linting.","Run `go mod tidy` after dependency changes.","Keep build-tags and skip-dirs config in sync with your source layout."],"tags":["go","packages","build","static-analysis"],"backgroundTag":"package-load-failed","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}