Skip to content

py_proto_library prepends too aggressively to the PYTHONPATH  #1551

@tpudlik

Description

@tpudlik

🐞 bug report

Affected Rule

py_proto_library

Is this a regression?

Yes, this is a regression due to #1406. (The failing provided below passes if you check out rules_python from the previous commit.)

Description

#1406 added support for generating Python code for proto libraries with strip_import_prefix attributes. This is great! Our project (http://pigweed.dev) uses such attributes (to my chargin, but they're here to stay), so I'm excited for it.

But the solution adopted in #1406 involves quite aggressive prepending to the PYTHONPATH. In particular, I find that if a generated Python proto library is added to the transitive dependencies of a target, then the repository root, and the roots of external repositories like com_google_protobuf, get added close to the top of the PYTHONPATH, ahead of paths coming from import attributes. This can lead to module name collisions at import time.

The description above is a little hard to parse, so I created a small repro (see below).

🔬 Minimal Reproduction

https://github.com/tpudlik/rules_python_path/

🔥 Exception or Error

Depending on whether you're importing a module or an object from a module, you'll hit a ModuleNotFoundError or an ImportError.

🌍 Your Environment

Operating System:

Linux

Output of bazel version:

  
Build label: 6.4.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Oct 19 17:07:43 2023 (1697735263)
Build timestamp: 1697735263
Build timestamp as int: 1697735263
  

Rules_python version:

  
bee35ef2abb0a1b59123500528c7d4ca0cd8a688
  

Anything else relevant?

https://issues.pigweed.dev/issues/266950138 tracks our migration to the rules_python version of py_proto_library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions