@@ -35,10 +35,14 @@ func NewCmdGoCache() *cobra.Command {
35
35
cmd := & cobra.Command {
36
36
Use : "gocache" ,
37
37
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` ." ,
39
39
RunE : func (cmd * cobra.Command , args []string ) error {
40
40
ctx := cmd .Context ()
41
41
42
+ if dir == "" {
43
+ return fmt .Errorf ("missing cache directory" )
44
+ }
45
+
42
46
err := os .MkdirAll (dir , 0755 )
43
47
if err != nil {
44
48
return err
@@ -69,11 +73,22 @@ func NewCmdGoCache() *cobra.Command {
69
73
}
70
74
71
75
func 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
+
72
87
dir , err := os .UserCacheDir ()
73
88
if err != nil {
74
89
return ""
75
90
}
76
- dir = filepath .Join (dir , "depot- go-cache " )
91
+ dir = filepath .Join (dir , "go-build " )
77
92
return dir
78
93
}
79
94
@@ -464,7 +479,7 @@ type DiskCache struct {
464
479
}
465
480
466
481
func (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 )
468
483
ij , err := os .ReadFile (actionFile )
469
484
if err != nil {
470
485
if os .IsNotExist (err ) {
@@ -484,7 +499,8 @@ func (dc *DiskCache) Get(ctx context.Context, actionID string) (outputID, diskPa
484
499
// Protect against malicious non-hex OutputID on disk
485
500
return "" , "" , nil
486
501
}
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
488
504
}
489
505
490
506
func (dc * DiskCache ) OutputFilename (objectID string ) string {
@@ -498,11 +514,12 @@ func (dc *DiskCache) OutputFilename(objectID string) string {
498
514
}
499
515
return ""
500
516
}
501
- return filepath .Join (dc .Dir , fmt .Sprintf ("o-%s" , objectID ))
517
+
518
+ return fileNameOutput (dc .Dir , objectID )
502
519
}
503
520
504
521
func (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 )
506
523
507
524
// Special case empty files; they're both common and easier to do race-free.
508
525
if size == 0 {
@@ -530,7 +547,7 @@ func (dc *DiskCache) Put(ctx context.Context, actionID, objectID string, size in
530
547
if err != nil {
531
548
return "" , err
532
549
}
533
- actionFile := filepath . Join (dc .Dir , fmt . Sprintf ( "a-%s" , actionID ) )
550
+ actionFile := fileNameAction (dc .Dir , actionID )
534
551
if _ , err := writeAtomic (actionFile , bytes .NewReader (ij )); err != nil {
535
552
return "" , err
536
553
}
@@ -558,3 +575,18 @@ func writeAtomic(dest string, r io.Reader) (int64, error) {
558
575
}
559
576
return size , nil
560
577
}
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