Source file src/cmd/go/internal/cache/default.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cache
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"sync"
    12  
    13  	"cmd/go/internal/base"
    14  	"cmd/go/internal/cfg"
    15  )
    16  
    17  // Default returns the default cache to use.
    18  // It never returns nil.
    19  func Default() Cache {
    20  	return initDefaultCacheOnce()
    21  }
    22  
    23  var initDefaultCacheOnce = sync.OnceValue(initDefaultCache)
    24  
    25  // cacheREADME is a message stored in a README in the cache directory.
    26  // Because the cache lives outside the normal Go trees, we leave the
    27  // README as a courtesy to explain where it came from.
    28  const cacheREADME = `This directory holds cached build artifacts from the Go build system.
    29  Run "go clean -cache" if the directory is getting too large.
    30  Run "go clean -fuzzcache" to delete the fuzz cache.
    31  See golang.org to learn more about Go.
    32  `
    33  
    34  // initDefaultCache does the work of finding the default cache
    35  // the first time Default is called.
    36  func initDefaultCache() Cache {
    37  	dir, _ := DefaultDir()
    38  	if dir == "off" {
    39  		if defaultDirErr != nil {
    40  			base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
    41  		}
    42  		base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
    43  	}
    44  	if err := os.MkdirAll(dir, 0777); err != nil {
    45  		base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
    46  	}
    47  	if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
    48  		// Best effort.
    49  		os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
    50  	}
    51  
    52  	diskCache, err := Open(dir)
    53  	if err != nil {
    54  		base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
    55  	}
    56  
    57  	if v := cfg.Getenv("GOCACHEPROG"); v != "" {
    58  		return startCacheProg(v, diskCache)
    59  	}
    60  
    61  	return diskCache
    62  }
    63  
    64  var (
    65  	defaultDirOnce    sync.Once
    66  	defaultDir        string
    67  	defaultDirChanged bool // effective value differs from $GOCACHE
    68  	defaultDirErr     error
    69  )
    70  
    71  // DefaultDir returns the effective GOCACHE setting.
    72  // It returns "off" if the cache is disabled,
    73  // and reports whether the effective value differs from GOCACHE.
    74  func DefaultDir() (string, bool) {
    75  	// Save the result of the first call to DefaultDir for later use in
    76  	// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
    77  	// subprocesses will inherit it, but that means initDefaultCache can't
    78  	// otherwise distinguish between an explicit "off" and a UserCacheDir error.
    79  
    80  	defaultDirOnce.Do(func() {
    81  		defaultDir = cfg.Getenv("GOCACHE")
    82  		if defaultDir != "" {
    83  			defaultDirChanged = true
    84  			if filepath.IsAbs(defaultDir) || defaultDir == "off" {
    85  				return
    86  			}
    87  			defaultDir = "off"
    88  			defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path")
    89  			return
    90  		}
    91  
    92  		// Compute default location.
    93  		dir, err := os.UserCacheDir()
    94  		if err != nil {
    95  			defaultDir = "off"
    96  			defaultDirChanged = true
    97  			defaultDirErr = fmt.Errorf("GOCACHE is not defined and %v", err)
    98  			return
    99  		}
   100  		defaultDir = filepath.Join(dir, "go-build")
   101  	})
   102  
   103  	return defaultDir, defaultDirChanged
   104  }
   105  

View as plain text