brash-baker-91190
02/03/2021, 6:26 PMpython_source_root
to the protobuf_library
target (https://www.pantsbuild.org/docs/protobuf#protobuf-and-source-roots). The documentation suggests that this will put the generated code in the specified source root; however, in practice it looks like it just creates that directory structure under dist/codegen
.
Additionally, dependency resolution for code that's using the generated *_pb2
modules doesn't appear to be working (as judged by the output of ./pants dependencies path/to/the/file/that/uses/protobufs.py
Another wrinkle is that we'd like to create a Python distribution containing just the generated protobuf code, but I'm not even sure how to express that in Pants notation... particularly with the need to have __init__.py
files created as well.
There seem to be a lot of moving parts here, and I'm not sure if there's a bug or (more likely) some incomplete understanding on my part, possibly coupled with some inconsistent file layout choices in this repository.
That's a lot of information, and I can provide more as needed... I just wanted to lay out my perception of the high-level issues as a way to hopefully start a discussion. Thanks in advance for any advice folks may have.happy-kitchen-89482
02/03/2021, 6:34 PMbrash-baker-91190
02/03/2021, 6:35 PMhundreds-father-404
02/03/2021, 6:36 PMbrash-baker-91190
02/03/2021, 6:38 PMbrash-baker-91190
02/03/2021, 6:38 PMhundreds-father-404
02/03/2021, 6:39 PMhowever, in practice it looks like it just creates that directory structure under dist/codegen.You're running
./pants export-codegen
to get this, right? That is expected. That goal is purely for you to be able to view what files Pants is generating and to consume it externally, e.g. for IDE autocompletion.
You do not need to ever manually run that export-codegen
goal for codegen to work. Pants runs things in chroots. It will include the generated files in those chroots for you, and it should always be up-to-date. export-codegen
will show you what those final paths are, but with a dist/codegen
prefix.brash-baker-91190
02/03/2021, 6:41 PMbrash-baker-91190
02/03/2021, 6:47 PMfoo/bar/baz
, and I set that as a root in pants.toml
and as a python_source_root
for my protobuf_library()
, I should expect the generated code to ultimately reside in foo/bar/baz
and be dependency-resolved accordingly? That is, if I've got that as a directory containing just an empty __init__.py
file, would the generated code then get recognized as part of a proper Python package?hundreds-father-404
02/03/2021, 6:50 PM./pants test
where it fails.
2. For that Python file's owning python_library
target, add to the dependencies
field the path to that Protobuf file's directory, e.g. src/protos/models
. (This assumes that you have one BUILD file per directory, with empty python_library()
and protobuf_library()
definitions)
When running something like ./pants test
, you can use --no-process-execution-cleanup-local-dirs
so that you can preserver the tmpdir and look to see that the paths are showing up how you'd expect
No need to get all imports of Protobuf working - we only need to get at least one working, so that we can isolate the problem.hundreds-father-404
02/03/2021, 6:52 PMI should expect the generated code to ultimately reside in foo/bar/bazYes. If you have
src/protos/models/f.proto
, and you set the python_source_root
to foo/bar/baz
, then Pants should be creating the file foo/bar/baz/models/f_pb2.py
.
if I've got that as a directory containing just an empty init.py fileHmm, it's very plausible issues with
__init__.py
are at play. Let's start with confirming the _pb2.py
file shows up properly, then we can check if the __init__.py
files are showing upbrash-baker-91190
02/03/2021, 6:52 PMhappy-kitchen-89482
02/03/2021, 7:24 PMbrash-baker-91190
02/03/2021, 7:40 PM__init__.py
file, and the protobuf code generation drops the new files off in that directory, and I can use them. The automatic dependency resolution doesn't appear to work, though, and I have to manually add it. Additionally, I can't seem to build a distribution for code that uses the generated code (let alone a distribution for just the generated code) because Pants says that no python_distribution
owns the proto file in question.brash-baker-91190
02/03/2021, 7:45 PMbrash-baker-91190
02/03/2021, 7:46 PMbrash-baker-91190
02/03/2021, 7:48 PMbrash-baker-91190
02/03/2021, 7:48 PMhundreds-father-404
02/03/2021, 7:49 PMI suppose I can deal with that 🙂I'm glad there's a workaround, but that's not very satisfying and we'd love to figure this out. A major goal for Pants is that you do not need to reorganize your codebase (at least much) to get Pants working. And we have more work to do if that is not the case. I don't think
relocated_files
will work in the context of Protobuf; that won't trigger codegen, and it only works on files that you essentially want to have unmodified, outside of their path. Like JSON files or PNG fileshundreds-father-404
02/03/2021, 7:50 PMbrash-baker-91190
02/03/2021, 7:54 PMbrash-baker-91190
02/04/2021, 4:25 PMpython_source_root
to direct where to put the entire package. That seems kind of niche, and a byproduct of us doing things incorrectly with protobufs. As such, I don't really view it as a problem with Pants. We need to clean up our protobufs for other reasons anyway; this merely gives us another reason to do it sooner 😅hundreds-father-404
02/04/2021, 4:27 PMour protobuf file structure on disk didn't really match the protobuf package structureWhat does this look like? My experience with Protobuf is limited, and I've only seen it where the
package
clause == the directory path (minus source roots)brash-baker-91190
02/04/2021, 4:49 PMbrash-baker-91190
02/04/2021, 4:51 PMhundreds-father-404
02/04/2021, 4:52 PM