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

cmd/go: go generate unexpected EOF error when comment lacks newline at EOF #32607

Closed
logan opened this issue Jun 13, 2019 · 8 comments
Closed

cmd/go: go generate unexpected EOF error when comment lacks newline at EOF #32607

logan opened this issue Jun 13, 2019 · 8 comments

Comments

@logan
Copy link

@logan logan commented Jun 13, 2019

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

$ go version
go version go1.12.6 darwin/amd64

What did you do?

$ echo -e -n 'package main\n//go:generate true' > gen.go
$ go generate ./gen.go
gen.go:2: error reading gen.go: unexpected EOF
$ echo >> gen.go
$ go generate ./gen.go
$
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 13, 2019

This is working as designed. Why do you think it is a bug? go generate expects a go:generate comment to be a normal line comment ending at a newline.

@logan

This comment has been minimized.

Copy link
Author

@logan logan commented Jun 13, 2019

Where is that expectation documented? I don't see that it's a necessary or useful requirement, and it's confusing to encounter in the wild.

I have a small patch to fix this ready (once I sort out CLA concerns with my employer). When an EOF is encountered, the generate comment has been read into a buffer. All it needs is a '\n' appended to it to parse and exec, but the current implementation returns io.ErrUnexpectedEOF instead.

@logan

This comment has been minimized.

Copy link
Author

@logan logan commented Jun 13, 2019

I forgot to mention when I filed this issue, I'm not the first person to run into this. See #18066.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 21, 2019

Let's just define go generate as requiring a full line comment ending in a newline, and move on.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 21, 2019

(If you want to send a very short doc change, we can consider it.)

@logan

This comment has been minimized.

Copy link
Author

@logan logan commented Jun 21, 2019

The only documentation I'm aware of is here: https://golang.org/pkg/cmd/go/internal/generate/

As someone who supports multiple generators for a number of other developers, this is an error I'm likely to be dealing with again. My users are unlikely to be viewing the godocs for that internal package.

In my opinion, if go fmt doesn't force newlines at the end of files (#1178), then go generate shouldn't require them.

I suppose this issue doesn't matter. It's small and the fix is trivial and I'll post it up for review as soon as my employer allows me to.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 21, 2019

I think you are misreading #1178. gofmt does ensure that a Go file ends with a newline.

> echo -e -n 'package main\n//go:generate true' > gen.go
> cat -ve gen.go
package main$
//go:generate true> gofmt -w gen.go
> cat -ve gen.go
package main$
$
//go:generate true$
@logan

This comment has been minimized.

Copy link
Author

@logan logan commented Jun 21, 2019

Ah, my mistake. I guess my colleague who encountered this must not have integrated go fmt with GoLand in the way I expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.