I'm having trouble writing an "in-repo" plugin for...
# general
r
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
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
right now,
register.py
imports
tutu
. The error happens when pants tries to load the backend
h
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
backend_packages and pythonpath are set. I think it is just a question of detecting my tutu dependency
h
Oh, wait, this tickles my memory
Try adding the
tutu
requirement to
plugins = []
in pants.toml
r
explicitly in a
python_requirement
on a BUILD file in the module (alongside register.py)
h
IIRC that is a known gotcha for in-repo plugins. The requirements have to be added to the
plugins
list.
👍 1
h
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
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
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
I did but let me check for typo
h
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
right now, everything is in the
register.py
. I have the
UnionRule(...)
So most likely, there is something wrong in my rules, right?
h
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
yep the target is there.
👍 1
h
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
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
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
Unknown goal: export-codegen
😢 1
did you mean export?
I don't see it in the list when I do
./pants help goals
h
If you activate
pants.backend.codegen.protobuf.python
, does it show up at least?
r
oh, yes it does and if I do
./pants export-codegen ::
, it actually call my backend
*calls