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
250 stars 157 forks source link

Python rules_proto_grpc causes Import errors? #268

Closed siva-tonita closed 1 year ago

siva-tonita commented 1 year ago

Issue Description

Thank you for a lovely repo!

We tried using rules_proto_grpc to set up a Python gRPC server, and are running into a strange error where including a python_grpc_library target as a dependency causes unexpected (and incorrect) import errors elsewhere.

Here's a minimal "toy" version where we're seeing errors (on a n2-standard-8 VM on Google Cloud, with Python 3.10.11).

Three directories under the repo root: A, A/B, and A/B/C.

Trying to bazel run A/B:bar works if the dependency "/A/B/C:baz_py_grpc" stays commented out; if it is UNCOMMENTED, the bazel run generates the error "ModuleNotFoundError: No module named 'A.foo'" -- and we're absolutely stumped.

It is not clear at all why that dependency messes with the normal Python import structure. Would be great if it can be debugged / fixed. Thanks in advance! rules_proto_grpc_issue.tar.gz

Log Output

INFO: Analyzed target //A/B:bar (1 packages loaded, 2 targets configured).
INFO: Found 1 target...
Target //A/B:bar up-to-date:
  bazel-bin/A/B/bar
INFO: Elapsed time: 0.291s, Critical Path: 0.01s
INFO: 4 processes: 4 internal.
INFO: Build completed successfully, 4 total actions
INFO: Running command line: bazel-bin/A/B/bar
goodbye
Traceback (most recent call last):
  File "/home/siva/.cache/bazel/_bazel_siva/30dd2e19d9d01a6c7490ff8e43d248db/execroot/__main__/bazel-out/k8-fastbuild/bin/A/B/bar.runfiles/__main__/A/B/bar.py", line 4, in <module>
    from A.foo import Foo
ModuleNotFoundError: No module named 'A.foo'

rules_proto_grpc Version

4.4.0

Bazel Version

6.2.1

OS

Debian

Link to Demo Repo

No response

WORKSPACE Content

See attached file.

BUILD Content

See attached file.

Proto Content

See attached file.

Any Other Content

No response

siva-tonita commented 1 year ago

In particular, without the python_grpc_library dependency //A/B/C:baz_py_grpc. bazel run A/B:bar legitimately builds A:foo (as can be verified by the presence of A/__pycache__/ with foo.cpython-310.pyc inside it (this is all under bazel-bin/A/B/bar.runfiles/__main__/). Similarly, if we just run bazel build A:foolib, we see the __pycache__ dir.

However, with that dependency //A/B/C:baz_py_grpc, I am not seeing the __pycache__ directory, confirming that the target A:foolib is not built.

aaliddell commented 1 year ago

Have you tried setting legacy_create_init = False, on your py_binary? The default mode is somewhat broken how it handles overlapping import trees

siva-tonita commented 1 year ago

Thank you for the suggestion; fixes our problem (for now; hopefully it will be easy to identify the missing __init__.py requirements in srcs and deps for any future breakages).

Closing the Issue with much appreciation for your help!