Alan E
Alan E

Reputation: 400

How to make IntelliJ IDEA recognize bazel-generated python protobufs?

I am having a hard time making IntelliJ IDEA recognize generated protobuf sources in python.

I have one proto file, one python file, and BUILD files:

$ find -type f
./WORKSPACE
./src/proto/BUILD
./src/proto/mymessage.proto
./src/python/foo/BUILD
./src/python/foo/foo.py

My python script imports generated python class for the protobuf:

$ cat src/python/foo/foo.py 
from src.proto import mymessage_pb2

message = mymessage_pb2.MyMessage()
message.my_field = "Hello world!"

print(message.my_field)

Python class for the proto is generated this way:

$ cat src/proto/BUILD 
package(default_visibility = ["//visibility:public"])

genrule(
    name = "gen_mymessage_py_proto",
    srcs = ["mymessage.proto"],
    outs = ["mymessage_pb2.py"],
    cmd = "protoc $(location mymessage.proto) --python_out=$(GENDIR) ",
)

py_library(
    name = "mymessage_py_proto",
    srcs = [":gen_mymessage_py_proto"],
)

Bazel runs everything as expected:

$ bazel run src/python/foo:foo 
INFO: Analysed target //src/python/foo:foo (16 packages loaded).
INFO: Found 1 target...
Target //src/python/foo:foo up-to-date:
  bazel-bin/src/python/foo/foo
INFO: Elapsed time: 0.525s, Critical Path: 0.05s
INFO: Build completed successfully, 5 total actions

INFO: Running command line: bazel-bin/src/python/foo/foo
Hello world!

But IntelliJ fails to recognize the import: IntelliJ fails to recognize generated proto import

What can I do to make IntelliJ recognize this import?

Upvotes: 1

Views: 1227

Answers (1)

Alan E
Alan E

Reputation: 400

Bazel didn't support resolving genfile imports. This was fixed as part of issues/144. The fix should be released on November 3.

Upvotes: 1

Related Questions