Repository navigation
testing: t.TempDir() failing if test name exceeds 255 chars #71742
Copy link
Copy link
Closed
Labels
BugReportIssues describing a possible bug in the Go implementation.Issues describing a possible bug in the Go implementation.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Description
Go version
go version go1.24.0 linux/amd64
Output of go env in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build206958557=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/root/test/go.mod'
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/root/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.0'
GOWORK=''
PKG_CONFIG='pkg-config'What did you do?
When migrating some test to using t.TempDir() over os.MkdirTemp() I all of the sudden got failures, turns out we have some quite long test names and t.TempDir() uses the name as basis for the temp directory. That fails if the path component exceeds 255 chars of course.
package main_test
import (
"strings"
"testing"
)
func TestTempDir(t *testing.T) {
// "TestTempDir" -> length 11
// max unit32 is used as pattern suffix -> max length 10 (random of course)
// + the name from t.Run() pushes the path component above 255 chars
t.Run(strings.Repeat("A", 255), func(t *testing.T) {
t.TempDir()
})
}https://go.dev/play/p/K3vSiE14faS
What did you see happen?
t.TempDir() will fail to create the directory if the test name is to long.
Given the path suffix is a random unit32 (with no zero padding) one might even see flakes if the total test name length is between 246-254 which makes it even worse.
What did you expect to see?
t.TempDir() should work regardless of the test name, maybe it should trim the path to 255 chars if necessary.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugReportIssues describing a possible bug in the Go implementation.Issues describing a possible bug in the Go implementation.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Activity
gabyhelp commentedon Feb 14, 2025
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
adonovan commentedon Feb 14, 2025
A test name of 255 bytes seems excessive, but I agree that if t.Run accepts the name, then t.TempDir within the subtest should work.
Luap99 commentedon Feb 14, 2025
Yeah it is not a common thing for sure, in our specific case we could use another name I suppose but we actually use the test name to contain all the test input data with several nested loops, that way we cover all combinations
https://github.com/containers/common/blob/1e4c9bf42bd56b0500948287da1d710f85d29352/libimage/manifest_list_test.go#L459-L505
and then I got a failure like
Considering someone needs to know which iteration failed with which inputs I think adding them all in the name makes sense. I guess the other option would be to
t.Logf()instead of the name.stphtt commentedon Feb 14, 2025
That makes sense, it's not something expected. Working on this one. I accidentally stumbled upon this bug too.
seankhliao commentedon Feb 14, 2025
the bug should be fixed, but you could also have setup your tests with a subtest per level of nested for loop
you're not limited to one level of subtests
gopherbot commentedon Feb 15, 2025
Change https://go.dev/cl/649915 mentions this issue:
testing: fix TempDir failure when test name too longgopherbot commentedon May 11, 2025
Change https://go.dev/cl/671577 mentions this issue:
testing: limit TempDir name lengthtesting: limit TempDir name length
testing: limit TempDir name length