{"record":{"id":"3fae522c03d57d98","repo":"golangci/golangci-lint","slug":"failed-to-build-packages-cache-w","errorCode":null,"errorMessage":"failed to build packages cache: %w","messagePattern":"failed to build packages cache: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/run.go","lineNumber":203,"sourceCode":"\t}\n\n\tc.dbManager = dbManager\n\n\tc.printer, err = printers.NewPrinter(c.log, &c.cfg.Output.Formats, c.reportData, c.cfg.GetBasePath())\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tc.goenv = goutil.NewEnv(c.log.Child(logutils.DebugKeyGoEnv))\n\n\tc.fileCache = fsutils.NewFileCache()\n\tc.lineCache = fsutils.NewLineCache(c.fileCache)\n\n\tsw := timeutils.NewStopwatch(\"pkgcache\", c.log.Child(logutils.DebugKeyStopwatch))\n\n\tpkgCache, err := cache.NewCache(sw, c.log.Child(logutils.DebugKeyPkgCache))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to build packages cache: %w\", err)\n\t}\n\n\tguard := load.NewGuard()\n\n\tpkgLoader := lint.NewPackageLoader(c.log.Child(logutils.DebugKeyLoader), c.cfg, args, c.goenv, guard)\n\n\tc.contextBuilder = lint.NewContextBuilder(c.cfg, pkgLoader, pkgCache, guard)\n\n\tif err = initHashSalt(c.log.Child(logutils.DebugKeyGoModSalt), c.buildInfo.Version, c.cfg); err != nil {\n\t\treturn fmt.Errorf(\"failed to init hash salt: %w\", err)\n\t}\n\n\tif ok := c.acquireFileLock(); !ok {\n\t\treturn errors.New(\"parallel golangci-lint is running\")\n\t}\n\n\treturn nil\n}","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/golangci/golangci-lint/blob/ed7a235d2d771152056fdc142a9855567c5796a9/pkg/commands/run.go#L185-L221","documentation":"Before linting, golangci-lint builds an on-disk packages cache (`cache.NewCache`). If cache initialization fails — typically because the user cache directory cannot be determined or created — the error is wrapped as `failed to build packages cache: %w`. Without this cache the run cannot proceed.","triggerScenarios":"`cache.NewCache` failing because the OS cache directory (e.g. $XDG_CACHE_HOME, ~/.cache, %LocalAppData%) is unset and home is undiscoverable, unwritable, or on a full/read-only filesystem.","commonSituations":"CI containers running as non-root without a writable HOME; XDG_CACHE_HOME pointing to a non-existent dir; read-only root filesystems in Docker; NFS-mounted homes with permission issues; `HOME` unset in cron/systemd jobs.","solutions":["Set a writable cache dir: export XDG_CACHE_HOME (or HOME) to an existing writable path.","Create the cache directory: mkdir -p ~/.cache/golangci-lint and fix permissions with chown/chmod.","Run the container with a writable volume mounted at the cache location (e.g. -v gocache:/root/.cache).","Check disk space if the error indicates a full filesystem.","Inspect the wrapped error after the message for the precise OS-level cause."],"exampleFix":"// before (CI step)\n- run: golangci-lint run   # HOME is unset in container\n// after\n- run: |\n    export HOME=/tmp && mkdir -p $HOME/.cache\n    golangci-lint run","handlingStrategy":"validation","validationCode":"// ensure a writable cache dir exists before running\ncacheDir := os.Getenv(\"XDG_CACHE_HOME\")\nif cacheDir == \"\" {\n    home, err := os.UserHomeDir()\n    if err != nil { return err }\n    cacheDir = filepath.Join(home, \".cache\")\n}\nif err := os.MkdirAll(filepath.Join(cacheDir, \"golangci-lint\"), 0o755); err != nil {\n    return fmt.Errorf(\"cache dir not usable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if _, err := cache.NewCache(sw, log); err != nil {\n    return fmt.Errorf(\"failed to build packages cache: %w\", err) // check HOME/XDG_CACHE_HOME\n}","preventionTips":["Set HOME or XDG_CACHE_HOME explicitly in CI containers running as non-root.","Mount a writable volume at the cache location in Docker.","Create ~/.cache/golangci-lint with correct ownership in setup steps.","Monitor disk space on runners."],"tags":["go","cache","filesystem","ci"],"backgroundTag":"cache-directory-unwritable","analyzedSha":"ed7a235d2d771152056fdc142a9855567c5796a9","analyzedAt":"2026-09-02T18:47:33.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}