golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.52k stars 17.6k forks source link

x/tools/cmd/stringer: replicate build tags in output file #47179

Open josharian opened 3 years ago

josharian commented 3 years ago

We use stringer in some platform-specific code. In order to keep the code compiling, we need the code that stringer outputs to be protected by a build tag.

I propose that stringer replicates into its output any build tags present in the file in which the type is defined. The type will only be defined when those build tags are present, so not doing this replication generates broken code.

We are currently manually editing the code after generation, which breaks our attempt to have CI check that go generate has been run. We put in this CI check after failure to run go generate caused bugs. Alas, not all code generation is amenable to tricks making such a CI check unnecessary.

Alternatives to this suggestion:

These alternatives might be independently useful, but it seems to me that getting build tags right automatically is a good place to start.

cc @robpike @bradfitz

cherrymui commented 3 years ago

cc @mvdan