https://pantsbuild.org/ logo
#general
Title
# general
r

ripe-cpu-85141

08/01/2022, 10:34 PM
I'm having trouble writing an "in-repo" plugin for a custom codegen. I have put it under
pants-plugins/tutugen
which contains a
register.py
file and a
BUILD
file. First, I'm not sure what I should put on the BUILD file. I read about python_source, python_requirement and pants_requirements. I'm not sure which ones should go in the file. Then, the plugin requires a 3rd party lib named
tutu
. If I try to exec any pants commands, I get a
No module named: 'tutu'
error. I'm not sure what is missing :-/
h

happy-kitchen-89482

08/01/2022, 10:43 PM
python_sources
should wrap your plugin's source files. Having that depend on a
pants_requirements()
target is handy shorthand for "depend on the version of Pants I am running in", so you don't have to repeat that version (and ensure it matches the
version=
value in your pants.toml), as you would if you used a regular
python_requirements
.
But you will need a
python_requirement
for
tutu
(or you can put it in a
requirements.txt
and use a
python_requirements()
to point to that, as usual)
Does your plugin code import from
tutu
? If not then you'll need an explicit dep from the plugin's
python_sources
to the
python_requirement
target for
tutu
And finally you will need to add the package containing your
register.py
to your
backend_packages
in
pants.toml
r

ripe-cpu-85141

08/01/2022, 10:45 PM
right now,
register.py
imports
tutu
. The error happens when pants tries to load the backend
h

happy-kitchen-89482

08/01/2022, 10:46 PM
And you may need to set
pythonpath = ["%(buildroot)s/path/to/plugin/source/root"]
in pants.toml
OK, so sounds like your
pants.toml
is set up correctly
And do you have
tutu
in a requirements.txt, or a
python_requirement
?
r

ripe-cpu-85141

08/01/2022, 10:46 PM
backend_packages and pythonpath are set. I think it is just a question of detecting my tutu dependency
h

happy-kitchen-89482

08/01/2022, 10:47 PM
Oh, wait, this tickles my memory
Try adding the
tutu
requirement to
plugins = []
in pants.toml
r

ripe-cpu-85141

08/01/2022, 10:47 PM
explicitly in a
python_requirement
on a BUILD file in the module (alongside register.py)
h

happy-kitchen-89482

08/01/2022, 10:47 PM
IIRC that is a known gotcha for in-repo plugins. The requirements have to be added to the
plugins
list.
👍 1
h

hundreds-father-404

08/01/2022, 10:50 PM
yeah, it's a little confusing. You don't actually have to have the
pants_requirements
and
python_sources
targets for a plugin to work; all that matters is the
[GLOBAL].plugins
,
backend_packages
, and
pythonpath
options. But, it's a good idea to have
pants_requirements
and
python_sources
so that you can run Pants on your plugin code, e.g. format it and lint it
r

ripe-cpu-85141

08/01/2022, 10:51 PM
ok, the plugins was missing. It works now
❤️ 1
Stange thing, the goal export-codegen doesn't exist
Am I missing something in the rules I wrote or is there something to import?
h

hundreds-father-404

08/01/2022, 10:53 PM
the goal export-codegen doesn't exist
A relevant backend needs to be implemented. We hide irrelevant goals by default. Did you register a
UnionRule(GenerateSourcesRequest, ...)
?
E.g.
UnionRule(GenerateSourcesRequest, GeneratePythonFromProtobufRequest)
r

ripe-cpu-85141

08/01/2022, 10:54 PM
I did but let me check for typo
h

hundreds-father-404

08/01/2022, 10:55 PM
a common error we sometimes do is not properly registering all the rules inside
register.py
. For example, we often have a dedicated file
rules.py
. You need to import that and then register its rules inside `register.py`'s
def rules()
function
r

ripe-cpu-85141

08/01/2022, 10:56 PM
right now, everything is in the
register.py
. I have the
UnionRule(...)
So most likely, there is something wrong in my rules, right?
h

hundreds-father-404

08/01/2022, 10:57 PM
Okay. Simple test then to make sure the backend is being picked up in general...add a target type
Copy code
from pants.engine.target import Target

class MyTarget(Target):
   alias = "my_target"
   core_fields = ()
   help = "blah"

def target_types():
   return [MyTarget]
Then
./pants help targets
r

ripe-cpu-85141

08/01/2022, 10:58 PM
yep the target is there.
👍 1
h

hundreds-father-404

08/01/2022, 10:59 PM
Strange that
./pants export-codegen
isn't showing up then...
GenerateSourcesRequest
does have a class property
exportable: bool
, but it defaults to True
are you able to share a gist of the file?
r

ripe-cpu-85141

08/01/2022, 11:01 PM
unfortunately I won't be able in this case. I followed https://www.pantsbuild.org/docs/plugins-codegen
The only difference (besides possible typos) is that the Request class parameters input and output have the same type
"input" and "output"
h

hundreds-father-404

08/01/2022, 11:05 PM
The only difference (besides possible typos) is that the Request class parameters input and output have the same type
I think that's legal
what's happening when you run
./pants export-codegen ::
?
r

ripe-cpu-85141

08/01/2022, 11:05 PM
Unknown goal: export-codegen
😢 1
did you mean export?
I don't see it in the list when I do
./pants help goals
h

hundreds-father-404

08/01/2022, 11:07 PM
If you activate
pants.backend.codegen.protobuf.python
, does it show up at least?
r

ripe-cpu-85141

08/01/2022, 11:09 PM
oh, yes it does and if I do
./pants export-codegen ::
, it actually call my backend
*calls