Skip to content

Commit 85e50d2

Browse files
authored
fix: py_proto_library: append to PYTHONPATH less (#1553)
Only append the `[proto_root]` to PYTHONPATH when it's actually necessary, i.e. when the generated Python modules are under _virtual_imports. Do no append it in the general case, when `[proto_root]` is just the repository root. This makes it much less likely that adding a transitive dep on a `py_proto_library` will reorder the `PYTHONPATH`. Such reordering is undesirable because it may lead to import errors. Fixes #1551
1 parent 793e26b commit 85e50d2

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

python/private/proto/py_proto_library.bzl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ def _py_proto_aspect_impl(target, ctx):
107107
return [
108108
_PyProtoInfo(
109109
imports = depset(
110-
# Adding to PYTHONPATH so the generated modules can be imported.
111-
# This is necessary when there is strip_import_prefix, the Python
112-
# modules are generated under _virtual_imports.
113-
[proto_root],
110+
# Adding to PYTHONPATH so the generated modules can be
111+
# imported. This is necessary when there is
112+
# strip_import_prefix, the Python modules are generated under
113+
# _virtual_imports. But it's undesirable otherwise, because it
114+
# will put the repo root at the top of the PYTHONPATH, ahead of
115+
# directories added through `imports` attributes.
116+
[proto_root] if "_virtual_imports" in proto_root else [],
114117
transitive = [dep[PyInfo].imports for dep in api_deps],
115118
),
116119
runfiles_from_proto_deps = runfiles_from_proto_deps,

0 commit comments

Comments
 (0)