{"record":{"id":"fd7a9334d6b8deb4","repo":"golang/go","slug":"module-cache-not-found-neither-gomodcache-nor-gop","errorCode":null,"errorMessage":"module cache not found: neither GOMODCACHE nor GOPATH is set","messagePattern":"module cache not found: neither GOMODCACHE nor GOPATH is set","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modfetch/cache.go","lineNumber":820,"sourceCode":"\t}\n\n\treturn nil\n}\n\nvar (\n\tstatCacheOnce sync.Once\n\tstatCacheErr  error\n\n\tcounterErrorsGOMODCACHEEntryRelative = counter.New(\"go/errors:gomodcache-entry-relative\")\n)\n\n// checkCacheDir checks if the directory specified by GOMODCACHE exists. An\n// 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","sourceCodeStart":802,"sourceCodeEnd":838,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modfetch/cache.go#L802-L838","documentation":"Thrown by `checkCacheDir` when `cfg.GOMODCACHE` is empty. The comment notes that modload.Init normally sets GOMODCACHE from GOPATH[0]/pkg/mod, so this error indicates a severely misconfigured environment where neither GOMODCACHE nor GOPATH is set. Without a cache directory, the module system cannot download, store, or verify modules.","triggerScenarios":"Any go command that touches the module cache when cfg.GOMODCACHE is empty string. This happens when GOPATH is unset/empty (modload.Init would normally set GOMODCACHE from it) and GOMODCACHE is explicitly empty. The check `if cfg.GOMODCACHE == \"\"` triggers.","commonSituations":"GOPATH and GOMODCACHE both unset in a minimal environment (e.g., fresh container, CI without Go env setup); GOPATH set to empty string explicitly; broken go installation where `go env` returns empty values; environment variable stripped in sandboxed builds.","solutions":["Set GOPATH: `export GOPATH=$HOME/go` (GOMODCACHE derives from it)","Set GOMODCACHE directly: `export GOMODCACHE=$HOME/go/pkg/mod`","Run `go env -w GOPATH=$HOME/go` to persist the setting","Ensure `go env GOROOT` is valid; a broken install can produce empty env values"],"exampleFix":"# before: neither set\ngo mod download  # fails\n# after\nexport GOPATH=$HOME/go\ngo mod download","handlingStrategy":"validation","validationCode":"// Ensure module cache environment is configured\nimport (\n    \"os\"\n    \"path/filepath\"\n)\n\nfunc ensureModCacheConfigured() error {\n    gomodcache := os.Getenv(\"GOMODCACHE\")\n    if gomodcache == \"\" {\n        gopath := os.Getenv(\"GOPATH\")\n        if gopath == \"\" {\n            home, _ := os.UserHomeDir()\n            gopath = filepath.Join(home, \"go\")\n        }\n        gomodcache = filepath.Join(gopath, \"pkg\", \"mod\")\n        os.Setenv(\"GOMODCACHE\", gomodcache)\n    }\n    if !filepath.IsAbs(gomodcache) {\n        return fmt.Errorf(\"GOMODCACHE must be absolute: %q\", gomodcache)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set GOPATH or GOMODCACHE in shell profile (.bashrc, .zshrc) for persistence","Use `go env -w GOPATH=...` for permanent configuration","In CI/Docker, explicitly set GOPATH in the environment"],"tags":["go","go-mod","cache","environment","configuration","gomodcache"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}