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

os: Getwd doesn't return canonical case of current directory on macOS #20947

Closed
glinton opened this issue Jul 7, 2017 · 7 comments
Closed

os: Getwd doesn't return canonical case of current directory on macOS #20947

glinton opened this issue Jul 7, 2017 · 7 comments
Labels
FrozenDueToAge OS-Darwin WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@glinton
Copy link

glinton commented Jul 7, 2017

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

go version go1.8.3 linux/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/opt/src/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build179514568=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

This may very well be an issue for mac regarding the case sensitivity of their nfs server, but here goes. We have a utility that adds an entry to /etc/exports by grabbing the current directory: os.Getwd(). Window's case insensitivity is handled because os.Getwd() returns syscall.Getwd() on a mac, however, we get whatever the user typed on their cd.

On a darwin machine:

mkdir /tmp/TestDir

cd /tmp/testdir
# fails, as 'PWD' is '/tmp/testdir'
# run playground code here

cd /tmp/TestDir
# works, as 'PWD' is '/tmp/TestDir'
# run playground code here

Build and run this code: https://play.golang.org/p/Yqk90j5ud5 https://play.golang.org/p/QmTFAiLcL4

For the next two expect/result sections, this:

package main

import "os"

func main() {
	cwd, _ := os.Getwd()
	os.Stdout.WriteString(cwd + "\n")
}

What did you expect to see?

Output of printing os.Getwd()

/tmp/TestDir

What did you see instead?

Output of printing os.Getwd()

/tmp/testdir
@bradfitz
Copy link
Contributor

bradfitz commented Jul 7, 2017

I cannot reproduce.

ward5k:foobar bradfitz$ mkdir /tmp/FooBar
ward5k:foobar bradfitz$ cd /tmp/FooBar/
ward5k:foobar bradfitz$ curl --silent https://play.golang.org/p/Yqk90j5ud5.go > getwd.go
ward5k:FooBar bradfitz$ go run getwd.go 
/private/tmp/FooBar
/tmp/FooBar
ward5k:FooBar bradfitz$ cd /tmp/foobar
ward5k:foobar bradfitz$ go run getwd.go 
/private/tmp/FooBar
/tmp/foobar
ward5k:foobar bradfitz$ sw_vers 
ProductName:	Mac OS X
ProductVersion:	10.12.5
BuildVersion:	16F73

It's not clear from your bug report which output you expect from which directory.

@bradfitz bradfitz added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 7, 2017
@bradfitz bradfitz changed the title os.Getwd fails to handle osx' ignorance of case os: Getwd fails to handle osx' ignorance of case Jul 7, 2017
@glinton
Copy link
Author

glinton commented Jul 7, 2017

I've updated the original comment with a more straightforward example - https://play.golang.org/p/QmTFAiLcL4.

I expect it to return the same actual filepath regardless of the way it is cdd to

edit - I imagine there's a reason for it, but moving this block below the syscall one below it, should resolve this. For now, a workaround is to use syscall.Getwd() where appropriate.

@bradfitz bradfitz changed the title os: Getwd fails to handle osx' ignorance of case os: Getwd doesn't return canonical case of current directory on macOS Jul 7, 2017
@davecheney
Copy link
Contributor

davecheney commented Jul 8, 2017 via email

@henvic
Copy link
Contributor

henvic commented Jul 15, 2017

@glinton have you ran into a, possibly, edge case where this is an issue?

@glinton
Copy link
Author

glinton commented Jul 17, 2017

@henvic (from the 'What did you do?' section of the original issue)

This may very well be an issue for mac regarding the case sensitivity of their nfs server, but here goes. We have a utility that adds an entry to /etc/exports by grabbing the current directory: os.Getwd(). Window's case insensitivity is handled because os.Getwd() returns syscall.Getwd(). On a mac, however, we get whatever the user typed on their cd.

So we end up getting permission denied when trying to mount, because the directory is /tmp/ExportedDir but /etc/exports (and the client) only has /tmp/exporteddir. Our current workaround is to use syscall.Getwd() to populate the exports file and provide the client with a target.

@ifarobi
Copy link

ifarobi commented Sep 20, 2017

I have the same issue. I use Go version 1.9.
But my in my case, os.Getwd() not returning the working directory as the projects folder, but it return the temporary build folder, such as var/folders/7d/b5gjzcc15cg04vtgz1xd6fth0000gn/T/go-build340557118/.../.../.

@ianlancetaylor
Copy link
Contributor

I think this is enough of a special case that we aren't going to try to address it. Using $PWD is often the right thing to do. In this case, it apparently isn't, but there is a workaround.

@golang golang locked and limited conversation to collaborators Apr 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge OS-Darwin WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

7 participants