{"record":{"id":"0f99e77c78b389fb","repo":"golangci/golangci-lint","slug":"failed-to-load-packages-w-0f99e7","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/package.go","lineNumber":60,"sourceCode":"func NewPackageLoader(log logutils.Log, cfg *config.Config, args []string, env *goutil.Env, loadGuard *load.Guard) *PackageLoader {\n\treturn &PackageLoader{\n\t\tcfg:         cfg,\n\t\targs:        args,\n\t\tlog:         log,\n\t\tdebugf:      logutils.Debug(logutils.DebugKeyLoader),\n\t\tgoenv:       env,\n\t\tpkgTestIDRe: regexp.MustCompile(`^(.*) \\[(.*)\\.test\\]`),\n\t\tloadGuard:   loadGuard,\n\t}\n}\n\n// Load loads packages.\nfunc (l *PackageLoader) Load(ctx context.Context, linters []*linter.Config) (pkgs, deduplicatedPkgs []*packages.Package, err error) {\n\tloadMode := findLoadMode(linters)\n\n\tpkgs, err = l.loadPackages(ctx, loadMode)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"failed to load packages: %w\", err)\n\t}\n\n\treturn pkgs, l.filterDuplicatePackages(pkgs), nil\n}\n\nfunc (l *PackageLoader) loadPackages(ctx context.Context, loadMode packages.LoadMode) ([]*packages.Package, error) {\n\tdefer func(startedAt time.Time) {\n\t\tl.log.Infof(\"Go packages loading at mode %s took %s\", stringifyLoadMode(loadMode), time.Since(startedAt))\n\t}(time.Now())\n\n\tl.prepareBuildContext()\n\n\tconf := &packages.Config{\n\t\tMode:       loadMode,\n\t\tTests:      l.cfg.Run.AnalyzeTests,\n\t\tContext:    ctx,\n\t\tBuildFlags: l.makeBuildFlags(),\n\t\tLogf:       l.debugf,","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/lint/package.go#L42-L78","documentation":"PackageLoader.Load is the entry point for loading Go packages via go/packages; when the underlying loadPackages call returns any error, it is wrapped as \"failed to load packages\". This is a wrapper error — the real cause is in the wrapped %w chain (toolchain, module resolution, or per-package errors surfaced by parseLoadedPackagesErrors).","triggerScenarios":"Any error returned by PackageLoader.loadPackages: `packages.Load` hard failure, or parse errors including ErrNoGoFiles / \"cannot find package\" conditions detected in parseLoadedPackagesErrors.","commonSituations":"Running golangci-lint in a directory outside a Go module; unresolvable imports because dependencies are not downloadable (network/proxy issues); invalid Go files in the target package; wrong path passed to `./...` style arguments.","solutions":["Read the wrapped cause in the error chain (Go 1.13+ %w) — fix the root error first (e.g. ErrNoGoFiles, module download failure)","Run `go build ./...` in the same directory to confirm the package graph loads with the plain toolchain","Ensure you are inside a valid module (`go.mod` present) and GOFLAGS/GOPROXY allow dependency resolution","Match your Go toolchain version to the one the project requires (go directive in go.mod)"],"exampleFix":"// before (shell)\ncd ./pkg/somepkg && golangci-lint run   # outside module root context\n// after\ncd /path/to/module/root && golangci-lint run ./pkg/somepkg/...","handlingStrategy":"try-catch","validationCode":"// Ensure module context is valid before linting:\n// if [ ! -f go.mod ]; then echo 'not a Go module'; exit 1; fi\n// go build ./... || { echo 'packages do not build; fix before linting'; exit 1; }","typeGuard":null,"tryCatchPattern":"err := golangciLintRun();\nvar loadErr *LoadError\nif errors.As(err, &loadErr) {\n    // inspect errors.Unwrap(loadErr) / errors.Is(err, exitcodes.ErrNoGoFiles)\n    // fix the root package-loading cause before retrying\n}","preventionTips":["Always run `go build ./...` successfully before running golangci-lint in CI","Run golangci-lint from the module root so relative patterns like ./... resolve","Keep `go mod tidy` and `go mod download` as pre-lint steps","Pin the Go toolchain version to the module's go directive"],"tags":["golangci-lint","go-packages","module-loading"],"backgroundTag":"package-load-failed","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}