2

As the title says, is it possible to generate code (with something like //go:generate) after a module dependency is downloaded?

Specifically, let's say there's a repo example.com/protobuf containing some .proto files and a few .sh scripts for generating bindings in different languages, plus a go.mod so it can be used as a dependency from go like so:

module example.com/application

go 1.18

require (
  example.com/protobuf v1.0.0
)

However, the generated .go files are not included in this repo, they have to be generated using one of the .sh scripts, so if you try this, you get an error like module example.com/protobuf@latest found (v1.0.0), but does not contain package example.com/protobuf/foo

Is there a way around this without resorting to eg. git submodules?

rophl
  • 187
  • 10
  • 1
    No, the generated code should be included in the module. – JimB Jul 05 '22 at 13:09
  • 1
    Are you asking for a way to _automatically_ run `go generate` on a module after downloading? That would be dreadful from a security standpoint (and a possible headache for keeping up the property of being cross-platform, and the requirement to have the necessary tooling installed). All-in-all, I'm with JimB on this; [here](https://stackoverflow.com/a/56417892/720999/) is the reasoning. – kostix Jul 05 '22 at 13:47

1 Answers1

2

No this is not possible for obvious security reasons.

Volker
  • 40,468
  • 7
  • 81
  • 87