Open dougthor42 opened 1 month ago
Hi @dougthor42 -
Is this issue reproducible with the latest release? Protobuf Python version 21.7 is no longer supported. You can find the support timelines for more recent versions of Protobuf Python at https://protobuf.dev/support/version-support/#python
:exclamation: :exclamation: :exclamation: Migrated from https://github.com/bazelbuild/rules_python/issues/2133 :exclamation: :exclamation: :exclamation:
🐞 bug report
Affected Rule
py_proto_library
Is this a regression?
I haven't investigated yet, sorry :face_with_diagonal_mouth:
Description
It appears that the modifications to
PYTHONPATH
(added whenpy_proto_library
uses aproto_library
that hasstrip_import_prefix
) can cause multiple paths to be included that have the same python namespaces defined.For example: all of these paths have a
doug
python package in them:This causes any python code that runs
import doug
(orimport doug.x.y.z
) to look in the_virtual_imports
directory because it shows up later insys.path
:Main question: Am I doing something wrong or is this actually (un)intended behavior?
🔬 Minimal Reproduction
Bear with me, as there's a fair bit of setup.
We use
proto_library.strip_import_prefix
because the some protos import others, and those proto import statements do not includesrc
directory, as you'll see below.Once you get the project structure in place, run:
This will print out some debugging info, namely
sys.path
and the path for the importeddoug
package andfoo_pb2
module.Actual Behavior
You'll see that the
doug
package will be loaded fromfoo_bin.runfiles/_main/src/doug/proto2/_virtual_imports/bar_proto/doug/__init__.py
.Expected Behavior
The
doug
package should be loaded fromfoo_bin.runfiles/_main/src/doug/__init__.py
.Investigation
Showing correct import path
Edit
./src/doug/BUILD.bazel
to remove the dependency on//src/doug/proto2:bar_pb2
and runbazel run //src/doug:foo_bin
again.You'll see that the
doug
package is correctly found atfoo_bin.runfiles/_main/src/doug/__init__.py
(yes, the python code will then throw ImportError aboutfoo_pb2.py
, but that's expected).Modifying proto imports fixes things
Revert those changes. Now edit
./src/doug/proto2/BUILD.bazel
and remove thestrip_import_prefix
lines. Runfoo_bin
again and you'll see an error related to building the protos because the proto import path is incorrect.With
strip_import_prefixes
removed, edit./src/doug/proto2/bar.proto
so that the import statement includessrc/
and runfoo_bin
again.Things will work and you'll notice that the
bar_pb2
is being loaded fromfoo_bin.runfiles/_main/src/doug/proto2/bar_pb2.py
- not the_virtual_imports
directory.Sadly this "modify proto imports" fix is not an option for us - we can't update our proto imports without significant work.
Project structure and Files
./MODULE.bazel
and./BUILD.bazel
have typical things you'd see for a python+proto project. We're using rules_python 0.33.1, protobuf 21.7, and rules_proto 5.3.0-21.7.The contents of the proto files don't matter too much, except that one proto file must reference the other via an
import
:🔥 Exception or Error
In "Minimal Reproduction", but the gist is
ModuleNotFoundError
.🌍 Your Environment
Operating System:
gLinux
Output of
bazel version
:7.2.0
Rules_python version:
0.33.1
Anything else relevant?