https://pantsbuild.org/ logo
#plugins
Title
# plugins
n

nice-park-16693

03/31/2023, 4:48 PM
Seeing something strange when trying to implement my first plugin. The plugin seems to "work", in that I can use the target it creates in a BUILD file somewhere and it ends up correctly wiring through to existing pants goals (package, publish, and run). However, both the tests I have written alongside my plugin code and the mypy linter fail to run; they both complain about unowned imports in the test code. Does anybody have any suggestions?
Copy code
❯ pants test ::
......
Engine traceback:
  in `test` goal
  in Run Pytest - pants/plugins/mypkg/something/else/foo_test.py:tests
  in Resolve transitive targets
  in Resolve direct dependencies of target - pants/plugins/mypkg/something/else/foo_test.py:tests
  in Inferring Python dependencies by analyzing source

UnownedDependencyError: Pants cannot infer owners for the following imports in the target pants/plugins/mypkg/something/else/foo_test.py:tests:

  * mypkg.something.else.foo.FooClass (line: 11)

If you do not expect an import to be inferrable, add `# pants: no-infer-dep` to the import line. Otherwise, see <https://www.pantsbuild.org/v2.15/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.
My code looks something like this:
Copy code
# pants/plugins/mypkg/BUILD
pants_requirements(name="pants", resolve="pants-plugins")
Copy code
# pants/plugins/mypkg/something/else/BUILD
python_sources(resolve="pants-plugins")
python_tests(name="tests", resolve="pants-plugins")
Copy code
# pants/plugins/mypkg/something/else/foo.py
does something interesting
Copy code
# pants/plugins/mypkg/something/else/foo_test.py
tests something interesting
Copy code
# pants/plugins/mypkg/something/else/register.py
registers my plugin
Copy code
# pants.toml
[GLOBAL]
pants_version = "2.15.0"
pythonpath = ["%(buildroot)s/pants/plugins"]

backend_packages.add = [
  ...
  "pants.backend.plugin_development",
  "mypkg.something.else", # my plugin gives me a new backend
]
this turned out (predictably) to be me being stupid. In
pants.toml
, I had:
Copy code
[source]
root_patterns = [
  "pants/plugins",
  ...
]
and my file locations for the plugins were things like:
Copy code
pants/plugins/mypkg/pants/plugins/myplugin/register.py
The nested
pants/plugins
directory (part of my package naming) understandably causes issues for pants. Changing my
root_patterns
to
"/pants/plugins"
(with a leading
/
) resolves the issues I was seeing.