Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bazel] Enable grpcio-reflection to be used via Bazel #31013

Merged
merged 4 commits into from
Aug 16, 2023
Merged

[Bazel] Enable grpcio-reflection to be used via Bazel #31013

merged 4 commits into from
Aug 16, 2023

Conversation

CareF
Copy link
Contributor

@CareF CareF commented Sep 16, 2022

The current py_grpc_library results in the wrong grpc proto python code path when grpc is a third-party source code in a Bazel project. This PR should fix it.

fixes #31011

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 16, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

@gnossen
Copy link
Contributor

gnossen commented Sep 16, 2022

Can you please add a test case to this directory?

@CareF
Copy link
Contributor Author

CareF commented Sep 18, 2022

Can you please add a test case to this directory?

Thanks for the remainder. I realize that there are more problems with this issue than what I have already done.
This PR fixes the pb2_grpc.py file path but yet still the generated pb2.py file tries to import beyond top-level package:

Under test/distrib/bazel/python/, the generated ./bazel-bin/external/com_github_grpc_grpc/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection_pb2.py file is

from ...com_github_grpc_grpc.src.proto.grpc.reflection.v1alpha.reflection_pb2 import *

with or without the change to proto plugin argument (my original fix to #31011). This will trigger the error ValueError: attempted relative import beyond top-level package

Traceback (most recent call last):
  File "/home/user/.cache/bazel/_bazel_lm/ce2d8b67d17f2cc0fd23ca117ea353ce/execroot/python_test_repo/bazel-out/k8-fastbuild/bin/import_from_grpcio_reflection_test.runfiles/python_test_repo/import_from_grpcio_reflection.py", line 17, in <module>
    from grpc_reflection.v1alpha import reflection
  File "/home/user/.cache/bazel/_bazel_lm/ce2d8b67d17f2cc0fd23ca117ea353ce/execroot/python_test_repo/bazel-out/k8-fastbuild/bin/import_from_grpcio_reflection_test.runfiles/com_github_grpc_grpc/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py", line 19, in <module>
    from grpc_reflection.v1alpha import reflection_pb2 as _reflection_pb2
  File "/home/user/.cache/bazel/_bazel_lm/ce2d8b67d17f2cc0fd23ca117ea353ce/execroot/python_test_repo/bazel-out/k8-fastbuild/bin/import_from_grpcio_reflection_test.runfiles/com_github_grpc_grpc/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection_pb2.py", line 1, in <module>
    from ...com_github_grpc_grpc.src.proto.grpc.reflection.v1alpha.reflection_pb2 import *
ValueError: attempted relative import beyond top-level package

To fix I have to add the following which is a little bit beyond the original scope:

diff --git a/bazel/python_rules.bzl b/bazel/python_rules.bzl
index d6345a4762..1fe61ca2bc 100644
--- a/bazel/python_rules.bzl
+++ b/bazel/python_rules.bzl
@@ -134,7 +134,7 @@ def _generate_py_impl(context):
         for py_src in context.attr.deps[0][PyProtoInfo].generated_py_srcs:
             reimport_py_file = context.actions.declare_file(py_src.basename)
             py_sources.append(reimport_py_file)
-            import_line = "from %s import *" % py_src.short_path.replace("/", ".")[:-len(".py")]
+            import_line = "from %s import *" % py_src.short_path.replace("..", "external").replace("/", ".")[:-len(".py")]
             context.actions.write(reimport_py_file, import_line)
 
     # Collect output PyInfo provider.
···

@CareF
Copy link
Contributor Author

CareF commented Sep 25, 2022

@gnossen PTAL

@gnossen gnossen added lang/Python release notes: yes Indicates if PR needs to be in release notes labels Oct 4, 2022
@gnossen gnossen changed the title Bug fix: py_grpc_library output file path as external Enable grpcio-reflection to be used via Bazel Oct 4, 2022
@CareF
Copy link
Contributor Author

CareF commented Oct 5, 2022

[1331/1338] Linking CXX executable test_core_iomgr_timer_list_test.exe

FAILED: test_core_iomgr_timer_list_test.exe

cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\test_core_iomgr_timer_list_test.dir --manifests  -- C:\PROGRA~2\MIB055~1\2017\COMMUN~1\VC\Tools\MSVC\1414~1.264\bin\Hostx86\x86\link.exe /nologo @CMakeFiles/test_core_iomgr_timer_list_test.rsp  /out:test_core_iomgr_timer_list_test.exe /implib:test_core_iomgr_timer_list_test.lib /pdb:test_core_iomgr_timer_list_test.pdb /version:0.0  /machine:X86 /debug /INCREMENTAL /subsystem:console  && cd ."

FINAL LINK: command "C:\PROGRA~2\MIB055~1\2017\COMMUN~1\VC\Tools\MSVC\1414~1.264\bin\Hostx86\x86\link.exe /nologo @CMakeFiles/test_core_iomgr_timer_list_test.rsp /out:test_core_iomgr_timer_list_test.exe /implib:test_core_iomgr_timer_list_test.lib /pdb:test_core_iomgr_timer_list_test.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console /MANIFEST /MANIFESTFILE:CMakeFiles\test_core_iomgr_timer_list_test.dir/intermediate.manifest CMakeFiles\test_core_iomgr_timer_list_test.dir/manifest.res" failed (exit code 1) with the following output:
LINK : the 32-bit linker (C:\PROGRA~2\MIB055~1\2017\COMMUN~1\VC\Tools\MSVC\1414~1.264\bin\Hostx86\x86\link.exe) failed to do memory mapped file I/O on `grpc.lib' and is going to restart linking with a 64-bit linker for better throughput

LINK : restarting link with 64-bit linker `C:\tools\msys64\usr\bin\link.exe'

link: unknown option -- i
Try '/c/PROGRA~2/MIB055~1/2017/COMMUN~1/VC/Tools/MSVC/1414~1.264/bin/Hostx86/x86/link --help' for more information.

seems to be completely irreverent to this PR

@CareF
Copy link
Contributor Author

CareF commented Oct 17, 2022

@gnossen Anything else I need to do?

@yashykt yashykt assigned gnossen and unassigned yashykt Apr 12, 2023
@aschleck
Copy link
Contributor

@gnossen is it possible to get this submitted? It's blocking our usage of grpcio-reflection so it'd be great to have this bug fixed.

@CareF
Copy link
Contributor Author

CareF commented Aug 7, 2023

I've been using this commit as a patch in my private projects for a while and didn't see any issues.

@gnossen gnossen changed the title Enable grpcio-reflection to be used via Bazel [Bazel] Enable grpcio-reflection to be used via Bazel Aug 16, 2023
Copy link
Contributor

@gnossen gnossen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Sorry for the delay.

@gnossen gnossen enabled auto-merge (squash) August 16, 2023 20:44
@gnossen gnossen merged commit c8f467a into grpc:master Aug 16, 2023
53 of 67 checks passed
eugeneo pushed a commit to eugeneo/grpc that referenced this pull request Aug 21, 2023
The current `py_grpc_library` results in the wrong grpc proto python
code path when grpc is a third-party source code in a Bazel project.
This PR should fix it.

fixes grpc#31011

<!--

If you know who should review your pull request, please assign it to
that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the
appropriate
lang label.

-->

---------

Co-authored-by: Richard Belleville <rbellevi@google.com>
@CareF CareF deleted the external_grpc_proto branch August 31, 2023 06:28
@mering
Copy link

mering commented Jun 3, 2024

@CareF @gnossen The change in bazel/python_rules.bzl breaks us when trying to upgrade with the message:

output 'external/MODULE~override/path/to/MYPROTO_pb2_grpc.py' was not created

When reverting this change and always using out_dir.path compilation works fine in your code base.

Any suggestion on how to move forward?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fail to build grpcio_reflection when using grpc as bazel external dependence
6 participants