is there a better way to extend dependencies decla...
# general
f
is there a better way to extend dependencies declared for target generators when you don't want to completely replace them with the
overrides
? ๐Ÿงต
Take https://github.com/pantsbuild/example-python. There are three sources of dependencies that are shown: 1. The dependency inference (
helloworld/translator/translator.py:lib
due to
from helloworld.translator.translator import LanguageTranslator
) 2. The manually set dependencies (the JSON file is being read, so
dependencies=[":translations"],
had to be set) 3. Anything from the
overrides
field, if set. Currently there is none.
Copy code
$ pants dependencies helloworld/greet/greeting.py

//:reqs#setuptools
//:reqs#types-setuptools
helloworld/greet:translations
helloworld/translator/translator.py:lib
Now, if I want just one file out of many that may be owned by a build target to depend on something extra (e.g. all files depend on a JSON file and just one file depends on a YAML file), one would intuitively want to do:
Copy code
python_sources(
    name="lib",
    dependencies=[":translations"],
    overrides={"greeting.py": {"dependencies": ["helloworld/main.py:lib"]}}
)
In this example picking
main.py
to keep it simple. The
main.py
does appear, however, the
helloworld/greet:translations
is gone because the
overrides
completely replaced the value of the
dependencies
field of that target.
Copy code
$ pants dependencies helloworld/greet/greeting.py

//:reqs#setuptools
//:reqs#types-setuptools
helloworld/main.py:lib
helloworld/translator/translator.py:lib
While we don't have an
extends
field that would let to extend the dependencies (of course this would be applicable only to some fields -- it doesn't make sense to extend the timeout for instance), I don't think we have a way to make sure that only some of the files under a target generator get some extra dependencies without duplicating "common" dependencies that all files share?
to get the dependency set manually on the JSON file back, it needs to be set in two places -- once in
dependencies
and once in `overrides`:
Copy code
python_sources(
    name="lib",
    dependencies=[":translations"],
    overrides={"greeting.py": {"dependencies": [":translations", "helloworld/main.py:lib"]}}
)
if the dependencies list is long, it may help to extract them into a variable like this:
Copy code
common_deps = [":translations"]
python_sources(
    name="lib",
    dependencies=common_deps,
    overrides={"greeting.py": {"dependencies": common_deps + ["helloworld/main.py:lib"]}}
)
which is not terrible, but not great either
b
variable is the best way I personally know of
๐Ÿ™ 1