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

Open
kstenerud opened this issue Feb 10, 2020 · 4 comments
Open

cmd/go: Allow empty go files #37152

kstenerud opened this issue Feb 10, 2020 · 4 comments

Comments

@kstenerud
Copy link

@kstenerud kstenerud commented Feb 10, 2020

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

This comment has been minimized.

Copy link
Member

@dominikh dominikh commented Feb 10, 2020

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

This comment has been minimized.

Copy link
Member

@mvdan 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

This comment has been minimized.

Copy link
Author

@kstenerud 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 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

This comment has been minimized.

Copy link
Member

@bcmills 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.