{"record":{"id":"ae012d767f081cc9","repo":"golang/go","slug":"gomodcache-entry-is-relative-must-be-absolute-pat-ae012d","errorCode":null,"errorMessage":"GOMODCACHE entry is relative; must be absolute path: %q.\\n","messagePattern":"GOMODCACHE entry is relative; must be absolute path: %q\\.\\\\n","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modfetch/cache.go","lineNumber":824,"sourceCode":"\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\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tif !fi.IsDir() {","sourceCodeStart":806,"sourceCodeEnd":842,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modfetch/cache.go#L806-L842","documentation":"Thrown by `checkCacheDir` when GOMODCACHE is set but `filepath.IsAbs` returns false — the path is relative rather than absolute. The module cache must be an absolute path because relative paths would resolve differently depending on the working directory, causing cache misses and inconsistent behavior. Note the format string ends with `\\n` (a literal newline in the error message, which is unusual and likely a minor formatting quirk).","triggerScenarios":"Setting GOMODCACHE to a relative path like `./pkg/mod`, `../cache`, or `go-cache`. The `filepath.IsAbs(cfg.GOMODCACHE)` check returns false. A counter `counterErrorsGOMODCACHEEntryRelative` is incremented for telemetry before the error is returned.","commonSituations":"Setting GOMODCACHE in a script or Makefile with a relative path; GOPATH set relatively (if GOMODCACHE derives from it); misconfigured CI with relative cache paths; `.env` files using relative paths for portability that backfire here.","solutions":["Set GOMODCACHE to an absolute path: `export GOMODCACHE=$HOME/go/pkg/mod`","Use `$(pwd)` to absolutize a relative path: `export GOMODCACHE=$(pwd)/go-cache`","Run `go env -w GOMODCACHE=/absolute/path/to/cache` to persist","Check that GOPATH (from which GOMODCACHE may derive) is absolute"],"exampleFix":"# before: relative path\nexport GOMODCACHE=./pkg/mod\n# after: absolute path\nexport GOMODCACHE=$HOME/go/pkg/mod","handlingStrategy":"validation","validationCode":"import (\n    \"os\"\n    \"path/filepath\"\n)\n\nfunc ensureGomodcacheAbsolute() error {\n    c := os.Getenv(\"GOMODCACHE\")\n    if c != \"\" && !filepath.IsAbs(c) {\n        abs, _ := filepath.Abs(c)\n        os.Setenv(\"GOMODCACHE\", abs)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use absolute paths for GOMODCACHE and GOPATH","Use $HOME or $(pwd) prefix to construct absolute paths in scripts","Validate environment in CI setup scripts before running go commands"],"tags":["go","go-mod","cache","environment","configuration","gomodcache","path"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}