You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/go,testing: GOWORK value does not invalidate test cache #60052
This may or may not be a bug. While investigating a gopls test that fails when using a go.work file, I noticed that the value of GOWORK is apparently not used in the test cache key:
$ go clean -cache
$ go test ./internal/lsp/safetoken
--- FAIL: TestGoplsSourceDoesNotCallTokenFileMethods (0.02s)
safetoken_test.go:89: err: exit status 1: stderr: go: -mod may only be set to readonly when in workspace mode, but it is set to "mod"
Remove the -mod flag to use the default readonly value,
or set GOWORK=off to disable workspace mode.
FAIL
FAIL golang.org/x/tools/gopls/internal/lsp/safetoken 0.036s
FAIL
$ GOWORK=off go test ./internal/lsp/safetoken
ok golang.org/x/tools/gopls/internal/lsp/safetoken 3.252s
$ go test ./internal/lsp/safetoken
ok golang.org/x/tools/gopls/internal/lsp/safetoken (cached)
Observe that we get a cache hit for the successful test run when GOWORK=off is set, even when GOWORK=off is not set.
If I had to guess, I'd say that there is an assumption that GOWORK values affect the build list, and therefore if GOWORK changes the build list will change, and therefore we don't need to hash the actual GOWORK value into the cache key. However, as we see here this may not be sufficient if the test in question actually invokes the Go command. (note that my GOWORK uses exactly x/tools and x/tools/gopls, so there is likely no difference in the build list when run from the gopls module, which has a replace directive for x/tools).
@bcmills@matloob what do you think? Should the value of GOWORK be part of the cache key? If not, please feel free to close this issue.
In general the cache key for a test only includes the factors that determine what's in the resulting binary and the environment variables and files explicitly accessed by the test itself. (And #33976 means that the binary doesn't depend on the build list as much as it probably should either.)
So if all of the packages are loaded from the same source files with or without go/work, this may be WAI.
But in this case, I guess the environment variable gets threaded through x/tools/go/packages and then read by the go command without being explicitly accessed by the test? I see that both os.Getenv and os.LookupEnv record accesses to keys, but Environ does not. Perhaps it should.
changed the title [-]cmd/go: GOWORK value does not invalidate test cache[/-][+]cmd/go,testing: GOWORK value does not invalidate test cache[/+]on May 8, 2023
Activity
bcmills commentedon May 8, 2023
In general the cache key for a test only includes the factors that determine what's in the resulting binary and the environment variables and files explicitly accessed by the test itself. (And #33976 means that the binary doesn't depend on the build list as much as it probably should either.)
So if all of the packages are loaded from the same source files with or without
go/work, this may be WAI.But in this case, I guess the environment variable gets threaded through
x/tools/go/packagesand then read by thegocommand without being explicitly accessed by the test? I see that bothos.Getenvandos.LookupEnvrecord accesses to keys, butEnvirondoes not. Perhaps it should.[-]cmd/go: GOWORK value does not invalidate test cache[/-][+]cmd/go,testing: GOWORK value does not invalidate test cache[/+]bcmills commentedon May 8, 2023
I've filed #60057 to consider the more general
os.Environbehavior.bcmills commentedon May 8, 2023
In the meantime, a workaround might be to do something like this in a test function:
Note that #59849 suggests this has to be in the test function itself rather than in
TestMain.findleyr commentedon May 8, 2023
Thanks! I think you have confirmed that this is WAI, and I will defer discussion to #60057.
Thanks for the suggestion, but I am loath to replace all of our calls to os.Environ.
That seems like a consequence of #44625 (comment).
Let's close this as unactionable, thanks.