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 errors when run on the Go Playground #40623

Closed
mntlty opened this issue Aug 6, 2020 · 5 comments
Closed

io/ioutil: WriteFile example errors when run on the Go Playground #40623

mntlty opened this issue Aug 6, 2020 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mntlty
Copy link

mntlty commented Aug 6, 2020

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

latest

Does this issue reproduce with the latest release?

yes

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

Go Playground https://play.golang.org/

What did you do?

run example at https://golang.org/pkg/io/ioutil/#WriteFile which links to https://play.golang.org/p/bIP2HMSXz1u

What did you expect to see?

no error

What did you see instead?

error: `open testdata/hello: no such file or directory

Suggested Fixes

WriteFile does not create a directory, and the code example tries to write to the non-existent directory testdata. Possible solution are either to create this directory in the example code or change the argument to WriteFile to just include the file name

@gopherbot gopherbot added this to the Unreleased milestone Aug 6, 2020
@tpaschalis
Copy link
Contributor

tpaschalis commented Aug 6, 2020

You're right! In the background, ioutil.WriteFile uses os.OpenFile, and has been doing so since 2011, which doesn't create any intermediate directories.

I think that we could indeed change the example.
My 2¢ would be to add a couple of lines that create a directory in the example. We could also avoid the testdata name since it holds a special meaning in Go.

I'm not sure about documenting this behavior though since we don't explicitly mention anything about directories there.

@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 7, 2020
@toothrot
Copy link
Contributor

toothrot commented Aug 7, 2020

/cc @dmitshur

@dmitshur dmitshur changed the title x/website: ioutil.WriteFile example is broken io/ioutil: WriteFile example is broken Aug 7, 2020
@dmitshur dmitshur modified the milestones: Unreleased, Backlog Aug 7, 2020
@dmitshur dmitshur changed the title io/ioutil: WriteFile example is broken io/ioutil: WriteFile example errors when run on the Go Playground Aug 7, 2020
@dmitshur
Copy link
Contributor

dmitshur commented Aug 7, 2020

The testdata directory exists on disk. However, the example doesn’t have an // Output comment, so it’s only meant to be compiled and displayed in documentation, but not executed:

$ go test -v -run ExampleWriteFile io/ioutil
testing: warning: no tests to run

It doesn’t work on the Go Playground because the testdata directory isn’t available in that environment. But given there isn’t an // Output comment, maybe there shouldn’t be a button to run it on the website.

@mntlty
Copy link
Author

mntlty commented Aug 7, 2020

@dmitshur the issue isn't really about the playground but that the example as written doesn't work without other steps that are not included here, like "must have this directory before running this code"

@AlexanderYastrebov
Copy link
Contributor

Seems like duplicate of #32916 fixed by https://golang.org/cl/284452

@mntlty mntlty closed this as completed Nov 17, 2021
@golang golang locked and limited conversation to collaborators Nov 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants