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: TempFile Name() stem response varies between 1.16 and 1.17 #48385

Closed
tcolgate opened this issue Sep 14, 2021 · 3 comments
Closed

io/ioutil: TempFile Name() stem response varies between 1.16 and 1.17 #48385

tcolgate opened this issue Sep 14, 2021 · 3 comments

Comments

@tcolgate
Copy link
Contributor

tcolgate commented Sep 14, 2021

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

Go 1.17 and Go 1.16

Does this issue reproduce with the latest release?

Present in Go 1.17.1

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

linux amd64 and darwin

What did you do?

Calling ioutil.TempFile(".","something"), the returned file has a different
response for Name() between go1.17 and go.1.16. In 1.17 the file is qualified
with ./, in 1.16 it was not.

For most practical purposes this is fine, but it breaks when passing the f.Name() directly
to (e.g.) docker daemon, which wants it without the prefix.

package main

import (
        "fmt"
        "io/ioutil"
        "os"
)

func main() {
        f, _ := ioutil.TempFile(".", "myfile")
        defer os.Remove(f.Name())
        fmt.Println(f.Name())
}

What did you expect to see?

Same response for filename stem from 1.16 and 1.17

What did you see instead?

1.17 includes the ./ prefix.

@tcolgate tcolgate changed the title ioutil: TempFile response varies between 1.16 and 1.17 ioutil: TempFile Name() stem response varies between 1.16 and 1.17 Sep 14, 2021
@randall77
Copy link
Contributor

This appears to be a consequence of the fact that os.CreateTemp and ioutil.TempFile behave slightly differently. In 1.17 we redirected the latter to use the former.

https://go-review.googlesource.com/c/go/+/285378

@ianlancetaylor @rsc @odeke-em

@dmitshur dmitshur changed the title ioutil: TempFile Name() stem response varies between 1.16 and 1.17 io/ioutil: TempFile Name() stem response varies between 1.16 and 1.17 Sep 14, 2021
@ianlancetaylor
Copy link
Contributor

In 1.16 ioutil.TempFile called filepath.Join to join the directory and file name. filepath.Join calls filepath.Clean, which eliminates path elements that are just .. In 1.17 ioutil.TempFile redirects to the newer os.CreateTemp, as @randall77 says. That function does not use filepath.Join because the os package can't depend on the path/filepath package. It uses a much simpler version which doesn't eliminate . elements.

This is a change in behavior but it doesn't strike me as being a bug. We don't document any particular behavior for ioutil.TempFile, and passing a relative path as the directory argument is not the usual case. The workaround for this would be to call filepath.Clean on the file name.

@tcolgate
Copy link
Contributor Author

Thanks, figured that may be the case, but thought the difference was worth reporting. (the real mystery is why docker daemon is fine with this for some dockerfiles and not for others, but that's definitely not your problem.

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

4 participants