Skip to content

testing: Chdir panics if not testing on the same drive as Go #80130

Description

@larhun

Go version

go version go1.26.4 windows/amd64

Output of go env in your module/workspace:

set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=1
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=
set GOCACHE=C:\Users\***\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=
set GOENV=C:\Users\***\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\***\AppData\Local\Temp\go-build4090839346=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=Z:\***\testchdir\go.mod
set GOMODCACHE=C:\Users\***\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\***\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\***\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.26.4
set GOWORK=
set PKG_CONFIG=pkg-config

What did you do?

On Z:\***\testchdir execute:

go mod init testchdir
mkdir chdir
cd chdir

Write the chdir_test.go file:

package chdir_test

import (
        "os"
        "testing"
)

func TestChdir_fail(t *testing.T) {
        var d = t.TempDir()
        t.Chdir(d)
}

func TestChdir_pass(t *testing.T) {
        var wd, _ = os.Getwd()
        var d = t.TempDir()
        os.Chdir(d)
        t.Cleanup(func() {
                os.Chdir(wd)
        })
}

Execute:

go test -run TestChdir_pass
go test -run TestChdir_fail

What did you see happen?

PASS result for TestChdir_pass and FAIL result for
TestChdir_fail with output:

--- FAIL: TestChdir_fail (1.90s)
    testing.go:1464: TempDir RemoveAll cleanup: unlinkat C:\Users\***\AppData\Local\Temp\TestChdir_fail1637014644\001: The process cannot access the file because it is being used by another process.
panic: testing.Chdir: chdir .: The system cannot find the path specified. [recovered, repanicked]

goroutine 7 [running]:
testing.tRunner.func1.2({0x7ff7bbdba540, 0x3fc05c6222d0})
        C:/Program Files/Go/src/testing/testing.go:1974 +0x239
testing.tRunner.func1()
        C:/Program Files/Go/src/testing/testing.go:1977 +0x349
panic({0x7ff7bbdba540?, 0x3fc05c6222d0?})
        C:/Program Files/Go/src/runtime/panic.go:860 +0x13a
testing.(*common).Chdir.func1()
        C:/Program Files/Go/src/testing/testing.go:1579 +0x94
testing.(*common).Cleanup.func1()
        C:/Program Files/Go/src/testing/testing.go:1317 +0x109
testing.(*common).runCleanup(0x3fc05c5fc400, 0x3fc05c675f48?)
        C:/Program Files/Go/src/testing/testing.go:1667 +0xea
testing.tRunner.func2()
        C:/Program Files/Go/src/testing/testing.go:2030 +0x25
testing.tRunner(0x3fc05c5fc400, 0x7ff7bbdfe010)
        C:/Program Files/Go/src/testing/testing.go:2042 +0xed
created by testing.(*T).Run in goroutine 1
        C:/Program Files/Go/src/testing/testing.go:2101 +0x4a9
exit status 2
FAIL    testchdir/chdir 2.179s

What did you expect to see?

Two PASS results, as this occurs when the testchdir module is on the C
drive instead of the Z drive.

Metadata

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.OS-Windows

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions