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

io/ioutil: TempDir fails if "/tmp" doesn't exist and first argument is empty #14196

Closed
deweerdt opened this issue Feb 2, 2016 · 7 comments
Closed
Milestone

Comments

@deweerdt
Copy link

deweerdt commented Feb 2, 2016

Seen on go1.5.2 linux/amd64, running a program using io.ioutil.TempDir() inside a docker image that has neither TMPDIR defined nor /tmp created.
Calling io.ioutil.TempDir() fails with: mkdir ... no such file or directory.

In the special case where TempDir (or TempFile) are called with an empty first argument, it feels like we could try to mkdir the directory before erroring out of the function.

Small repro case:

$ cat test1.go
package main

import (
        "io/ioutil"
)

func main() {
        _, err := ioutil.TempDir("", "blah")
        if err != nil {
                println(err.Error())
        }
}
$ go build test1.go
$ mkdir dir
$ mv test1 dir
$ sudo chroot ./dir /test1
mkdir /tmp/blah629598329: no such file or directory
$

@bradfitz bradfitz changed the title io.ioutil.TempDir() fails if "/tmp" doesn't exist and first argument is empty io/ioutil: TempDir fails if "/tmp" doesn't exist and first argument is empty Feb 2, 2016
@bradfitz bradfitz added this to the Go1.7 milestone Feb 2, 2016
@bradfitz
Copy link
Contributor

bradfitz commented Feb 2, 2016

Or we can just make the error message better. I don't like the idea of creating /tmp automatically, because my "/" filesystem is likely small, slow, read-only or otherwise special. I think it's a surprise to put /tmp there. If /tmp isn't there, it's probably because of some problem and I'd prefer to explode and learn about the problem than make things worse.

Now that you know your problem, you can create /tmp in your Docker container and/or set TMPDIR.

@deweerdt
Copy link
Author

deweerdt commented Feb 2, 2016

Now that you know your problem, you can create /tmp in your Docker container and/or set TMPDIR.

Yes, it's definitely something that can be addressed in the caller program.
I opened the issue because one way to understand passing "" as a base directory is 'I don't care where, just make it work', but I also can see how mkdir() the directory would be surprising. A better error message might be the way to go.

@odeke-em
Copy link
Member

odeke-em commented Feb 3, 2016

I've taken a stab at this with https://go-review.googlesource.com/19178. The only problem is, how could I test this as changing out os.TempDir is impractical. Also a code review is pending.

@rakyll
Copy link
Contributor

rakyll commented Feb 10, 2016

I opened the issue because one way to understand passing "" as a base directory is 'I don't care where, just make it work'

ioutil gives a reference to the os.TempDir. Neither ioutil.TempDir or ioutil.TempFile are meant to be just working if an empty dir is given.

I don't like the fact that we are returning a custom error from ioutil functions whereas users who depend on os.TempDir directly need to handle a different error. I think more docs would help, noting what happens if the directory doesn't exist.

Your issue also applies for the arbitrary values of the dir. What does the following code suppose to return? Custom error?

ioutil.TempDir("/doesnt-exist", "foo")

@deweerdt
Copy link
Author

Your issue also applies for the arbitrary values of the dir. What does the following code suppose to return? Custom error?

I think of that case differently: since we explicitly selected a value, it's reasonable to expect the caller to set the environment up.

@gopherbot
Copy link

CL https://golang.org/cl/19178 mentions this issue.

@rsc rsc modified the milestones: Go1.8, Go1.7 May 18, 2016
@bradfitz bradfitz self-assigned this Sep 8, 2016
@gopherbot
Copy link

CL https://golang.org/cl/28772 mentions this issue.

@golang golang locked and limited conversation to collaborators Sep 8, 2017
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

6 participants