@@ -331,14 +331,19 @@ func (b GolangCrossBuilder) Build() error {
331331 hostDir := filepath .Join (build .Default .GOPATH , "pkg" , "mod" )
332332 args = append (args , "-v" , hostDir + ":/go/pkg/mod:ro" )
333333 }
334- // Mount the go build cache into the container.
335- out , err := exec .Command ("go" , "env" , "GOCACHE" ).Output ()
336- if err != nil {
337- return fmt .Errorf ("failed to get GOCACHE: %w" , err )
334+
335+ buildCacheLocation := "/tmp/.cache/go-build"
336+ if CrossBuildMountBuildCache {
337+ // Mount the go build cache into the container.
338+ out , err := exec .Command ("go" , "env" , "GOCACHE" ).Output ()
339+ if err != nil {
340+ return fmt .Errorf ("failed to get GOCACHE: %w" , err )
341+ }
342+ cacheDir := strings .TrimSpace (string (out ))
343+ args = append (args ,
344+ "-v" , fmt .Sprintf ("%s:%s" , cacheDir , buildCacheLocation ),
345+ )
338346 }
339- cacheDir := strings .TrimSpace (string (out ))
340- buildCacheMountPoint := "/tmp/.cache/go-build"
341- args = append (args , "-v" , fmt .Sprintf ("%s:%s" , cacheDir , buildCacheMountPoint ))
342347
343348 // Mount /opt/git-mirrors (if present) to resolve git alternates in CI
344349 if _ , err := os .Stat ("/opt/git-mirrors" ); err == nil {
@@ -356,7 +361,7 @@ func (b GolangCrossBuilder) Build() error {
356361 args = append (args ,
357362 "--rm" ,
358363 "--env" , "GOFLAGS=-mod=readonly" ,
359- "--env" , fmt .Sprintf ("GOCACHE=%s" , buildCacheMountPoint ),
364+ "--env" , fmt .Sprintf ("GOCACHE=%s" , buildCacheLocation ), // ensure this is writable by the user
360365 "--env" , "MAGEFILE_VERBOSE=" + verbose ,
361366 "--env" , "MAGEFILE_TIMEOUT=" + EnvOr ("MAGEFILE_TIMEOUT" , "" ),
362367 "--env" , fmt .Sprintf ("SNAPSHOT=%v" , Snapshot ),
0 commit comments