Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: TempDir fails when TMPDIR is a relative directory and os.Chdir() is called #67954

Closed
arp242 opened this issue Jun 12, 2024 · 2 comments

Comments

@arp242
Copy link

arp242 commented Jun 12, 2024

Go version

1.22.4

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN='/home/martin/.local/gobin'
GOCACHE='/home/martin/.cache/go/cache'
GOENV='/home/martin/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS='-modcacherw'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/martin/.cache/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/martin/.cache/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR='/home/martin/.cache/go/tmp'
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/home/martin/testtmp/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/home/martin/.cache/go/tmp/go-build3133883126=/tmp/go-build -gno-record-gcc-switches'

Setup:

% mkdir tmp
% go mod init testtemp

% cat test_test.go
package main

import (
	"fmt"
	"os"
	"testing"
)

func TestTest(t *testing.T) {
	fmt.Println(t.TempDir())
	fmt.Println(t.TempDir())
	os.Chdir("./tmp")
	fmt.Println(t.TempDir())
}

This runs fine:

% go test
/tmp/TestTest731866446/001
/tmp/TestTest731866446/002
/tmp/TestTest731866446/003

But when setting TMPDIR to ./tmp it stops working after the os.Chdir() call:

% TMPDIR=./tmp go test
./tmp/TestTest4094023708/001
./tmp/TestTest4094023708/002
--- FAIL: TestTest (0.00s)
	test_test.go:13: TempDir: stat ./tmp: no such file or directory
FAIL
exit status 1
FAIL    test    0.002s

It needs to be the full path:

% TMPDIR=/home/martin/testtmp/tmp go test
/home/martin/testtmp/tmp/TestTest2010017600/001
/home/martin/testtmp/tmp/TestTest2010017600/002
/home/martin/testtmp/tmp/TestTest2010017600/003
PASS
ok      test    0.002s

Context: I have some filesystem-specific code that's sensitive to the current directory, so I need to change it. I have ./tmp mounted as a XFS image to test that it works on that, in addition to ext4.

I can just use the full path of course, but it's fairly long and annoying, and "it works fine, except in this one specific case" is rather confusing. And should be easy enough to fix with filepath.Abs() call.

@seankhliao
Copy link
Member

I'd say this is working as intended. You want TMPDIR to be relative to the current directory, and it stays relative to your working directory.

TMPDIR=$(pwd)/tmp isn't that much to type out.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants