{"record":{"id":"3947f7e0eaa0a8da","repo":"golang/go","slug":"could-not-create-module-cache-w","errorCode":null,"errorMessage":"could not create module cache: %w","messagePattern":"could not create module cache: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modfetch/cache.go","lineNumber":833,"sourceCode":"// error is returned if it does not.\nfunc checkCacheDir(ctx context.Context) error {\n\tif cfg.GOMODCACHE == \"\" {\n\t\t// modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE\n\t\t// is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.\n\t\treturn fmt.Errorf(\"module cache not found: neither GOMODCACHE nor GOPATH is set\")\n\t}\n\tif !filepath.IsAbs(cfg.GOMODCACHE) {\n\t\tcounterErrorsGOMODCACHEEntryRelative.Inc()\n\t\treturn fmt.Errorf(\"GOMODCACHE entry is relative; must be absolute path: %q.\\n\", cfg.GOMODCACHE)\n\t}\n\n\t// os.Stat is slow on Windows, so we only call it once to prevent unnecessary\n\t// I/O every time this function is called.\n\tstatCacheOnce.Do(func() {\n\t\tfi, err := os.Stat(cfg.GOMODCACHE)\n\t\tif err != nil {\n\t\t\tif !os.IsNotExist(err) {\n\t\t\t\tstatCacheErr = fmt.Errorf(\"could not create module cache: %w\", err)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tif err := os.MkdirAll(cfg.GOMODCACHE, 0o777); err != nil {\n\t\t\t\tstatCacheErr = fmt.Errorf(\"could not create module cache: %w\", err)\n\t\t\t\treturn\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif !fi.IsDir() {\n\t\t\tstatCacheErr = fmt.Errorf(\"could not create module cache: %q is not a directory\", cfg.GOMODCACHE)\n\t\t\treturn\n\t\t}\n\t})\n\treturn statCacheErr\n}\n","sourceCodeStart":815,"sourceCodeEnd":849,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modfetch/cache.go#L815-L849","documentation":"Thrown inside checkCacheDir when os.Stat(GOMODCACHE) fails with an error that is NOT 'file does not exist' — meaning a permission error, I/O error, or path error. The %w wraps the underlying OS error so the root cause (e.g. 'permission denied') is visible. This runs inside a sync.Once so the check only happens once per process.","triggerScenarios":"Calling any go command that touches the module cache (go build, go mod download, go get) when the GOMODCACHE path is inaccessible: parent directory lacks execute permission, the path crosses a broken symlink, or the filesystem returns an I/O error.","commonSituations":"GOMODCACHE points to a directory whose parent has restrictive permissions (e.g. owned by root), a NFS mount that went stale, a docker volume mounted read-only, or a broken symlink in the path.","solutions":["Run 'go env GOMODCACHE' to see the current path, then 'ls -la' on the parent directory to check permissions.","Fix ownership/permissions: 'sudo chown -R $(whoami) <parent_dir>' or 'chmod +x <parent_dir>'.","If the path is on a stale or unmounted filesystem, remount or re-point GOMODCACHE with 'go env -w GOMODCACHE=/new/abs/path'.","Check for broken symlinks in the path with 'readlink -f <GOMODCACHE>'."],"exampleFix":"# before: GOMODCACHE parent owned by root\nsudo mkdir -p /root/.cache/go-build\n# after\nsudo chown -R $USER:$USER $(dirname $(go env GOMODCACHE))\n# or set a writable location\ngo env -w GOMODCACHE=$HOME/go/pkg/mod","handlingStrategy":"validation","validationCode":"// Before invoking go commands, verify GOMODCACHE is stat-able\nimport (\n    \"os\"\n    \"fmt\"\n    \"path/filepath\"\n)\n\nfunc validateGOMODCACHE(gomodcache string) error {\n    if gomodcache == \"\" {\n        return fmt.Errorf(\"GOMODCACHE is empty\")\n    }\n    if !filepath.IsAbs(gomodcache) {\n        return fmt.Errorf(\"GOMODCACHE must be absolute: %q\", gomodcache)\n    }\n    if _, err := os.Stat(gomodcache); err != nil && !os.IsNotExist(err) {\n        return fmt.Errorf(\"GOMODCACHE stat failed (non-NotExist): %w\", err)\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Set GOMODCACHE to an absolute, user-owned path via 'go env -w GOMODCACHE=$HOME/go/pkg/mod'.","In CI, create and chown the cache directory before running go commands.","Avoid pointing GOMODCACHE at network filesystems; prefer local disk.","Add a startup check in build scripts that os.Stat's the cache parent and fails fast with a clear message."],"tags":["filesystem","permissions","gomodcache","config"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}