> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trunk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Go build cache

> Use the Trunk shared cache for Go build results. Use it in Docker steps and in golangci-lint too.

The Go cache keeps the results of Go commands in the Trunk shared cache. This
includes `go build`, `go test` and `go vet`. A later job reads these results.
Then Go compiles only the packages that changed.

## Setup

The Go cache is **off** by default, because it changes how `go` runs. An admin
sets it to on in the **Shared cache** tab on the **Runners** page of the Trunk
web app. The admin can set it for the organization or for one repository. See
[Cache settings](https://docs.trunk.io/runners/cache/settings.md).

> [!NOTE]
>
> You must use **Go 1.24 or later**. An older Go does not use the shared cache.
> It uses only its local build cache.

## Environment variables and log lines

When the Go cache is on, the runner starts with these environment variables:

| Variable | Value |
| - | - |
| `GOCACHEPROG` | `/usr/local/bin/kiln-gocacheprog`. This is the Trunk cache helper. Go starts it for each `go` command. The helper reads and writes the cache entries. |
| `TRUNK_GOCACHEPROG_URL` | The address of the cache for this job. The address is correct only for this job. |

You can also set this variable:

| Variable | Use |
| - | - |
| `TRUNK_GOCACHEPROG_DIR` | The local directory of the helper. The default is `${XDG_CACHE_HOME:-$HOME/.cache}/trunk-gocacheprog`. The helper reads this directory first. Then it reads the shared cache. |

At the start of the job, the job log shows one of these lines:

* `trunk-ci: the Go cache is set up for steps on the runner host; a container step or a Docker action builds without it.`
* `trunk-ci: the Go cache helper is not on this runner; this job builds without the Go cache.`

After each `go` command, the helper writes one summary line to stderr:

```text theme={"system"}
trunk-go-cache: gets: 6706 local hits, 3825 remote hits, 488 misses; puts: 434; uploads: 434 done, 0 dropped
```

* A `local hit` is an entry from the local directory.
* A `remote hit` is an entry from the shared cache.
* A `put` is an entry that Go wrote.
* An `upload` sends a `put` to the shared cache.

## Steps that use the cache

| Where the Go command runs | Does it use the Go cache? |
| - | - |
| A step on the runner host | Yes. |
| A job in a container (`container:`) | No. It uses only its local cache. |
| A Docker action | No. It uses only its local cache. |
| Your own `docker run` in a step | Only when you give it the helper. See [Docker steps](#docker-steps). |

Trunk sets the variables in the environment of the runner, not in
`$GITHUB_ENV`. Thus a container step does not get a `GOCACHEPROG` that it
cannot start.

> [!WARNING]
>
> Do not write `GOCACHEPROG` to `$GITHUB_ENV`. In a container step, `go` stops
> when it cannot start the helper.

To run one step without the cache, set an empty value for that step:

```yaml theme={"system"}
- run: go build ./...
  env:
    GOCACHEPROG: ""
```

## Failure behavior

A cache problem does not stop the build.

* When a read fails, is too slow or finds no entry, it is a miss. Go then
  compiles the package.
* When an upload fails, the helper drops it.
* After three failures in sequence, the helper stops reads or uploads for the
  remaining time of that `go` command.
* A read waits for a maximum of 10 seconds.
* The helper does not upload an entry that is larger than 2 GiB.

## Docker steps

A Go build in your own container can also use the cache. Put the helper in the
container, and give the container the cache address:

```bash theme={"system"}
helper="$(readlink -f "$GOCACHEPROG")"
# The cache listener on the runner uses two addresses: 127.0.0.1 and 172.17.0.1.
# 172.17.0.1 is the Docker bridge. A container can use only this address.
url="${TRUNK_GOCACHEPROG_URL/127.0.0.1/172.17.0.1}"
mkdir -p "$RUNNER_TEMP/go-cache-local"

docker run --rm \
  --volume "$helper:/usr/local/bin/trunk-go-cache:ro" \
  --volume "$RUNNER_TEMP/go-cache-local:/go-cache-local" \
  --env GOCACHEPROG=/usr/local/bin/trunk-go-cache \
  --env TRUNK_GOCACHEPROG_URL="$url" \
  --env TRUNK_GOCACHEPROG_DIR=/go-cache-local \
  --volume "$PWD:$PWD" --workdir "$PWD" \
  my-go-image go build ./...
```

* The helper is a static binary. It runs in all Linux images, Alpine too.
* Put the real file in the container (`readlink -f`). `$GOCACHEPROG` can be a
  symbolic link.
* The cache address has no credential. The runner adds the credential. Thus
  the container gets no secret.
* When `TRUNK_GOCACHEPROG_URL` starts with `https://`, do not change it.

## Helper output on stderr

The helper writes its `trunk-go-cache:` lines to stderr, and `go` sends them on.
Some tools read each stderr line of `go test` as an error. For example,
gotestsum does this. For these tools, use a small wrapper script. The wrapper
writes the lines to a file. Set the wrapper as `GOCACHEPROG` for that step only:

```yaml theme={"system"}
- name: Test
  run: |
    wrapper="$RUNNER_TEMP/trunk-go-cache.sh"
    printf '#!/bin/sh\nexec /usr/local/bin/kiln-gocacheprog "$@" 2>>"%s"\n' \
      "$RUNNER_TEMP/trunk-go-cache.log" > "$wrapper"
    chmod +x "$wrapper"
    GOCACHEPROG="$wrapper" gotestsum ./...
    cat "$RUNNER_TEMP/trunk-go-cache.log"
```

## golangci-lint

golangci-lint can also keep its analysis cache in the shared cache. It reads
`GOLANGCI_LINT_CACHEPROG`. This variable uses the same protocol as
`GOCACHEPROG`.

```yaml theme={"system"}
- name: Lint
  run: |
    wrapper="$RUNNER_TEMP/golangci-lint-cache.sh"
    mkdir -p "$RUNNER_TEMP/golangci-lint-cache"
    printf '#!/bin/sh\nexport TRUNK_GOCACHEPROG_DIR=%s\nexec %s "$@"\n' \
      "$RUNNER_TEMP/golangci-lint-cache" "$GOCACHEPROG" > "$wrapper"
    chmod +x "$wrapper"
    GOLANGCI_LINT_CACHEPROG="$wrapper" golangci-lint run
```

Give golangci-lint its own local directory, as in the example. Then its
entries and the Go build entries stay in different directories.

### Key prefixes

The cache uses all the text after `/cache/v1/go/` in the address as the key of
the entry. Thus, when you add a path segment to `TRUNK_GOCACHEPROG_URL`, you
get a different set of keys. Use this when the key of the tool does not
include an input that changes the result.

For example, the key of golangci-lint includes the path of the
[ruleguard](https://github.com/quasilyte/go-ruleguard) rules file. It does not
include the contents of that file. Thus, after you change a rule,
golangci-lint can use a result from before the change. This result can come
from a different branch. Put a hash of the rules in the address to prevent
this:

```bash theme={"system"}
rules="$(git ls-files -s -- ruleguard | sha256sum | cut -c1-16)"
printf '#!/bin/sh\nexport TRUNK_GOCACHEPROG_DIR=%s\nexport TRUNK_GOCACHEPROG_URL="$TRUNK_GOCACHEPROG_URL/ruleguard-%s"\nexec %s "$@"\n' \
  "$RUNNER_TEMP/golangci-lint-cache/ruleguard-$rules" "$rules" "$GOCACHEPROG" > "$wrapper"
```

A key can have a maximum of 1,024 characters.

## Behavior and limits

* **Some misses occur in each job.** Go keeps an index of each package
  directory of your module in the build cache. The key of the index includes
  the time when each file changed. A new checkout gives each file a new time.
  Thus these entries miss in each job, and the helper uploads them again. Go
  makes them again in a few milliseconds. When a job changes little, these
  entries are frequently most of the misses.
* **A change to an exported API makes Go compile the importers again.** A change
  can alter the exported types, the functions or the inlinable code of a
  package. Then all the packages that import it get new keys. The first job
  that builds that version compiles them. A change in the body of a function
  does not have an effect on the importers.
* **The Go cache does not keep modules.** `GOMODCACHE` stays on the runner. To
  keep the downloaded modules for later jobs, put them in a
  [template](https://docs.trunk.io/runners/actions/templates.md), or use `actions/cache`.
* **Branches.** By default, a job reads entries from its own branch and from
  the default branch. It writes only to its own branch. An admin can let a job
  also read entries from other branches. See
  [Cache settings](https://docs.trunk.io/runners/cache/settings.md#which-branches-a-job-uses-cache-from).
* **Storage.** The Go cache follows the
  [cache storage policy](https://docs.trunk.io/runners/cache.md#cache-storage-policy). When a
  repository is more than its limit, Trunk removes the entries that jobs used
  least recently.
