From 2b05d9fba8cd7406c6c73549885210ba4771cc9e Mon Sep 17 00:00:00 2001 From: Ted Pudlik Date: Fri, 10 Nov 2023 11:24:31 -0800 Subject: [PATCH] fix: py_proto_library: append to PYTHONPATH less 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. Fixes #1551 --- python/private/proto/py_proto_library.bzl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/python/private/proto/py_proto_library.bzl b/python/private/proto/py_proto_library.bzl index 116590f1a3..76dd2242b6 100644 --- a/python/private/proto/py_proto_library.bzl +++ b/python/private/proto/py_proto_library.bzl @@ -107,10 +107,13 @@ def _py_proto_aspect_impl(target, ctx): return [ _PyProtoInfo( imports = depset( - # Adding to PYTHONPATH so the generated modules can be imported. - # This is necessary when there is strip_import_prefix, the Python - # modules are generated under _virtual_imports. - [proto_root], + # Adding to PYTHONPATH so the generated modules can be + # imported. This is necessary when there is + # strip_import_prefix, the Python modules are generated under + # _virtual_imports. But it's undesirable otherwise, because it + # will put the repo root at the top of the PYTHONPATH, ahead of + # directories added through `imports` attributes. + [proto_root] if "_virtual_imports" in proto_root else [], transitive = [dep[PyInfo].imports for dep in api_deps], ), runfiles_from_proto_deps = runfiles_from_proto_deps,