Protobuf: Bug Report: The output file location of `protoc` is not expected under certain circumstances.

Created on 5 Jun 2017  路  3Comments  路  Source: protocolbuffers/protobuf

protobuf version: 3.2
System: Ubuntu 16.04

When --proto-path is specified twice with certain setup, the --cpp_out will not be the direct output directory.

Set up

  • .proto file: ~/project/proto/main.proto
  • another .proto file: ~/project/proto/shared.proto

Process

  1. change directory to ~/project/build/
  2. run
    /usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/ -I ~/project/proto/ ~/project/proto/hello.proto

Result
Output files are under ~/project/build/proto/ but not ~/project/build/, which is unexpected.

  1. Remove first ipath:
    /usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/proto/ ~/project/proto/hello.proto

Result
Output files are under ~/project/build/, which is expected.

Most helpful comment

The directory layout of generated files is determined by their import name (the name you use in the 'import "path/to/other.proto";' statement to import that .proto file) and the import name as I explained in https://github.com/google/protobuf/issues/3044 is determined by matching the .proto file path against the --proto_path flags in order.

For

/usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/ -I ~/project/proto/ ~/project/proto/hello.proto

The import name is "proto/hello.proto" because ~/project/proto/hello.proto matches the first "-I ~/project", and as such protoc will generate ~/project/build/proto/hellp.pb.h|cc.

If you instead do

/usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/proto -I ~/project ~/project/proto/hello.proto

The import name will be "hello.proto" because "-I ~/project/proto" will be matched first.

Usually it's better to just put all .proto files in a single directory tree and specify the -I flag only once on the command line. More complex setup is usually not necessary and is very easy to get it wrong.

All 3 comments

The correct command line parameter for --cpp_out is: protoc --cpp_out=~/project/build/

@adhesiveNOR both forms are valid.

Part of the mechanism is explained in :
https://github.com/google/protobuf/issues/3044

Seems like searching in ipath is recursive. It goes deep down to the every leaf of directory tree. Multiple ipath are assigned priorities.

The directory layout of generated files is determined by their import name (the name you use in the 'import "path/to/other.proto";' statement to import that .proto file) and the import name as I explained in https://github.com/google/protobuf/issues/3044 is determined by matching the .proto file path against the --proto_path flags in order.

For

/usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/ -I ~/project/proto/ ~/project/proto/hello.proto

The import name is "proto/hello.proto" because ~/project/proto/hello.proto matches the first "-I ~/project", and as such protoc will generate ~/project/build/proto/hellp.pb.h|cc.

If you instead do

/usr/local/bin/protoc --cpp_out ~/project/build/ -I ~/project/proto -I ~/project ~/project/proto/hello.proto

The import name will be "hello.proto" because "-I ~/project/proto" will be matched first.

Usually it's better to just put all .proto files in a single directory tree and specify the -I flag only once on the command line. More complex setup is usually not necessary and is very easy to get it wrong.

Was this page helpful?
0 / 5 - 0 ratings