I seem to be running into this issue here as well ...
# general
d
I seem to be running into this issue here as well -> https://pantsbuild.slack.com/archives/C046T6T9U/p1743090146943159 where I have an old django codebase and I would like to just say "All python code under
Copy code
src/**/*.py
f
I did find a way to do this in the end, if you use
python_sources(sources=['***/**.py])
it will force all the python files to be bundled together. However, you must remove
python_sources()
from any BUILD files in subdirectories because pants won't allow any overlap.
d
so basically at the top of the directory tree just have a single BUILD file with a
python_sources(sources=['***/**.py])
and then no other BUILD files underneath. And this solves the "two parents for a target" issue?
I'll give that a try
f
You stick that asterisk source in the BUILD file of your app, and then you can reference that app's directory as a dependency and it should grab all of it at once I think
d
Hmm.. Ok, so then in a pex_binary I just need to add all my apps once each?
f
Yeah if you add them as dependencies it should pick them up
d
Hmm.. So im getting this interesting error that says that I have two parents for a single target. I think its because I have overlap between the "add everything in this directory" and the "pants is inferring what it needs" did you have those warnings?
Copy code
but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use:
f
Possibly yes. In the end I went down a different route to solve this so I'm currently having a whole set of different problems 😄
e
You have multiple
python_source(s)
that reference that particular file
f
It was sort of working for me though. I deleted all the BUILD files in subdirectories when I was testing it
d
@elegant-florist-94385 yeah, I have a bunch of just
python_source()
BUILD files in a number of directories, and then one in a parent directory with that glob. Im getting the feeling I should just remove all the lower level ones and rely on the top level? Is that a bad idea? or totally fine? Particularly in a codebase that is basically all or nothing.
e
Yeah, you'll have to remove those individual ones since double targets is not good. (Anything that depends on it will have to explicitly mention which one it refers to). (There are use cases for this, typically when using
parametrize
, but that's another discussion)
Using a global top level
python_sources
is okay. but a couple downsides to be aware of. 1. If you need a smaller glob of files from a subdirectory, you can't have it. (This would involve duplicated targets, so it may be better to have
src/app/A
, and
src/app/B
each with a glob and then whatever needs global can just depend on both of them). 2. You CAN use
overrides
in your
python_sources
to customize particular fields on a particular file (and this can be used on globs too). a. The downside is that these customizations live in the top level build file, and not near the particular file being customized