Description
Summary
Add GOMODCACHE
to control where the module download cache lives. Its default can continue to be GOPATH[0]/pkg/mod
, and the variable would be very similar and consistent with GOCACHE
.
Description
The module download cache has lived in GOPATH[0]/pkg/mod/
since it first appeared. It's understandable why it doesn't live under GOCACHE
, where the build cache is located; builds are generally fast and reliable if one has the source, but downloading a module from the internet isn't nearly as reliable.
I also understand why it was put under GOPATH
; until recently, it was the only persistent directory that Go made use of. That only changed in the last release, with the addition of os.UserConfigDir()+"/go/env"
for go env -w
.
However, there's no way to configure where the module download cache is located. For example, this is useful in CI environments to place the build and module download caches somewhere that's persisted between builds.
The only way to store the download cache elsewhere is to move GOPATH
entirely. This has several disadvantages:
-
GOPATH
contains much more. For many users, it still contains code. For almost everyone, it also contains the installed binaries, unless they've setGOBIN
. It's too big of a knob to just change the location of the module download cache. -
Many environments explicitly set
GOPATH
, such as thegolang:1.13
image, meaning that we can't simplygo env -w GOPATH=/custom/path
just like we could withGOCACHE
. This makes the module download cache harder to deal with than the build cache, for no apparent reason. -
GOPATH
's future is uncertain; it might contain more in the future, or it might go away entirely. Relying on it to set the module download cache location is not a good long-term plan.
This idea first came up in #31283, which was closed by its author. I left a comment there a while ago, but thought it would be better to open a new proposal.