#30162·protobuf

protobuf_generate() ignores --proto_path passed through PROTOC_OPTIONS

Author: 15daksh-2003Created Sep 25, 2026Updated Sep 25, 2026
Labelsbuguntriaged

What version of protobuf and what language are you using?

Version: v36.1

Language: C++, through the CMake protobuf_generate() function

What supported operating system version are you using (e.g. Linux, Windows) ?

Linux (Arch Linux, x86_64)

What supported runtime / compiler version are you using (e.g. python version, gcc version)

GCC 16.2.1, CMake 4.4.3, Ninja 1.13.2

What did you do?

  1. put the below CMakeLists.txt in an empty directory. It writes a .proto into the build tree and passes that directory to protoc with --proto_path in PROTOC_OPTIONS:

    cmake
    cmake_minimum_required(VERSION 3.16)
    project(repro CXX)
    find_package(protobuf CONFIG REQUIRED)
    
    set(gen_dir ${CMAKE_CURRENT_BINARY_DIR}/gen)
    file(WRITE ${gen_dir}/foo.proto "syntax = \"proto3\";\nmessage Foo { int32 x = 1; }\n")
    
    protobuf_generate(OUT_VAR srcs
      PROTOS ${gen_dir}/foo.proto
      PROTOC_OPTIONS --proto_path=${gen_dir})
    add_custom_target(gen ALL DEPENDS ${srcs})
  2. Configure with the build dir outside the source dir: cmake -S . -B ../build

  3. Configure and build with the build dir inside it: cmake -S . -B build, then cmake --build build twice.

What did you expect to see

Both configure, and the build generates foo.pb.h and foo.pb.cc in the build dir once. That's what protoc does with the same option: protoc --proto_path=<build>/gen --cpp_out=<build> <build>/gen/foo.proto.

What did you see instead?

Step 2 fails to configure:

CMake Error at /usr/lib/cmake/protobuf/protobuf-generate.cmake:131 (message):
  Error: protobuf_generate could not find any correct proto include
  directory.

Step 3 configures, but protobuf_generate() declares build/build/gen/foo.pb.h while protoc writes build/foo.pb.h. So the declared outputs never exist, and protoc seems to rerun on every build.

Anything else we should know about your project / environment

Passing the directory as IMPORT_DIRS ${gen_dir} instead works. So (just thinking out loud): either protobuf_generate() should take -I/--proto_path from PROTOC_OPTIONS into account, ahead of the others as protoc does, or the docs and the error message should say that import roots have to go in IMPORT_DIRS.

Source: protocolbuffers/protobuf