dockerimg: mount Go caches for faster Docker race builds
When building sketch binary with race detector in Docker, the system
was downloading all modules and rebuilding from scratch every time,
making repeated builds slow and inefficient.
This adds host Go cache mounting to buildLinuxSketchBinWithDocker by:
- Getting host GOCACHE and GOMODCACHE directories using 'go env'
- Mounting them as volumes in the Docker container:
-v $GOCACHE:/root/.cache/go-build
-v $GOMODCACHE:/go/pkg/mod
The optimization ensures:
- First race build: Downloads modules (same time as before)
- Subsequent race builds: Reuses cached modules (much faster)
- Build artifacts persist between Docker container runs
Adds helper functions getHostGoCacheDir() and getHostGoModCacheDir()
to retrieve cache paths from the host environment, with proper error
handling and path validation.
Includes unit test to verify cache directory detection works correctly
on all platforms where Go is installed.
Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: sd279e56e1574e4e1k
diff --git a/dockerimg/dockerimg_test.go b/dockerimg/dockerimg_test.go
index e69d9d7..b19b6d5 100644
--- a/dockerimg/dockerimg_test.go
+++ b/dockerimg/dockerimg_test.go
@@ -7,6 +7,7 @@
"io/fs"
"net/http"
"os"
+ "path/filepath"
"strings"
"testing"
"testing/fstest"
@@ -225,3 +226,35 @@
}
}
}
+
+func TestGetHostGoCacheDirs(t *testing.T) {
+ ctx := context.Background()
+
+ // Test getHostGoCacheDir
+ goCacheDir, err := getHostGoCacheDir(ctx)
+ if err != nil {
+ t.Fatalf("getHostGoCacheDir failed: %v", err)
+ }
+ if goCacheDir == "" {
+ t.Fatal("getHostGoCacheDir returned empty string")
+ }
+ t.Logf("GOCACHE: %s", goCacheDir)
+
+ // Test getHostGoModCacheDir
+ goModCacheDir, err := getHostGoModCacheDir(ctx)
+ if err != nil {
+ t.Fatalf("getHostGoModCacheDir failed: %v", err)
+ }
+ if goModCacheDir == "" {
+ t.Fatal("getHostGoModCacheDir returned empty string")
+ }
+ t.Logf("GOMODCACHE: %s", goModCacheDir)
+
+ // Both should be absolute paths
+ if !filepath.IsAbs(goCacheDir) {
+ t.Errorf("GOCACHE is not an absolute path: %s", goCacheDir)
+ }
+ if !filepath.IsAbs(goModCacheDir) {
+ t.Errorf("GOMODCACHE is not an absolute path: %s", goModCacheDir)
+ }
+}