Skip to content

cmd/go: invoking go run from go test can corrupt build cache #69566

@rittneje

Description

@rittneje

Go version

go version go1.22.7 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/tmp/.gocache'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.7'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/Users/rittneje/go-test-bug/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1468831127=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I have the following files:

go.mod

module gotestbug

go 1.22

pkg1/foo_test.go

package pkg1

import (
	"bytes"
	"os/exec"
	"path/filepath"
	"testing"
)

func TestFoo(t *testing.T) {
	cmd := exec.Command("go", "run", filepath.Join("testdata", "main.go"))
	output := new(bytes.Buffer)
	cmd.Stdout = output
	cmd.Stderr = output

	t.Logf("+ %s", cmd)
	if err := cmd.Run(); err != nil {
		t.Fatal(err, output)
	}
}

pkg1/testdata/main.go

package main

func main() {
}

pkg2/bar_test.go

package pkg2

import (
	"testing"
)

func TestBar(t *testing.T) {
	t.Log("hello")
}

I then ran the following commands as root to prime the build cache. (I intentionally set the mtimes and trim.txt file to the past in order to reproduce the bug.)

go clean -cache
go build std
chmod -R a+rwX "${GOCACHE}"
find "${GOCACHE}" -type f -exec touch '{}' -d '2024-07-01 00:00:00' \;
printf '1719792000' > "${GOCACHE}/trim.txt"

Finally I ran go test as a non-root user. (I passed -p=1 to force it to run one package at a time in order to make the bug happen deterministically.)

go test -v -count=1 -p=1 ./...

What did you see happen?

As described in #69565, since the build cache was crafted to look old, go run decided to trim it. This somehow causes go test to break.

=== RUN   TestFoo
    foo_test.go:16: + /usr/local/go/bin/go run testdata/main.go
--- PASS: TestFoo (0.15s)
PASS
ok      gotestbug/pkg1  0.148s
# gotestbug/pkg2 [gotestbug/pkg2.test]
pkg2/bar_test.go:4:2: could not import testing (open /tmp/.gocache/26/26737db8c74b26401dc8828801cc3793a888d7c29c40b7500b7e9e5f96deec19-d: no such file or directory)
FAIL    gotestbug/pkg2 [build failed]
FAIL

It should be noted that this issue does not happen if I run go test as root.

What did you expect to see?

The test should pass without any compilation issues.

Metadata

Metadata

Assignees

Labels

GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions