Skip to content

cmd/go: fail go clean command when failed to find go cache directory #70392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cmd/go/alldocs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cmd/go/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestMain(m *testing.M) {
defer removeAll(testTmpDir)
}

testGOCACHE, _ = cache.DefaultDir()
testGOCACHE, _, _ = cache.DefaultDir()
if testenv.HasGoBuild() {
testBin = filepath.Join(testTmpDir, "testbin")
if err := os.Mkdir(testBin, 0777); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions src/cmd/go/internal/cache/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ See golang.org to learn more about Go.
// initDefaultCache does the work of finding the default cache
// the first time Default is called.
func initDefaultCache() Cache {
dir, _ := DefaultDir()
dir, _, err := DefaultDir()
if err != nil {
base.Fatalf("build cache is required, but could not be located: %v", err)
}
if dir == "off" {
if defaultDirErr != nil {
base.Fatalf("build cache is required, but could not be located: %v", defaultDirErr)
}
base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
}
if err := os.MkdirAll(dir, 0o777); err != nil {
Expand Down Expand Up @@ -71,7 +71,7 @@ var (
// DefaultDir returns the effective GOCACHE setting.
// It returns "off" if the cache is disabled,
// and reports whether the effective value differs from GOCACHE.
func DefaultDir() (string, bool) {
func DefaultDir() (string, bool, error) {
// Save the result of the first call to DefaultDir for later use in
// initDefaultCache. cmd/go/main.go explicitly sets GOCACHE so that
// subprocesses will inherit it, but that means initDefaultCache can't
Expand Down Expand Up @@ -100,5 +100,5 @@ func DefaultDir() (string, bool) {
defaultDir = filepath.Join(dir, "go-build")
})

return defaultDir, defaultDirChanged
return defaultDir, defaultDirChanged, defaultDirErr
}
10 changes: 8 additions & 2 deletions src/cmd/go/internal/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
sh := work.NewShell("", &load.TextPrinter{Writer: os.Stdout})

if cleanCache {
dir, _ := cache.DefaultDir()
dir, _, err := cache.DefaultDir()
if err != nil {
base.Fatal(err)
}
if dir != "off" {
// Remove the cache subdirectories but not the top cache directory.
// The top cache directory may have been created with special permissions
Expand All @@ -180,7 +183,10 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
// Instead of walking through the entire cache looking for test results,
// we write a file to the cache indicating that all test results from before
// right now are to be ignored.
dir, _ := cache.DefaultDir()
dir, _, err := cache.DefaultDir()
if err != nil {
base.Fatal(err)
}
if dir != "off" {
f, err := lockedfile.Edit(filepath.Join(dir, "testexpire.txt"))
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/envcmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func MkEnv() []cfg.EnvVar {
env[i].Changed = true
}
case "GOCACHE":
env[i].Value, env[i].Changed = cache.DefaultDir()
env[i].Value, env[i].Changed, _ = cache.DefaultDir()
case "GOTOOLCHAIN":
env[i].Value, env[i].Changed = cfg.EnvOrAndChanged("GOTOOLCHAIN", "")
case "GODEBUG":
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/help/helpdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ General-purpose environment variables:
The directory where 'go install' will install a command.
GOCACHE
The directory where the go command will store cached
information for reuse in future builds.
information for reuse in future builds. Must be an absolute path.
GOCACHEPROG
A command (with optional space-separated flags) that implements an
external go command build cache.
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modindex/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func GetPackage(modroot, pkgdir string) (*IndexPackage, error) {
// using the index, for instance because the index is disabled, or the package
// is not in a module.
func GetModule(modroot string) (*Module, error) {
dir, _ := cache.DefaultDir()
dir, _, _ := cache.DefaultDir()
if !enabled || dir == "off" {
return nil, errDisabled
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
// Read testcache expiration time, if present.
// (We implement go clean -testcache by writing an expiration date
// instead of searching out and deleting test result cache entries.)
if dir, _ := cache.DefaultDir(); dir != "off" {
if dir, _, _ := cache.DefaultDir(); dir != "off" {
if data, _ := lockedfile.Read(filepath.Join(dir, "testexpire.txt")); len(data) > 0 && data[len(data)-1] == '\n' {
if t, err := strconv.ParseInt(string(data[:len(data)-1]), 10, 64); err == nil {
testCacheExpire = time.Unix(0, t)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/internal/work/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (sh *Shell) moveOrCopyFile(dst, src string, perm fs.FileMode, force bool) e
// Otherwise fall back to standard copy.

// If the source is in the build cache, we need to copy it.
dir, _ := cache.DefaultDir()
dir, _, _ := cache.DefaultDir()
if strings.HasPrefix(src, dir) {
return sh.CopyFile(dst, src, perm, force)
}
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/go/testdata/script/clean_cache_n.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ go clean -cache
! go clean -cache .
stderr 'go: clean -cache cannot be used with package arguments'

# GOCACHE must be an absolute path.
env GOCACHE=.
! go clean -cache
stderr 'go: GOCACHE is not an absolute path'

-- main.go --
package main

Expand Down