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: WriteFile example returns "No such file or directory" because testdata does not exist #32916

Closed
KolesnikDmitriy opened this issue Jul 3, 2019 · 10 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done. Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@KolesnikDmitriy
Copy link

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

$ go version
go1.12.6

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
https://play.golang.org/

What did you do?

Run example in https://golang.org/pkg/io/ioutil/#WriteFile

What did you expect to see?

Expected file created

What did you see instead?

2009/11/10 23:00:00 open testdata/hello: No such file or directory

Program exited.

@AlexRouSg
Copy link
Contributor

AlexRouSg commented Jul 3, 2019

Are you running the example in the playground or on your system?

EDIT: Sorry was looking at the wrong example

@av86743
Copy link

av86743 commented Jul 3, 2019

Indeed, that link https://golang.org/pkg/io/ioutil/#WriteFile above, which offers much better func TempFile choice, also with examples, deserves an issue on its own.

@agnivade
Copy link
Contributor

agnivade commented Jul 3, 2019

Thanks. The code in the example assumes a testdata directory. Since it cannot find it, it throws an error. It worked so far because the source tree always had a testdata directory.

You are welcome to send a patch.

@agnivade agnivade changed the title ioutil.WriteFile return "No such file or directory" in example io/ioutil: WriteFile example returns "No such file or directory" because testdata does not exist Jul 3, 2019
@agnivade agnivade added help wanted NeedsFix The path to resolution is known, but the work has not been done. Suggested Issues that may be good for new contributors looking for work to do. labels Jul 3, 2019
@agnivade agnivade added this to the Unplanned milestone Jul 3, 2019
@darkLord19
Copy link
Contributor

@cuonglm
Copy link
Member

cuonglm commented Jul 4, 2019

@darkLord19 Examples are run in separate environment, without the directory structure as in source code. ReadFile has the same problem as it read testdata/hello file.

@gopherbot
Copy link

Change https://golang.org/cl/185037 mentions this issue: io/ioutil: make Read/WriteFile examples runnable in playground

@av86743
Copy link

av86743 commented Jul 9, 2019

Regarding https://golang.org/cl/185037 :

$ find go/src/github.com/av86743/go/src -type f -name 'example_test.go' -exec grep -e '"testdata' {} ';' -print 
	r, err := zip.OpenReader("testdata/readme.zip")
go/src/github.com/av86743/go/src/archive/zip/example_test.go
	cert, err := tls.LoadX509KeyPair("testdata/example-cert.pem", "testdata/example-key.pem")
go/src/github.com/av86743/go/src/crypto/tls/example_test.go
	content, err := ioutil.ReadFile("testdata/hello")
	err := ioutil.WriteFile("testdata/hello", message, 0644)
go/src/github.com/av86743/go/src/io/ioutil/example_test.go
$

@bcmills
Copy link
Contributor

bcmills commented Jul 15, 2019

I believe this is a specific case of the broader issue #19825.

@fgm
Copy link

fgm commented Oct 13, 2019

Does the fix need to bring the testdata/hello file by changing the playground implementation, or could it just read one of the few files actually available, readable and non-empty test files on the playground, currently just /etc/group and /etc/hosts ? The latter would actually be demonstrating the purpose of ioutil.ReadFile, without touching anything else anywhere.

oshoval added a commit to oshoval/sriov-network-operator-1 that referenced this issue Dec 23, 2020
tryCreateUdevRule creates a udev file by using WriteFile
WriteFile will not create the directory hierarchy in case
it doesn't exist [1].

Therefore create the folder in case it doesn't exist.

[1] golang/go#32916

Related to k8snetworkplumbingwg#10

Signed-off-by: Or Shoval <oshoval@redhat.com>
pliurh pushed a commit to pliurh/sriov-network-operator-2 that referenced this issue Jan 5, 2021
tryCreateUdevRule creates a udev file by using WriteFile
WriteFile will not create the directory hierarchy in case
it doesn't exist [1].

Therefore create the folder in case it doesn't exist.

[1] golang/go#32916

Related to k8snetworkplumbingwg#10

Signed-off-by: Or Shoval <oshoval@redhat.com>
@gopherbot
Copy link

Change https://golang.org/cl/284452 mentions this issue: io/ioutil: fix example test for WriteFile to allow it to run in the playground

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done. Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants