rules_proto_grpc icon indicating copy to clipboard operation
rules_proto_grpc copied to clipboard

Generate python code without requiring py_library imports

Open neilisaac opened this issue 2 years ago • 2 comments

Description

For a given proto file first/second/mylib.proto, I would like to generate runfiles/workspace/first/second/mylib_pb2.py, however

  • NO_PREFIX produces runfiles/workspace/first/second/first/second/mylib_pb2.py
  • PREFIXED produces runfiles/workspace/first/second/mylib_py_proto_generated_py/first//second/mylib_pb2.py

Both of these depend on py_library imports flag in order to set PYTHONPATH correctly at runtime. This is done in the generated py_binary wrapper.

If I want to use this library without running a py_binary entrypoint, the PYTHONPATH would be difficult to predict. An example of this would be a python module that is loaded using the cpython API instead of being run as a standalone executable.

I propose adding a non-default mode ex. NO_PREFIX_FLAT which writes the generated files to the output path without any prefixes.

neilisaac avatar Apr 12 '22 13:04 neilisaac

This would also help to make linting / auto-completion simpler

YvanGuidoin avatar May 23 '22 16:05 YvanGuidoin

The correct way of consuming Python outputs without using py_binary is to use the PyInfo provider that py_library gives you. This provider contains all the import path details necessary to resolve imports without using the py_binary stub.

I don't mind taking the PR, but beware it might not solve your problems, as the issue with NO_PREFIX_FLAT is that any duplicate file names will collide in the flat namespace. e.g this will error:

  • package/a/thing.proto
  • package/b/thing.proto

as both will attempt to write to package/thing_pb2.py in the package root. Also, the files won't appear back in your source tree if the goal is interoperating with non-Bazel linters etc, as the outputs still end up in bazel-out etc.

aaliddell avatar Jun 21 '22 22:06 aaliddell