Skip to content

Commit 13d24b6

Browse files
oioojBryan C. Mills
authored and
Bryan C. Mills
committed
cmd/go/internal/cache: disable builds if GOCACHE is not an absolute path
If GOCACHE is set but is not an absolute path, we cannot build. And GOCACHE=off also returns the error message "build cache is disabled by GOCACHE=off". Fixes #30447 Change-Id: I24f64bc886599ca0acd757acada4714aebe4d3ae Reviewed-on: https://go-review.googlesource.com/c/164200 Run-TryBot: Baokun Lee <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent db2b6e1 commit 13d24b6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ 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" || dir == "" {
40+
if dir == "off" {
4141
if defaultDirErr != nil {
4242
base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
4343
}
@@ -74,7 +74,12 @@ func DefaultDir() string {
7474

7575
defaultDirOnce.Do(func() {
7676
defaultDir = os.Getenv("GOCACHE")
77+
if filepath.IsAbs(defaultDir) || defaultDir == "off" {
78+
return
79+
}
7780
if defaultDir != "" {
81+
defaultDir = "off"
82+
defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path")
7883
return
7984
}
8085

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

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ env HOME=
1212
! go build -o triv triv.go
1313
stderr 'build cache is required, but could not be located: GOCACHE is not defined and .*'
1414

15+
# If GOCACHE is set but is not an absolute path, and we cannot build.
16+
env GOCACHE=test
17+
! go build -o triv triv.go
18+
stderr 'build cache is required, but could not be located: GOCACHE is not an absolute path'
19+
1520
# An explicit GOCACHE=off also disables builds.
1621
env GOCACHE=off
1722
! go build -o triv triv.go

0 commit comments

Comments
 (0)