How do I find the Go module source cache?

Rob G. picture Rob G. · Aug 29, 2018 · Viewed 14.4k times · Source

I've upgraded a project to Go 1.11 and enabled module support for my project, but it seems that CircleCI is re-downloading the dependencies on every build. I know CircleCI allows caching between rebuilds, so I've looked at the documentation for Go modules, and while it mentions a cache, I can't seem to find where it actually exists.

Where is the source cache for Go modules?

Answer

amcelwee picture amcelwee · Aug 29, 2018

As of the final 1.11 release, the go module cache (used for storing downloaded modules and source code), is in the $GOPATH/pkg/mod location (see the docs here). For clarification, the go build cache (used for storing recent compilation results) is in a different location.

This article, indicated that it's in the $GOPATH/src/mod, but in the timespan of the recent ~40 days, the golang team must have changed that target location. This message thread has some discussion on why the downloaded items ended up in $GOPATH/pkg.

You can also use the go mod download -json command to see the downloaded modules/source metadata and their location on your local disk. Example output below:

$ go mod download -json
go: finding github.com/aws/aws-sdk-go v1.14.5
go: finding github.com/aws/aws-lambda-go v1.2.0
{
    "Path": "github.com/aws/aws-lambda-go",
    "Version": "v1.2.0",
    "Info": "/go/pkg/mod/cache/download/github.com/aws/aws-lambda-go/@v/v1.2.0.info",
    "GoMod": "/go/pkg/mod/cache/download/github.com/aws/aws-lambda-go/@v/v1.2.0.mod",
    "Zip": "/go/pkg/mod/cache/download/github.com/aws/aws-lambda-go/@v/v1.2.0.zip",
    "Dir": "/go/pkg/mod/github.com/aws/[email protected]",
    "Sum": "h1:2f0pbAKMNNhvOkjI9BCrwoeIiduSTlYpD0iKEN1neuQ=",
    "GoModSum": "h1:zUsUQhAUjYzR8AuduJPCfhBuKWUaDbQiPOG+ouzmE1A="
}
{
    "Path": "github.com/aws/aws-sdk-go",
    "Version": "v1.14.5",
    "Info": "/go/pkg/mod/cache/download/github.com/aws/aws-sdk-go/@v/v1.14.5.info",
    "GoMod": "/go/pkg/mod/cache/download/github.com/aws/aws-sdk-go/@v/v1.14.5.mod",
    "Zip": "/go/pkg/mod/cache/download/github.com/aws/aws-sdk-go/@v/v1.14.5.zip",
    "Dir": "/go/pkg/mod/github.com/aws/[email protected]",
    "Sum": "h1:+l1m6QH6LypE2kL0p/G0Oh7ceCv+IVQ1h5UEBt2xjjU=",
    "GoModSum": "h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k="
}

That output is from a build on CircleCI 2.0, using their official circleci/golang:1.11 image. This is a contrived example to show how you would include the restore_cache and save_cache steps for the new golang module cache location:

steps:
    - checkout
    - restore_cache:
       keys:
         - gomod-cache-{{ checksum "go.sum" }}
    - run: go vet ./...
    - save_cache:
        key: gomod-cache-{{ checksum "go.sum" }}
        paths:
          - /go/pkg/mod