9

I use a code generator plugin to generate the server stub from the OpenAPI specifications.

I want to know if I should commit the generated code to version control.

4
  • Can you specify which generator you are using? A code example would also be nice.
    – sHartmann
    Jun 2, 2019 at 13:46
  • If the specification ever changes, would you manually modify this file or re-generate it? Jun 2, 2019 at 14:15
  • 3
    If you want go get to work on your package and the generated files are required to build the Go code, then commit the generated Go files to your source code control system. Jun 2, 2019 at 14:59
  • For generated code from protoc etc. which is OS neutral, it's common practice to get checkin this code. A makefile is nice to easily allow someone to refresh the code, if they need to add/modify any message types.
    – colm.anseo
    Jun 2, 2019 at 18:16

2 Answers 2

11

In general, yes.

The reasoning is simple: if I'm a downstream user of your code—that is, I want to merely include your library as a dependency or build your application,—there is no point in creating additional burden on me for building your code; I should be able to merely go build/go install your code and call it a day.

If, instead, I intend to actually develop your code—either with the intent to have my changes included upstream or for maintaining these changes unpublished—it's perfectly OK to require me to regenerate certain files where it is required.

0

That would depend on the context of the code generation. ideally you'd want to add a go generate step to your CI pipeline before the test and build steps. In your repo readme file you should add instructions explaining how to generate the files while developing.

However, if the generation is something that can only be run on your development machine than you can add this to your source control.

2
  • 2
    Then your package will not be compatible with go get.
    – boreq
    Jun 2, 2019 at 23:11
  • @boreq this is indeed a context in which you should commit
    – Variant
    Jun 3, 2019 at 7:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.