How to build classes protobuf (protoc) from a go based project (cortex)

Viewed 29

I'm trying to follow the instructions here to create python classes from a protobuf defanition found here.

When I run:

protoc -I=.  --python_out=target cortex.proto

I'm stuck with an error:

cortex.proto:7:1: Import "github.com/gogo/protobuf/gogoproto/gogo.proto" was not found or had errors.

It's clear to me that I also need to clone a dependency. I did this manually with git clone https://github.com/gogo/protobuf but was then stuck with a dependency of the dependency.

I believe what I need to do is to use go to pull the full set of dependencies for me. I tried go get but this results with the dependencies being cloned with names such as ~/go/pkg/mod/github.com/gogo/protobuf@v1.3.2 (including the tag). And protoc still can't find them as a result.

I think I'm missing something simple about how go works. I've not spent much time with go so my suspicion is that I've simply not found the correct command to pull the dependencies in a way that protobuf will accept.

How do you compile protobuf files from a go project (using go dependencies)?

1 Answers

In this instance it looks like the command I had missed was go mod vendor which I discovered on this answer https://stackoverflow.com/a/65874584/453851. The command copies the download modules into the vendor folder at the root of the project repo.

To Build coretex.proto as python

On my Mac with go and protoc installed:

git clone https://github.com/cortexproject/cortex.git
cd cortex/pkg/cortexpb
go get
go mod vendor

cd ../..  # Root of the cloned cortex project
mkdir python
# Don't forget to include the vendor directory
protoc -I=vendor -I=pkg --python_out=python pkg/cortexpb/cortex.proto
# You may need to compile others from vendor to get the python module working
protoc -I=vendor --python_out=python vendor/github.com/gogo/protobuf/gogoproto/gogo.proto

# Populate missing __init__.py files
find python -type d -exec touch {}/__init__.py ';'

This leaves you with the python classes in a python directory.

Related