Skip to content

Commit 665af86

Browse files
mneverovgopherbot
authored andcommitted
cmd/go: fail go clean command when failed to find go cache directory
Currently, if computing of the go cache directory fails it does not expose the error. Commands like go clean, exec, modindex that use go cache directory continue execution producing incorrect or no result. This patch adds an error to the return values such that it can be validated on call sites. It also introduces such validation in go clean -cache command to fail execution in case when error occurred. Fixes #69997 Change-Id: I53fd1ec67f0a6bd8a367e785dcb145a673c084dc GitHub-Last-Rev: e2063d1 GitHub-Pull-Request: #70392 Reviewed-on: https://go-review.googlesource.com/c/go/+/628596 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Michael Matloob <[email protected]>
1 parent 84e0061 commit 665af86

File tree

10 files changed

+26
-15
lines changed

10 files changed

+26
-15
lines changed

src/cmd/go/alldocs.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/go_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func TestMain(m *testing.M) {
197197
defer removeAll(testTmpDir)
198198
}
199199

200-
testGOCACHE, _ = cache.DefaultDir()
200+
testGOCACHE, _, _ = cache.DefaultDir()
201201
if testenv.HasGoBuild() {
202202
testBin = filepath.Join(testTmpDir, "testbin")
203203
if err := os.Mkdir(testBin, 0777); err != nil {

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ See golang.org to learn more about Go.
3434
// initDefaultCache does the work of finding the default cache
3535
// the first time Default is called.
3636
func initDefaultCache() Cache {
37-
dir, _ := DefaultDir()
37+
dir, _, err := DefaultDir()
38+
if err != nil {
39+
base.Fatalf("build cache is required, but could not be located: %v", err)
40+
}
3841
if dir == "off" {
39-
if defaultDirErr != nil {
40-
base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
41-
}
4242
base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
4343
}
4444
if err := os.MkdirAll(dir, 0o777); err != nil {
@@ -71,7 +71,7 @@ var (
7171
// DefaultDir returns the effective GOCACHE setting.
7272
// It returns "off" if the cache is disabled,
7373
// and reports whether the effective value differs from GOCACHE.
74-
func DefaultDir() (string, bool) {
74+
func DefaultDir() (string, bool, error) {
7575
// Save the result of the first call to DefaultDir for later use in
7676
// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
7777
// subprocesses will inherit it, but that means initDefaultCache can't
@@ -100,5 +100,5 @@ func DefaultDir() (string, bool) {
100100
defaultDir = filepath.Join(dir, "go-build")
101101
})
102102

103-
return defaultDir, defaultDirChanged
103+
return defaultDir, defaultDirChanged, defaultDirErr
104104
}

src/cmd/go/internal/clean/clean.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
155155
sh := work.NewShell("", &load.TextPrinter{Writer: os.Stdout})
156156

157157
if cleanCache {
158-
dir, _ := cache.DefaultDir()
158+
dir, _, err := cache.DefaultDir()
159+
if err != nil {
160+
base.Fatal(err)
161+
}
159162
if dir != "off" {
160163
// Remove the cache subdirectories but not the top cache directory.
161164
// The top cache directory may have been created with special permissions
@@ -182,7 +185,10 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
182185
// Instead of walking through the entire cache looking for test results,
183186
// we write a file to the cache indicating that all test results from before
184187
// right now are to be ignored.
185-
dir, _ := cache.DefaultDir()
188+
dir, _, err := cache.DefaultDir()
189+
if err != nil {
190+
base.Fatal(err)
191+
}
186192
if dir != "off" {
187193
f, err := lockedfile.Edit(filepath.Join(dir, "testexpire.txt"))
188194
if err == nil {

src/cmd/go/internal/envcmd/env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func MkEnv() []cfg.EnvVar {
131131
env[i].Changed = true
132132
}
133133
case "GOCACHE":
134-
env[i].Value, env[i].Changed = cache.DefaultDir()
134+
env[i].Value, env[i].Changed, _ = cache.DefaultDir()
135135
case "GOTOOLCHAIN":
136136
env[i].Value, env[i].Changed = cfg.EnvOrAndChanged("GOTOOLCHAIN", "")
137137
case "GODEBUG":

src/cmd/go/internal/help/helpdoc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ General-purpose environment variables:
507507
The directory where 'go install' will install a command.
508508
GOCACHE
509509
The directory where the go command will store cached
510-
information for reuse in future builds.
510+
information for reuse in future builds. Must be an absolute path.
511511
GOCACHEPROG
512512
A command (with optional space-separated flags) that implements an
513513
external go command build cache.

src/cmd/go/internal/modindex/read.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func GetPackage(modroot, pkgdir string) (*IndexPackage, error) {
156156
// using the index, for instance because the index is disabled, or the package
157157
// is not in a module.
158158
func GetModule(modroot string) (*Module, error) {
159-
dir, _ := cache.DefaultDir()
159+
dir, _, _ := cache.DefaultDir()
160160
if !enabled || dir == "off" {
161161
return nil, errDisabled
162162
}

src/cmd/go/internal/test/test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
840840
// Read testcache expiration time, if present.
841841
// (We implement go clean -testcache by writing an expiration date
842842
// instead of searching out and deleting test result cache entries.)
843-
if dir, _ := cache.DefaultDir(); dir != "off" {
843+
if dir, _, _ := cache.DefaultDir(); dir != "off" {
844844
if data, _ := lockedfile.Read(filepath.Join(dir, "testexpire.txt")); len(data) > 0 && data[len(data)-1] == '\n' {
845845
if t, err := strconv.ParseInt(string(data[:len(data)-1]), 10, 64); err == nil {
846846
testCacheExpire = time.Unix(0, t)

src/cmd/go/internal/work/shell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (sh *Shell) moveOrCopyFile(dst, src string, perm fs.FileMode, force bool) e
127127
// Otherwise fall back to standard copy.
128128

129129
// If the source is in the build cache, we need to copy it.
130-
dir, _ := cache.DefaultDir()
130+
dir, _, _ := cache.DefaultDir()
131131
if strings.HasPrefix(src, dir) {
132132
return sh.CopyFile(dst, src, perm, force)
133133
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ go clean -cache
2020
! go clean -cache .
2121
stderr 'go: clean -cache cannot be used with package arguments'
2222

23+
# GOCACHE must be an absolute path.
24+
env GOCACHE=.
25+
! go clean -cache
26+
stderr 'go: GOCACHE is not an absolute path'
27+
2328
-- main.go --
2429
package main
2530

0 commit comments

Comments
 (0)