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: Allow empty go files #37152

Closed
kstenerud opened this issue Feb 10, 2020 · 5 comments
Closed

cmd/go: Allow empty go files #37152

kstenerud opened this issue Feb 10, 2020 · 5 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@kstenerud
Copy link

When isolating tests, it's convenient to be able to just comment out entire source files by typing CTRL-a, CTRL-/. However, if you do that in a go source file, the compiler complains:

expected 'package', found 'EOF'

What would be nicer would be to have the go compiler simply ignore empty go files, so that we don't have to go back to the top of the file and uncomment that one package line just to keep it happy.

The end result is the same either way: A compilation unit that does nothing.

@dominikh
Copy link
Member

This change would require changing the language specification as well as multiple build systems, libraries for processing Go code, and existing tooling that expects to find package declarations in Go files. That seems vastly out of proportion for something that can be better solved by adding a // +build ignore directive at the top of the file, or finding a new key combination that doesn't comment out the package declaration.

@mvdan
Copy link
Member

mvdan commented Feb 10, 2020

I agree with Dominik. Even if we agreed this is a good idea, it would take a few painful years for all the tooling and libraries out there to catch up.

This proposal is also not complete, in my opinion. What's an empty Go file? It's really not a file containing zero bytes, as you yourself mention that it can have comments. Would those comments be Go syntax? Would build directives in such files be obeyed, like //go:generate an // +build? If you used https://golang.org/pkg/go/parser/#ParseFile on such a file, would it return a nil file, or a file without a package name and all the comments?

@kstenerud
Copy link
Author

kstenerud commented Feb 10, 2020

What's an empty Go file?

"A file with no package declaration and no code to compile" then. The compiler can already detect this. My proposal was simply to ignore this case and throw it away rather than complaining and stopping compilation.

Would those comments be Go syntax? Would build directives in such files be obeyed, like //go:generate an // +build?

Of course. If you comment out the whole file, you'll get // //go:generate, so it doesn't get processed. You have all of the same behaviors as always. No change needed.

If you used https://golang.org/pkg/go/parser/#ParseFile on such a file, would it return a nil file, or a file without a package name and all the comments?

Neither. You'd get an AST of a bunch of comments (unless the parser throws them out?).

@ALTree ALTree changed the title Please allow empty go files cmd7go: Allow empty go files Feb 11, 2020
@ALTree ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 11, 2020
@ALTree ALTree added this to the Unplanned milestone Feb 11, 2020
@ALTree ALTree changed the title cmd7go: Allow empty go files cmd/go: Allow empty go files Feb 11, 2020
@bcmills
Copy link
Contributor

bcmills commented Feb 11, 2020

Given the trivial // +build ignore workaround the @dominikh notes (#37152 (comment)), and the more-or-less equivalent workaround of renaming the file with a different extension, this doesn't seem worth the tooling churn.

Can you give more detail on the motivating use-case? In my experience most packages don't even compile if you comment out an entire non-test source file, and the -run flag is much more precise for selecting which tests to run anyway.

CC @matloob @jayconrod

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 11, 2020
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Mar 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

6 participants