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/vet: reject flag.Parse during func init #33190

Open
rsc opened this issue Jul 19, 2019 · 1 comment
Open

cmd/vet: reject flag.Parse during func init #33190

rsc opened this issue Jul 19, 2019 · 1 comment

Comments

@rsc
Copy link
Contributor

@rsc rsc commented Jul 19, 2019

In #31859, @cespare suggested rejecting flag.Parse during func init, which is always incorrect (other packages not yet initialized may want to define flags).

We could add a special runtime hook of some kind to allow flag to see whether main.main has started, but that would be unfortunate.
There also may be lots of code in the wild that does parse flags during init and kind of works out OK, and if it's working well enough we don't want to break it unnecessarily.

A vet check, on by default during go test, seems like the perfect compromise to me.

@gbbr

This comment has been minimized.

Copy link
Member

@gbbr gbbr commented Sep 21, 2019

A vet check would be very helpful, if it’s accomplishable, as the current output is confusing and misleading. Example:

$ go test ./pkg/trace/api
flag provided but not defined: -test.testlogfile
Usage of /var/folders/02/23s7r95n6t1739kdh0zc3trr0000gn/T/go-build397748628/b001/api.test:
  ...

I'm honestly a bit shocked that such a change went through. This (IMHO) is worse than the original problem being fixed.

gbbr added a commit to DataDog/datadog-agent that referenced this issue Sep 23, 2019
Due to a recent change in go1.13 (golang/go#21051), parsing of
flags inside `init` functions causes tests to fail. This change moves
`flag.Parse` inside `main.main`.

For more information see golang/go#33190
gbbr added a commit to DataDog/datadog-agent that referenced this issue Sep 23, 2019
Due to a recent change in go1.13 (golang/go#21051), parsing of
flags inside `init` functions causes tests to fail. This change moves
`flag.Parse` inside `main.main`.

For more information see golang/go#33190
gbbr added a commit to DataDog/datadog-agent that referenced this issue Sep 23, 2019
Due to a recent change in go1.13 (golang/go#21051), parsing of
flags inside `init` functions causes tests to fail. This change moves
`flag.Parse` inside `main.main`.

For more information see golang/go#33190
gbbr added a commit to DataDog/datadog-agent that referenced this issue Sep 23, 2019
Due to a recent change in go1.13 (golang/go#21051), parsing of
flags inside `init` functions causes tests to fail. This change moves
`flag.Parse` inside `main.main`.

For more information see golang/go#33190
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
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.