Skip to content

testing: t.TempDir() failing if test name exceeds 255 chars #71742

Closed

Description

@Luap99

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.

Activity

  1. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Feb 14, 2025
  2. adonovan commented on Feb 14, 2025

    @adonovan
    Member

    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.

  3. Luap99 commented on Feb 14, 2025

    @Luap99
    Author

    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

    manifest_list_test.go:385: TempDir: mkdir /tmp/TestAddArtifactsartifactType=applicationx-something-else,configType=applicationvnd.oci.image.config.v1+json,configLength=9,layerType=applicationvnd.oci.image.layer.v1.tar+gzip,excludeTitles=true,artifactSubject=blobs-only,indexSubject=config-only2880673117: file name too long
    

    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.

  4. stphtt commented on Feb 14, 2025

    @stphtt

    That makes sense, it's not something expected. Working on this one. I accidentally stumbled upon this bug too.

  5. seankhliao commented on Feb 14, 2025

    @seankhliao
    Member

    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

    for a := range ...
      t.Run(a, func{
         for b := range ...
           t.Run(b, func{
              for c := range ...
                 t.Run(c, func{
    
  6. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Feb 15, 2025
  7. added this to the Backlog milestone on Feb 15, 2025
  8. gopherbot commented on Feb 15, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/649915 mentions this issue: testing: fix TempDir failure when test name too long

  9. gopherbot commented on May 11, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/671577 mentions this issue: testing: limit TempDir name length

  10. added a commit that references this issue on May 12, 2025
    ac992f2
  11. added a commit that references this issue on May 24, 2025
    6f73082
  12. locked and limited conversation to collaborators on May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues 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.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions