<#18667 Nested directories which match source root...
# github-notifications
c
#18667 Nested directories which match source roots can lead to slightly confusing behaviour Issue created by cbwl It would be helpful if pants could warn when package structures contain directories which match with source roots. I recently implemented a plugin under a package called something like
myorg.pants.plugins.myplugin
. The plugin worked fine but various pants goals (
lint
,
test
, etc.) failed with pants errors along the lines of:
Copy code
❯ 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:
Copy code
[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/p1680281281374909https://pantsbuild.slack.com/archives/C046T6T9U/p1680518068155799 pantsbuild/pants