From bc7ef8240bbbf70416795bd135e23c1a051c4ae5 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Tue, 3 Nov 2026 12:20:48 +0100 Subject: [PATCH] Keep Go's caches out of the test stage's image (closes #256) The test stage ran go mod download in its own layer, and go test left Go's build cache in another. Exporting them took about 20 seconds of every make test, and nothing reads either one afterwards. Removing the build cache alone saved almost nothing, because the module cache is the larger layer. So the download now runs in the same RUN as go test, and go clean removes both caches once the tests pass. A failing run still reruns with -v and exits 1. Model: opus-5-5 --- Dockerfile | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index d8fae5c..0e22ff2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,14 +33,18 @@ RUN golangci-lint run --config .golangci.yml ./... # golang:1.24.13-bookworm, 2026-10-06 FROM golang@sha256:1a6d4452c65dea36aac2e2d606b01b4a029ec90cc1ae53890540ce6173ea77ac AS test WORKDIR /src -COPY go.mod go.sum ./ -RUN go mod download COPY . . RUN mkdir -p web/dist && touch web/dist/index.html web/dist/style.css web/dist/app.js # -p 4 because test runs on a shared build host cap their parallelism. -RUN go test -p 4 -timeout 90s -race -cover ./... || \ +# The modules download in this RUN rather than a layer of their own, because +# exporting a layer that holds them is slow. Nothing reads them or the build +# cache afterwards, so go clean removes both. +RUN set -e; \ + go mod download; \ + go test -p 4 -timeout 90s -race -cover ./... || \ { echo "--- Rerunning with -v for details ---"; \ - go test -p 4 -timeout 90s -race -v ./...; exit 1; } + go test -p 4 -timeout 90s -race -v ./...; exit 1; }; \ + go clean -cache -modcache # Build stage. Nothing is wanted from either phase above; the copies # are what make BuildKit build them first, so this stage cannot run