@@ -35,10 +35,14 @@ func NewCmdGoCache() *cobra.Command {
3535 cmd := & cobra.Command {
3636 Use : "gocache" ,
3737 Short : `Go compiler remote cache using Depot. To use set GOCACHEPROG="depot gocache"` ,
38- Long : "depot gocache implements the Go compiler external cache protocol. It communicates over stdin/stdout with the Go tool cache." ,
38+ Long : "depot gocache implements the Go compiler external cache protocol.\n To clean the cache use `go clean - cache` ." ,
3939 RunE : func (cmd * cobra.Command , args []string ) error {
4040 ctx := cmd .Context ()
4141
42+ if dir == "" {
43+ return fmt .Errorf ("missing cache directory" )
44+ }
45+
4246 err := os .MkdirAll (dir , 0755 )
4347 if err != nil {
4448 return err
@@ -69,11 +73,22 @@ func NewCmdGoCache() *cobra.Command {
6973}
7074
7175func defaultCacheDir () string {
76+ dir := os .Getenv ("GOCACHE" )
77+ if dir != "" {
78+ if dir == "off" {
79+ return ""
80+ } else if ! filepath .IsAbs (dir ) {
81+ return ""
82+ } else {
83+ return dir
84+ }
85+ }
86+
7287 dir , err := os .UserCacheDir ()
7388 if err != nil {
7489 return ""
7590 }
76- dir = filepath .Join (dir , "depot- go-cache " )
91+ dir = filepath .Join (dir , "go-build " )
7792 return dir
7893}
7994
@@ -464,7 +479,7 @@ type DiskCache struct {
464479}
465480
466481func (dc * DiskCache ) Get (ctx context.Context , actionID string ) (outputID , diskPath string , err error ) {
467- actionFile := filepath . Join (dc .Dir , fmt . Sprintf ( "a-%s" , actionID ) )
482+ actionFile := fileNameAction (dc .Dir , actionID )
468483 ij , err := os .ReadFile (actionFile )
469484 if err != nil {
470485 if os .IsNotExist (err ) {
@@ -484,7 +499,8 @@ func (dc *DiskCache) Get(ctx context.Context, actionID string) (outputID, diskPa
484499 // Protect against malicious non-hex OutputID on disk
485500 return "" , "" , nil
486501 }
487- return ie .OutputID , filepath .Join (dc .Dir , fmt .Sprintf ("o-%v" , ie .OutputID )), nil
502+ outputFile := fileNameOutput (dc .Dir , ie .OutputID )
503+ return ie .OutputID , outputFile , nil
488504}
489505
490506func (dc * DiskCache ) OutputFilename (objectID string ) string {
@@ -498,11 +514,12 @@ func (dc *DiskCache) OutputFilename(objectID string) string {
498514 }
499515 return ""
500516 }
501- return filepath .Join (dc .Dir , fmt .Sprintf ("o-%s" , objectID ))
517+
518+ return fileNameOutput (dc .Dir , objectID )
502519}
503520
504521func (dc * DiskCache ) Put (ctx context.Context , actionID , objectID string , size int64 , body io.Reader ) (diskPath string , _ error ) {
505- file := filepath . Join (dc .Dir , fmt . Sprintf ( "o-%s" , objectID ) )
522+ file := fileNameOutput (dc .Dir , objectID )
506523
507524 // Special case empty files; they're both common and easier to do race-free.
508525 if size == 0 {
@@ -530,7 +547,7 @@ func (dc *DiskCache) Put(ctx context.Context, actionID, objectID string, size in
530547 if err != nil {
531548 return "" , err
532549 }
533- actionFile := filepath . Join (dc .Dir , fmt . Sprintf ( "a-%s" , actionID ) )
550+ actionFile := fileNameAction (dc .Dir , actionID )
534551 if _ , err := writeAtomic (actionFile , bytes .NewReader (ij )); err != nil {
535552 return "" , err
536553 }
@@ -558,3 +575,18 @@ func writeAtomic(dest string, r io.Reader) (int64, error) {
558575 }
559576 return size , nil
560577}
578+
579+ // fileName is very similar to the Go compiler's file name.
580+ // This allows us to use the same `go clean -cache` command to clean up the cache.
581+ // We prefix "depot-" to the filename to avoid conflicts with the Go compiler's cache.
582+ func fileName (dir , id , key string ) string {
583+ return filepath .Join (dir , id [:2 ], "depot-" + id + "-" + key )
584+ }
585+
586+ func fileNameAction (dir , actionID string ) string {
587+ return fileName (dir , actionID , "a" )
588+ }
589+
590+ func fileNameOutput (dir , outputID string ) string {
591+ return fileName (dir , outputID , "d" )
592+ }
0 commit comments