rules-proto-grpc / rules_proto_grpc

Bazel rules for building Protobuf and gRPC code and libraries from proto_library targets
https://rules-proto-grpc.com
Apache License 2.0
249 stars 156 forks source link

Python proto rule broken when proto files are placed under sub directory of source file. #294

Closed rosun82 closed 6 months ago

rosun82 commented 8 months ago

Issue Description

Imagine the following case:

src
+ a.py
+ a_test.py
  + protos
    + b.proto

Assume a_test.py imports both a.py and c_pb2, e.g.,

from src import a
from src.protos import b_pb2

Here the build will fail with the following kind of error:

Traceback (most recent call last):
  File "/.../bazel-out/k8-opt/bin/src/a_test.runfiles/com_github_???/src/a_test.py", line 5, in <module>
    from src import a
ImportError: cannot import name 'a' from 'src' (/.../bazel-out/k8-opt/bin/src/a_test.runfiles/com_github_???/src/protos/b_py_proto_pb/src/__init__.py

What this says is essentially the proto rule sets a new import path, which shields the original one (repo root).

The temporary solution is to move protos to the repo root, or at least make sure the path of all python file do not have common prefix with all proto file. This, as one would imagine, causes difficulty in organizing projects.

p.s.: there are no such issue with blaze, so I suppose it is fixable.

Log Output

No response

rules_proto_grpc Version

4.5.0

Bazel Version

6.4.0

OS

ubuntu

Link to Demo Repo

No response

WORKSPACE Content

No response

BUILD Content

No response

Proto Content

No response

Any Other Content

No response

aaliddell commented 8 months ago

Do you have legacy_create_init on your py_binary/py_test rules set to False? The default behaviour (implicitly True) is very broken when it comes to overlapping names

rosun82 commented 8 months ago

Thanks for the pointer!

Setting --incompatible_default_to_explicit_init_py=true fixes the issue.

I think this should be made clear somewhere in the document though.

github-actions[bot] commented 6 months ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.