{"record":{"id":"8424335022e14c4d","repo":"golang/go","slug":"could-not-create-module-cache-q-is-not-a-directo","errorCode":null,"errorMessage":"could not create module cache: %q is not a directory","messagePattern":"could not create module cache: %q is not a directory","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modfetch/cache.go","lineNumber":843,"sourceCode":"\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":825,"sourceCodeEnd":849,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modfetch/cache.go#L825-L849","documentation":"Thrown inside checkCacheDir when os.Stat succeeds (the path exists) but fi.IsDir() is false — the GOMODCACHE path resolves to a regular file, symlink-to-file, or device, not a directory. The module cache must be a directory.","triggerScenarios":"Running any module-aware go command when GOMODCACHE points at an existing file. Common after someone accidentally writes output to the cache path, or a misconfigured tool creates a file where a directory is expected.","commonSituations":"A stray 'go env -w GOMODCACHE=~/go' when ~/go was previously a file; a CI script that redirects output to the cache path; a symlink pointing at a file.","solutions":["Identify what the path currently is: 'file $(go env GOMODCACHE)'.","Remove or move the offending file: 'rm $(go env GOMODCACHE)'.","Retry the go command — it will auto-create the directory.","If the file was created by mistake, investigate which tool/script wrote it."],"exampleFix":"# before: GOMODCACHE is a file\n$ file $(go env GOMODCACHE)\n/home/user/go/pkg/mod: ASCII text\n# after\nrm $(go env GOMODCACHE)\ngo build ./...","handlingStrategy":"validation","validationCode":"func validateGOMODCACHEIsDir(gomodcache string) error {\n    fi, err := os.Stat(gomodcache)\n    if os.IsNotExist(err) {\n        return nil // will be created\n    }\n    if err != nil {\n        return err\n    }\n    if !fi.IsDir() {\n        return fmt.Errorf(\"GOMODCACHE %q exists but is not a directory\", gomodcache)\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Never redirect file output to the GOMODCACHE path.","In setup scripts, os.Stat the path and assert IsDir before running go.","Use a dedicated, predictable cache path that no other tool writes to.","Document the cache location for your team to prevent accidental file creation."],"tags":["filesystem","gomodcache","config","corruption"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}