Skip to content

Commit 452acab

Browse files
committed
md/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 golang#69997
1 parent d87878c commit 452acab

File tree

9 files changed

+18
-12
lines changed

9 files changed

+18
-12
lines changed

src/cmd/go/alldocs.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/go_test.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ 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, _, _ := DefaultDir()
3838
if dir == "off" {
3939
if defaultDirErr != nil {
4040
base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
@@ -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

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

155155
if cleanCache {
156-
dir, _ := cache.DefaultDir()
156+
dir, _, err := cache.DefaultDir()
157+
if err != nil {
158+
base.Fatal(err)
159+
}
157160
if dir != "off" {
158161
// Remove the cache subdirectories but not the top cache directory.
159162
// The top cache directory may have been created with special permissions
@@ -180,7 +183,10 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
180183
// Instead of walking through the entire cache looking for test results,
181184
// we write a file to the cache indicating that all test results from before
182185
// right now are to be ignored.
183-
dir, _ := cache.DefaultDir()
186+
dir, _, err := cache.DefaultDir()
187+
if err != nil {
188+
base.Fatal(err)
189+
}
184190
if dir != "off" {
185191
f, err := lockedfile.Edit(filepath.Join(dir, "testexpire.txt"))
186192
if err == nil {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func MkEnv() []cfg.EnvVar {
130130
env[i].Changed = true
131131
}
132132
case "GOCACHE":
133-
env[i].Value, env[i].Changed = cache.DefaultDir()
133+
env[i].Value, env[i].Changed, _ = cache.DefaultDir()
134134
case "GOTOOLCHAIN":
135135
env[i].Value, env[i].Changed = cfg.EnvOrAndChanged("GOTOOLCHAIN", "")
136136
case "GODEBUG":

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ General-purpose environment variables:
508508
GOBIN
509509
The directory where 'go install' will install a command.
510510
GOCACHE
511-
The directory where the go command will store cached
511+
The absolute path to the directory where the go command will store cached
512512
information for reuse in future builds.
513513
GODEBUG
514514
Enable various debugging facilities. See https://go.dev/doc/godebug

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func GetPackage(modroot, pkgdir string) (*IndexPackage, error) {
151151
// using the index, for instance because the index is disabled, or the package
152152
// is not in a module.
153153
func GetModule(modroot string) (*Module, error) {
154-
dir, _ := cache.DefaultDir()
154+
dir, _, _ := cache.DefaultDir()
155155
if !enabled || dir == "off" {
156156
return nil, errDisabled
157157
}

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

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

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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)