Skip to content

Commit fd323a8

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/cache: save more data from DefaultDir
cmd/go/main.go sets GOCACHE explicitly, so if we don't save some metadata about how DefaultDir arrived at its answer we will be unable to reconstruct it later. Fixes #29243 Change-Id: Ic8bb859ab045a29c91f6a4527e65aedabf874d53 Reviewed-on: https://go-review.googlesource.com/c/154309 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent 4771356 commit fd323a8

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

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

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ See golang.org to learn more about Go.
3737
// the first time Default is called.
3838
func initDefaultCache() {
3939
dir := DefaultDir()
40-
if dir == "off" {
41-
die()
40+
if dir == "off" || dir == "" {
41+
if defaultDirErr != nil {
42+
base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
43+
}
44+
base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
4245
}
4346
if err := os.MkdirAll(dir, 0777); err != nil {
4447
base.Fatalf("failed to initialize build cache at %s: %s\n", dir, err)
@@ -55,29 +58,35 @@ func initDefaultCache() {
5558
defaultCache = c
5659
}
5760

61+
var (
62+
defaultDirOnce sync.Once
63+
defaultDir string
64+
defaultDirErr error
65+
)
66+
5867
// DefaultDir returns the effective GOCACHE setting.
5968
// It returns "off" if the cache is disabled.
6069
func DefaultDir() string {
61-
dir := os.Getenv("GOCACHE")
62-
if dir != "" {
63-
return dir
64-
}
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.
6574

66-
// Compute default location.
67-
dir, err := os.UserCacheDir()
68-
if err != nil {
69-
return "off"
70-
}
71-
return filepath.Join(dir, "go-build")
72-
}
75+
defaultDirOnce.Do(func() {
76+
defaultDir = os.Getenv("GOCACHE")
77+
if defaultDir != "" {
78+
return
79+
}
7380

74-
// die calls base.Fatalf with a message explaining why DefaultDir was "off".
75-
func die() {
76-
if os.Getenv("GOCACHE") == "off" {
77-
base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
78-
}
79-
if _, err := os.UserCacheDir(); err != nil {
80-
base.Fatalf("build cache is required, but could not be located: %v", err)
81-
}
82-
panic(fmt.Sprintf("cache.die called unexpectedly with cache.DefaultDir() = %s", DefaultDir()))
81+
// Compute default location.
82+
dir, err := os.UserCacheDir()
83+
if err != nil {
84+
defaultDir = "off"
85+
defaultDirErr = fmt.Errorf("GOCACHE is not defined and %v", err)
86+
return
87+
}
88+
defaultDir = filepath.Join(dir, "go-build")
89+
})
90+
91+
return defaultDir
8392
}

src/cmd/go/testdata/script/build_nocache.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
1-
# Set GOCACHE to a directory that doesn't allow writes.
2-
[windows] skip # Does not support unwritable directories.
1+
# As of Go 1.12, the module cache is required.
2+
3+
# If none of the variables we use to locate GOCACHE are set, the cache is off
4+
# and we cannot build.
5+
env GOCACHE=
6+
env XDG_CACHE_HOME=
7+
env HOME=
8+
[plan9] env home=
9+
[windows] env LocalAppData=
10+
! go build -o triv triv.go
11+
stderr 'build cache is required, but could not be located: GOCACHE is not defined and .*'
12+
13+
# An explicit GOCACHE=off also disables builds.
14+
env GOCACHE=off
15+
! go build -o triv triv.go
16+
stderr 'build cache is disabled by GOCACHE=off'
17+
18+
# If GOCACHE is set to an unwritable directory, we should diagnose it as such.
19+
[windows] stop # Does not support unwritable directories.
320
[root] skip # Can write to unwritable directories.
421

522
mkdir $WORK/unwritable/home
@@ -8,9 +25,6 @@ chmod 0555 $WORK/unwritable/home
825
[plan9] env home=$WORK/unwritable/home
926

1027
env GOCACHE=$WORK/unwritable/home
11-
12-
# As of Go 1.12, the module cache is required:
13-
# failure to write to it should cause builds to fail.
1428
! go build -o triv triv.go
1529
stderr 'failed to initialize build cache.* permission denied'
1630

0 commit comments

Comments
 (0)