Hi folks, I'm looking for some help - how do I par...
# general
a
Hi folks, I'm looking for some help - how do I parametrize an explicit dependency when I have multiple lock files? I have a project that has a dependency with
kubernetes
to run but it doesn't explicitly need to import it, so I added this to the
BUILD
file and it seemed to work well:
Copy code
python_sources(
    name="myproj",
    dependencies=["3rdparty/python:default#kubernetes"]
)
The problem is that I have multiple lock files (
default.lock
,
pipeline.lock
) and when I run with a different resolve I get this error:
Copy code
NoCompatibleResolveException: The target projects/myproj/main:deps uses the `resolve` `pipeline-resolve`, but some of its dependencies are not compatible with that resolve:

  * 3rdparty/python:default#kubernetes (default-resolve)

All dependencies must work with the same `resolve`. To fix this, either change the `resolve=` field on those dependencies to `pipeline-resolve`, or change the `resolve=` of the target projects/myproj/main:deps. If those dependencies should work with multiple resolves, use the `parametrize` mechanism with the `resolve=` field or manually create multiple targets for the same entity.

For more information, see <https://www.pantsbuild.org/2.21/docs/python/overview/lockfiles#multiple-lockfiles>.
I can't figure out how to dynamically make it use
3rdparty/python:default#kubernetes
or
3rdparty/python:default#kubernetes
depending on whichever resolve is selected, does anybody have any ideas? (Full
BUILD
file in the 🧵 )
Copy code
python_sources(
    name="myproj",
    dependencies=["3rdparty/python:default#kubernetes"]
)

pex_binary(
    name="deps",
    entry_point="main.py",
    layout="packed",
    include_sources=False,
    include_tools=True,
    dependencies=[":myproj"]
)

pex_binary(
    name="srcs",
    entry_point="main.py",
    layout="packed",
    include_requirements=False,
    include_tools=True,
    dependencies=[":myproj"]
)
c
It would be nice to see the build file where
3rdparty/python:default
is defined. I'm guessing it looks something like this:
Copy code
python_requirements(name="default", source="default-requirements.txt", resolve="python-default")
python_requirements(name="pipeline", source="pipeline-requirements.txt", resolve="python-pipeline")
each resolve is a different universe of dependencies. Since
myproj
is in the "python-default" universe, accessing the
3rdparty/python:default#kubernetes
target works. But if you have target in a different resolve (the "python-pipeline" resolve), it's in a different universe. If you need a target to exist in both universes, the typical way to do that is through
parametrize
. For example, this parametrises a Python target to both resolves:
Copy code
python_sources(name="mypython",resolve=parametrize("python-default", "python-pipeline"))
In you case, you need to parametrize both the resolve and the dependency fields together. You can do that with a more advanced form of parametrisation:
Copy code
python_sources(
    name="myproj",
    **parametrize("python-default", dependencies=[":default#kr8s"], resolve="pipeline-default"),
    **parametrize("python-pipeline", dependencies=[":pipeline#kr8s"], resolve="python-pipeline"),
)
I'm also not sure how you're "selecting" a resolve, but I'm guessing it's something like
Copy code
pex_binary(name="mypex",dependencies=[":myproj"],resolve="python-pipeline")
Full example I'm working off of:
Copy code
python_requirement(name="default", requirements=["kr8s"], resolve="python-default")

python_requirement(name="pipeline", requirements=["kr8s"], resolve="python-pipeline")

python_sources(
    name="myproj",
    **parametrize("python-default", dependencies=[":default#kr8s"], resolve="pipeline-default"),
    **parametrize("python-pipeline", dependencies=[":pipeline#kr8s"], resolve="python-pipeline"),
)

pex_binary(name="mypex", dependencies=[":myproj"], resolve="python-pipeline")
here's the ref for parametrizing targets for the details
a
I'll give this a go, thanks a lot @careful-address-89803!