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.
16 // Default returns the default cache to use.
17 func Default() (*Cache, error) {
18 defaultOnce.Do(initDefaultCache)
19 return defaultCache, defaultDirErr
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 staticcheck.
33 // initDefaultCache does the work of finding the default cache
34 // the first time Default is called.
35 func initDefaultCache() {
37 if err := os.MkdirAll(dir, 0777); err != nil {
38 log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
40 if _, err := os.Stat(filepath.Join(dir, "README")); err != nil {
42 ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666)
47 log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
53 defaultDirOnce sync.Once
58 // DefaultDir returns the effective STATICCHECK_CACHE setting.
59 func DefaultDir() string {
60 // Save the result of the first call to DefaultDir for later use in
61 // initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
62 // subprocesses will inherit it, but that means initDefaultCache can't
63 // otherwise distinguish between an explicit "off" and a UserCacheDir error.
65 defaultDirOnce.Do(func() {
66 defaultDir = os.Getenv("STATICCHECK_CACHE")
67 if filepath.IsAbs(defaultDir) {
71 defaultDirErr = fmt.Errorf("STATICCHECK_CACHE is not an absolute path")
75 // Compute default location.
76 dir, err := os.UserCacheDir()
78 defaultDirErr = fmt.Errorf("STATICCHECK_CACHE is not defined and %v", err)
81 defaultDir = filepath.Join(dir, "staticcheck")