cool-easter-32542
01/22/2024, 6:52 PM* for globs over just the current working directory, ** for recursive globs over everything below (at any level the current working directory), with prefix ! for ignores.
The best practice, however, is to follow the 1:1:1 principle: metadata about your code should live near the code itself. For this reason, I think it may be helpful to enforce this via a global option so that users will be able to forbid using recursive globs in sources fields (or elsewhere where the files ownership is declared) similarly to how we handle globs in BUILD files that do not match files on disk (ignore/warn/fail).
Describe the solution you'd like
Having this directory with source code, e.g.
$ tree foo
foo
├── bar
│ ├── __init__.py
│ ├── mod3.py
│ └── mod4.py
├── baz
│ ├── __init__.py
│ ├── mod1.py
│ └── mod2.py
├── BUILD
└── __init__.py
3 directories, 8 files
with
$ cat foo/BUILD
python_sources(sources=["**"])
running
$ pants list foo::
//src/python/pants/foo/__init__.py:../../../../all-__init__.py-files
//src/python/pants/foo/bar/__init__.py:../../../../../all-__init__.py-files
//src/python/pants/foo/baz/__init__.py:../../../../../all-__init__.py-files
src/python/pants/foo:foo
src/python/pants/foo/BUILD
src/python/pants/foo/__init__.py
src/python/pants/foo/bar/__init__.py:../foo
src/python/pants/foo/bar/mod3.py:../foo
src/python/pants/foo/bar/mod4.py:../foo
src/python/pants/foo/baz/__init__.py:../foo
src/python/pants/foo/baz/mod1.py:../foo
src/python/pants/foo/baz/mod2.py:../foo
yields all the files in the directory tree, recursively. With the global option to control whether using recursive globs is permitted, one should be able to raise an error or warn about it.
Describe alternatives you've considered
Currently, users who would like to control whether recursive globs are used would need to use some kind of semantic grep / AST tools to identify recursive globs usage which may be a lot of work.
Additional context
I can see a use case where there's a directory with lots of inner directories containing some non-code resources and one wouldn't want to create individual BUILD files inside them, particularly, if the directories are added/removed all the time. See #18217 for illustration. So one may want to allow using a recursive pattern on some targets, but not all of them (e.g. allow on resources, but forbid on python_sources). A global option won't work in this case, but adding a new field to a base target that deals with file ownership may be unjustified?
pantsbuild/pants