{"record":{"id":"bfe6fd5bfe3189a7","repo":"golang/go","slug":"gocache-is-not-defined-and-v","errorCode":null,"errorMessage":"GOCACHE is not defined and %v","messagePattern":"GOCACHE is not defined and (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/cache/default.go","lineNumber":85,"sourceCode":"\tdefaultDirChanged bool // effective value differs from $GOCACHE\n\tdefaultDirErr     error\n)\n\n// DefaultDir returns the effective GOCACHE setting.\n// It returns \"off\" if the cache is disabled,\n// and reports whether the effective value differs from GOCACHE.\nfunc DefaultDir() (string, bool, error) {\n\t// Save the result of the first call to DefaultDir for later use in\n\t// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that\n\t// subprocesses will inherit it, but that means initDefaultCache can't\n\t// otherwise distinguish between an explicit \"off\" and a UserCacheDir error.\n\n\tdefaultDirOnce.Do(func() {\n\t\t// Compute default location.\n\t\tdir, err := os.UserCacheDir()\n\t\tif err != nil {\n\t\t\tdefaultDir = \"off\"\n\t\t\tdefaultDirErr = fmt.Errorf(\"GOCACHE is not defined and %v\", err)\n\t\t} else {\n\t\t\tdefaultDir = filepath.Join(dir, \"go-build\")\n\t\t}\n\n\t\tnewDir := cfg.Getenv(\"GOCACHE\")\n\t\tif newDir != \"\" {\n\t\t\tdefaultDirErr = nil\n\t\t\tdefaultDirChanged = newDir != defaultDir\n\t\t\tdefaultDir = newDir\n\t\t\tif filepath.IsAbs(defaultDir) || defaultDir == \"off\" {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tdefaultDir = \"off\"\n\t\t\tdefaultDirErr = fmt.Errorf(\"GOCACHE is not an absolute path\")\n\t\t\treturn\n\t\t}\n\t})\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/cache/default.go#L67-L103","documentation":"Returned by cache.DefaultDir() when GOCACHE is unset AND os.UserCacheDir() fails. With no default location available the cache is forced to \"off\" and the error records the underlying UserCacheDir failure. Subsequent build caching is disabled, so the user sees slow no-cache builds.","triggerScenarios":"GOCACHE env var unset and the platform cannot derive a user cache directory (HOME unset on Unix, or XDG_CACHE_HOME misconfigured; no home dir on a minimal container/service account).","commonSituations":"Docker/OCI images running as nobody without HOME; CI runners with stripped env; systemd services lacking Environment=HOME=; chroot/sandbox without passwd lookup.","solutions":["Set HOME (or XDG_CACHE_HOME) so os.UserCacheDir() succeeds.","Set GOCACHE explicitly to an absolute directory the process can write.","If caching is intentionally off, set GOCACHE=off to silence the ambiguity.","For containers, add `ENV HOME=/tmp` or `ENV GOCACHE=/tmp/go-build`."],"exampleFix":"# before: container with no HOME\n$ go build .  # GOCACHE is not defined and $HOME is not defined\n\n# after\n$ export HOME=/tmp\n$ go build .","handlingStrategy":"validation","validationCode":"if os.Getenv(\"GOCACHE\") == \"\" {\n    if _, err := os.UserCacheDir(); err != nil {\n        // set a writable cache before invoking the toolchain\n        _ = os.Setenv(\"GOCACHE\", filepath.Join(os.TempDir(), \"go-build\"))\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set HOME (and ideally GOCACHE) in containers/service units.","Validate env in CI with `go env GOCACHE` returning a real path.","Run as a user with a resolvable home directory."],"tags":["cache","configuration","environment","gocache"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}