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

filepath.Abs returns different values when passed "." #41670

Closed
hbt opened this issue Sep 28, 2020 · 3 comments
Closed

filepath.Abs returns different values when passed "." #41670

hbt opened this issue Sep 28, 2020 · 3 comments

Comments

@hbt
Copy link

hbt commented Sep 28, 2020

What version of Go are you using (go version)?

$ go version go version go1.14.2 linux/amd64


Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/hassen/.cache/go-build"
GOENV="/home/hassen/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/hassen/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/hassen/goroot"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/hassen/goroot/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build324432177=/tmp/go-build -gno-record-gcc-switches"

What did you do?

calling filepath.Abs returns different values based on whether the program is ran in terminal or in CI/CD system because it sometimes uses Getenv("PWD") OR does syscall.Getwd

https://play.golang.org/p/lvU1dF0dFcE

What did you expect to see?

I expect the filepath.Abs to return the same value and be consistent and either always call syscall.Getwd or always use PWD

What did you see instead?

Different values. View https://play.golang.org/p/lvU1dF0dFcE example

@ianlancetaylor
Copy link
Contributor

The way it works is that if the environment variable PWD is set, and it names the same directory as the current directory, then we use the value in PWD. This generally gives a better result when using a Unix shell. If PWD is not set, then we call the getcwd system call. If you need some sort of canonicalized path that is independent of PWD, use filepath.EvalSymlinks.

Closing because this is working as intended.

@hbt
Copy link
Author

hbt commented Sep 28, 2020

I already worked around it by evaluating the symlinks but I wouldn't call this "working as intended."

having the api rely on both PWD and getcwd is wrong.
os.Chdir doesn't change PWD and vice-versa. Which is fine.

The API should pick one lookup method though.

Instead we had a program that passes the test suite but failed in production because the golang API used the PWD environment variable in production and getcwd in the test suite.

@ianlancetaylor
Copy link
Contributor

I don't agree that there is an obvious right answer here. The approach that Go takes is the one that most Unix shells take, and it produces appropriate results when a user uses cd to get to a directory and then runs a Go program that uses that directory.

@golang golang locked and limited conversation to collaborators Sep 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants