@@ -35,12 +35,14 @@ See golang.org to learn more about Go.
35
35
// initDefaultCache does the work of finding the default cache
36
36
// the first time Default is called.
37
37
func initDefaultCache () {
38
- dir := DefaultDir ()
38
+ dir , showWarnings := defaultDir ()
39
39
if dir == "off" {
40
40
return
41
41
}
42
42
if err := os .MkdirAll (dir , 0777 ); err != nil {
43
- fmt .Fprintf (os .Stderr , "go: disabling cache (%s) due to initialization failure: %s\n " , dir , err )
43
+ if showWarnings {
44
+ fmt .Fprintf (os .Stderr , "go: disabling cache (%s) due to initialization failure: %s\n " , dir , err )
45
+ }
44
46
return
45
47
}
46
48
if _ , err := os .Stat (filepath .Join (dir , "README" )); err != nil {
@@ -50,7 +52,9 @@ func initDefaultCache() {
50
52
51
53
c , err := Open (dir )
52
54
if err != nil {
53
- fmt .Fprintf (os .Stderr , "go: disabling cache (%s) due to initialization failure: %s\n " , dir , err )
55
+ if showWarnings {
56
+ fmt .Fprintf (os .Stderr , "go: disabling cache (%s) due to initialization failure: %s\n " , dir , err )
57
+ }
54
58
return
55
59
}
56
60
defaultCache = c
@@ -59,14 +63,24 @@ func initDefaultCache() {
59
63
// DefaultDir returns the effective GOCACHE setting.
60
64
// It returns "off" if the cache is disabled.
61
65
func DefaultDir () string {
66
+ dir , _ := defaultDir ()
67
+ return dir
68
+ }
69
+
70
+ // defaultDir returns the effective GOCACHE setting.
71
+ // It returns "off" if the cache is disabled.
72
+ // The second return value reports whether warnings should
73
+ // be shown if the cache fails to initialize.
74
+ func defaultDir () (string , bool ) {
62
75
dir := os .Getenv ("GOCACHE" )
63
76
if dir != "" {
64
- return dir
77
+ return dir , true
65
78
}
66
79
67
80
// Compute default location.
68
81
// TODO(rsc): This code belongs somewhere else,
69
82
// like maybe ioutil.CacheDir or os.CacheDir.
83
+ showWarnings := true
70
84
switch runtime .GOOS {
71
85
case "windows" :
72
86
dir = os .Getenv ("LocalAppData" )
@@ -76,20 +90,20 @@ func DefaultDir() string {
76
90
dir = os .Getenv ("AppData" )
77
91
}
78
92
if dir == "" {
79
- return "off"
93
+ return "off" , true
80
94
}
81
95
82
96
case "darwin" :
83
97
dir = os .Getenv ("HOME" )
84
98
if dir == "" {
85
- return "off"
99
+ return "off" , true
86
100
}
87
101
dir += "/Library/Caches"
88
102
89
103
case "plan9" :
90
104
dir = os .Getenv ("home" )
91
105
if dir == "" {
92
- return "off"
106
+ return "off" , true
93
107
}
94
108
// Plan 9 has no established per-user cache directory,
95
109
// but $home/lib/xyz is the usual equivalent of $HOME/.xyz on Unix.
@@ -101,10 +115,15 @@ func DefaultDir() string {
101
115
if dir == "" {
102
116
dir = os .Getenv ("HOME" )
103
117
if dir == "" {
104
- return "off"
118
+ return "off" , true
119
+ }
120
+ if dir == "/" {
121
+ // probably docker run with -u flag
122
+ // https://golang.org/issue/26280
123
+ showWarnings = false
105
124
}
106
125
dir += "/.cache"
107
126
}
108
127
}
109
- return filepath .Join (dir , "go-build" )
128
+ return filepath .Join (dir , "go-build" ), showWarnings
110
129
}
0 commit comments