...

Source file src/github.com/rogpeppe/go-internal/cache/default.go

Documentation: github.com/rogpeppe/go-internal/cache

     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  	"log"
    10  	"os"
    11  	"path/filepath"
    12  	"sync"
    13  )
    14  
    15  // Default returns the default cache to use.
    16  // It never returns nil.
    17  func Default() *Cache {
    18  	defaultOnce.Do(initDefaultCache)
    19  	return defaultCache
    20  }
    21  
    22  var (
    23  	defaultOnce  sync.Once
    24  	defaultCache *Cache
    25  )
    26  
    27  // cacheREADME is a message stored in a README in the cache directory.
    28  // Because the cache lives outside the normal Go trees, we leave the
    29  // README as a courtesy to explain where it came from.
    30  const cacheREADME = `This directory holds cached build artifacts from the Go build system.
    31  Run "go clean -cache" if the directory is getting too large.
    32  Run "go clean -fuzzcache" to delete the fuzz cache.
    33  See golang.org to learn more about Go.
    34  `
    35  
    36  // initDefaultCache does the work of finding the default cache
    37  // the first time Default is called.
    38  func initDefaultCache() {
    39  	dir := DefaultDir()
    40  	if dir == "off" {
    41  		if defaultDirErr != nil {
    42  			log.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
    43  		}
    44  		log.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
    45  	}
    46  	if err := os.MkdirAll(dir, 0777); err != nil {
    47  		log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
    48  	}
    49  	if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
    50  		// Best effort.
    51  		os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
    52  	}
    53  
    54  	c, err := Open(dir)
    55  	if err != nil {
    56  		log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
    57  	}
    58  	defaultCache = c
    59  }
    60  
    61  var (
    62  	defaultDirOnce sync.Once
    63  	defaultDir     string
    64  	defaultDirErr  error
    65  )
    66  
    67  // DefaultDir returns the effective GOCACHE setting.
    68  // It returns "off" if the cache is disabled.
    69  func DefaultDir() string {
    70  	// Save the result of the first call to DefaultDir for later use in
    71  	// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
    72  	// subprocesses will inherit it, but that means initDefaultCache can't
    73  	// otherwise distinguish between an explicit "off" and a UserCacheDir error.
    74  
    75  	defaultDirOnce.Do(func() {
    76  		// NOTE: changed from upstream's cfg.Getenv, so it will ignore "go env -w".
    77  		// Consider calling "go env" or copying the cfg package instead.
    78  		defaultDir = os.Getenv("GOCACHE")
    79  		if filepath.IsAbs(defaultDir) || defaultDir == "off" {
    80  			return
    81  		}
    82  		if defaultDir != "" {
    83  			defaultDir = "off"
    84  			defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path")
    85  			return
    86  		}
    87  
    88  		// Compute default location.
    89  		dir, err := os.UserCacheDir()
    90  		if err != nil {
    91  			defaultDir = "off"
    92  			defaultDirErr = fmt.Errorf("GOCACHE is not defined and %v", err)
    93  			return
    94  		}
    95  		defaultDir = filepath.Join(dir, "go-build")
    96  	})
    97  
    98  	return defaultDir
    99  }
   100  

View as plain text