cool-easter-32542
04/03/2023, 9:58 PMmyorg.pants.plugins.myplugin. The plugin worked fine but various pants goals (lint, test, etc.) failed with pants errors along the lines of:
❯ pants test ::
......
Engine traceback:
in `test` goal
in Run Pytest - pants/plugins/myorg/pants/plugins/myplugin/foo_test.py:tests
in Resolve transitive targets
in Resolve direct dependencies of target - pants/plugins/myorg/pants/plugins/myplugin/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/myorg/pants/plugins/myplugin/foo_test.py:tests:
* myorg.pants.plugins.myplugin.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.
It took a bit of poking around to realise that the problem here was that a part of my package name (the pants.plugins part) was identical in directory structure to the source root in which my plugin was defined (the top-level directory /pants/plugins). Pants is fine with this if source roots are well-defined, but in my case my pants.toml had this incorrect configuration:
[source]
root_patterns = [
"pants/plugins", # missing leading slash
...
]
The problem here was resolved by adding the leading slash to my root_pattern so that Pants no longer (quite reasonably) thought that the directory /pants/plugins/myorg/pants/plugins was, in fact a source root.
It would be convenient if pants could report a warning about cases where multiple nested paths both correspond to a root_pattern. That would have immediately pointed to the issue here and helped me more quickly arrive at the solution of replacing my root_pattern with "/pants/plugins".
I don't think that any change in behaviour around treatment of root_patterns or dependency inference is strictly needed beyond that warning.
Related slack posts:
• https://pantsbuild.slack.com/archives/C01CQHVDMMW/p1680281281374909
• https://pantsbuild.slack.com/archives/C046T6T9U/p1680518068155799
pantsbuild/pants