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: os.Create & os.OpenFile both not creating .json file #67677

Closed
samjtro opened this issue May 28, 2024 · 7 comments
Closed

os: os.Create & os.OpenFile both not creating .json file #67677

samjtro opened this issue May 28, 2024 · 7 comments

Comments

@samjtro
Copy link

samjtro commented May 28, 2024

Go version

1.22.3

OS

Debian 12.0

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/sam/.cache/go-build'
GOENV='/home/sam/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/sam/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/sam/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/sam/repos/github.com/samjtro/go-trade/go.mod'

What did you do?

Attempted to use os.Create to create a JSON file.

f, err := os.Create("~/.foo/bar.json")
utils.Check(err)

Also attempted to use os.OpenFile directly with the os.O_CREATE flag.

f, err := os.OpenFile("~/.foo/bar.json", os.O_CREATE, 0644) // I tried 0666, 0777 for this as well as the below implementation
utils.Check(err)

Also attempted to use os.WriteFile, another wrapper for os.OpenFile.

err = os.WriteFile("~/.foo/bar.json", tokensJson, 0777)
utils.Check(err)

What did you see happen?

I received the following error, and the file was not created.

2024/05/28 13:03:53 open ~/.foo/bar.json: no such file or directory

What did you expect to see?

Ideally, I would have expected to see no error and the file created.

@samjtro
Copy link
Author

samjtro commented May 28, 2024

I attempted to trace this back, but the furthest I got before giving up was openFileNolog() in file_unix.go.

Not even sure this is correct though.

@j178
Copy link
Contributor

j178 commented May 28, 2024

Does the ~/.foo directory exist?

@samjtro
Copy link
Author

samjtro commented May 28, 2024

~/.foo exists. I am utilizing it for loading in a config.env file earlier in the process, which works fine.

@samjtro
Copy link
Author

samjtro commented May 28, 2024

I reproduced this in a separate project, receiving the same error when attempting to save directly to the home directory as well (I also changed ~ to $HOME, which resulted in the same error).

@neild
Copy link
Contributor

neild commented May 28, 2024

Conversion of ~ or $HOME to the location of your home directory is performed by your shell.

The os package does not perform shell expansion, so os.Create("~/.foo/bar.json") is attempting to create a file in the directory with the literal name "~". This directory presumably does not exist.

@j178
Copy link
Contributor

j178 commented May 28, 2024

You need to expand or $HOME before calling OpenFile.

@samjtro
Copy link
Author

samjtro commented May 28, 2024

Thank you for catching my mistake! I knew I was doing something silly. Closing.

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