Skip to content
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

cmd/go/internal/envcmd: GOCACHE=off behavior difficult to debug #29243

Closed
kevinburke opened this issue Dec 14, 2018 · 12 comments
Closed

cmd/go/internal/envcmd: GOCACHE=off behavior difficult to debug #29243

kevinburke opened this issue Dec 14, 2018 · 12 comments

Comments

@kevinburke
Copy link
Contributor

Please answer these questions before submitting your issue. Thanks!

What did you do?

I have a test that calls go build with the following code:

	env := []string{
		"GOROOT=" + os.Getenv("GOROOT"),
		"GOPATH=" + filepath.Join(root, "src", "tool", "_gopath"),
	}
        cmdArgs := []string{"build", "-o", binPath}
	cmd := exec.Command("go", cmdArgs...)
	cmd.Env = append(env, "GOOS="+p.OS.String(), "GOARCH="+p.Arch.String())

Compiling with tip this failed first with the error "exit status 1", then when I printed out the stdout/stderr I got:

build cache is disabled by GOCACHE=off, but required as of Go 1.12

This was very confusing because I double and triple checked the env vars and the source code and I never referred to GOCACHE.

Finally, by editing the cmd/go source code and recompiling I was able to determine the actual problem was that I was not passing through the $HOME variable, so os.UserCacheDir() was failing, and cmd/go/internal/cache/default.go:DefaultDir was returning an error. $HOME is not a requirement to use the build cache on Go 1.11, or, perhaps this test program was failing to hit the build cache in Go 1.11 and it was not an error.

This is concerning because we should not expect the average Go user to recompile the Go command to determine what is wrong with their program.

It would be good to provide an error message that lets the user better debug what is wrong with their program and how/why GOCACHE is being set to off in the build environment.

Does this issue reproduce with the latest release (go1.11.2)?

No

System details

go version devel +a6293995c5 Thu Dec 13 23:58:06 2018 +0000 darwin/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kevin/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/kevin"
GOPROXY=""
GORACE=""
GOROOT="/Users/kevin/go"
GOTMPDIR=""
GOTOOLDIR="/Users/kevin/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
GOROOT/bin/go version: cache default dir /Users/kevin/Library/Caches/go-build
go version devel +a6293995c5 Thu Dec 13 23:58:06 2018 +0000 darwin/amd64
GOROOT/bin/go tool compile -V: cache default dir /Users/kevin/Library/Caches/go-build
compile version devel +a6293995c5 Thu Dec 13 23:58:06 2018 +0000
uname -v: Darwin Kernel Version 18.2.0: Fri Oct  5 19:41:49 PDT 2018; root:xnu-4903.221.2~2/RELEASE_X86_64
ProductName:	Mac OS X
ProductVersion:	10.14.1
BuildVersion:	18B75
lldb --version: lldb-1000.0.38.2
  Swift-4.2
@kevinburke
Copy link
Contributor Author

see also #29251, it's concerning that this error message has appeared twice, which increases the likelihood (to me anyway) that a significant portion of the user base will hit it.

@bcmills bcmills added the GoCommand cmd/go label Dec 14, 2018
@bcmills bcmills self-assigned this Dec 14, 2018
@bcmills
Copy link
Contributor

bcmills commented Dec 14, 2018

$HOME is not a requirement to use the build cache on Go 1.11

On Darwin, the build cache requires either GOCACHE or os.UserCacheDir(), and os.UserCacheDir() requires HOME.

or, perhaps this test program was failing to hit the build cache in Go 1.11 and it was not an error.

That seems more likely. But we should certainly fix the diagnostics.

@bcmills
Copy link
Contributor

bcmills commented Dec 14, 2018

FWIW, the diagnostic in this case is intended to read build cache is required, but could not be located: $HOME is not defined.

The logic for that is here, and seemed so trivial that I only tested one mode:

// die calls base.Fatalf with a message explaining why DefaultDir was "off".
func die() {
if os.Getenv("GOCACHE") == "off" {
base.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12")
}
if _, err := os.UserCacheDir(); err != nil {
base.Fatalf("build cache is required, but could not be located: %v", err)
}
panic(fmt.Sprintf("cache.die called unexpectedly with cache.DefaultDir() = %s", DefaultDir()))
}

Obviously I should have tested more thoroughly. 🙂

@bcmills bcmills added this to the Go1.12 milestone Dec 14, 2018
@FiloSottile
Copy link
Contributor

Are we committed to not letting users build code unless they set $HOME or $GOCACHE? I'd expect this to break quite a few Docker images, which probably didn't want to keep the cache around anyway. Also, I run some machines with a read-only $HOME, which I guess won't be able to build Go code anymore.

It already broke tip.golang.org and stuff inside Google, too.

@bcmills
Copy link
Contributor

bcmills commented Dec 14, 2018

Are we committed to not letting users build code unless they set $HOME or $GOCACHE?

It's what we announced in the 1.11 release notes, and nobody complained about it then.

We could perhaps put the cache in os.TempDir() if we can't find it some other place, though.

@bcmills
Copy link
Contributor

bcmills commented Dec 14, 2018

At any rate, that seems like a separate issue from the broken diagnostic message.

@kevinburke
Copy link
Contributor Author

kevinburke commented Dec 14, 2018 via email

@FiloSottile
Copy link
Contributor

At any rate, that seems like a separate issue from the broken diagnostic message.

Opened #29267.

@gopherbot
Copy link

Change https://golang.org/cl/154309 mentions this issue: cmd/go/internal/cache: save more data from DefaultDir

@purpleidea
Copy link

build cache is disabled by GOCACHE=off, but required as of Go 1.12

Just started seeing this in all my travis builds. Which broke everything! I set this variable in my .envrc because I NEVER want the tests cached! How else can I disable test caching now??

@ianlancetaylor
Copy link
Contributor

You can always disable test caching by using the -test.count=1 option, as documented at https://golang.org/cmd/go/#hdr-Test_packages.

@gopherbot
Copy link

Change https://golang.org/cl/163798 mentions this issue: playground: use Go 1.12

gopherbot pushed a commit to golang/playground that referenced this issue Feb 26, 2019
Go 1.12 has been released¹ and should be used, so that it's possible
to play with Go programs that rely on Go 1.12-only features.

In Go 1.12, build cache is required. Either GOCACHE must be set,
or HOME must be set so that GOCACHE can be implicitly deduced.
Set HOME and pass it on to the go build invocation inside the
compileAndRun function.

This fixes the following error, detected by the playground test:

	2019/02/26 06:28:44 compile error: build cache is required, but could not
	be located: GOCACHE is not defined and neither $XDG_CACHE_HOME nor $HOME
	are defined
	The command '/bin/sh -c /app/playground test' returned a non-zero code: 1

This is related to issues golang/go#29243 and golang/go#29251,
and the fix in CL 154181.

¹ https://groups.google.com/d/msg/golang-announce/Y1RZqnCKUKY/N9yK5c8iBgAJ

Fixes golang/go#30397

Change-Id: I44c5c8928060732f31fd935b114568258c7298c7
Reviewed-on: https://go-review.googlesource.com/c/163798
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
@golang golang locked and limited conversation to collaborators Feb 26, 2020
@rsc rsc unassigned bcmills Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants