Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC][bazel] Make python interpreter and pip dependencies usable in repository rules #25697

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

pamaury
Copy link
Contributor

@pamaury pamaury commented Dec 18, 2024

See the commits. The goal of this PR is to define a clean way of using the python interpreter defined by rules_python, as well as the pip dependencies, but the repository rules. This required a bit of trickery and pythonpath manipulation. The rationale for using an extension is detailed in the commit.

A side benefit of all this is that the interpret is also usable (for testing, debugging, whatever) using bazel run:

./bazelisk run @ot_py_for_repo//:python

If you try it with this PR, you'll notice that you can import jsonschema but not import hsjon because hjson is not listed in REPO_RULES_PIP_DEPS

Currently, it is quite tricky to use the python interpreter in
the repository rules (like in bitstream_workspace.py) because
the repositories created by rules_python are not really meant
to be used in the loading phase. With blzmod, this has become
quite cumbersome and so this commit introduces a clean
mechanism to do so.

The first is to create an extension. This extension has a single
method to register a python interpret and some packages which
are import by pip. The extension will then create a repository
containing a single executable which acts as python interpreter
taking care of all the detais.

Strictly speaking, this could have been achieved without an
extension but it would then require to manipulate the pythonpath
at every call site and create the required labels every time.
By putting everything into an extension, we get to do it once
and make it very easy to use everywhere.

Signed-off-by: Amaury Pouly <[email protected]>
This python script ideally requires the jsonschema package.

Signed-off-by: Amaury Pouly <[email protected]>
@pamaury pamaury requested review from a-will and jwnrt December 18, 2024 15:27
@pamaury pamaury requested a review from cfrantz as a code owner December 18, 2024 15:27
@pamaury pamaury changed the title Ot py for repo [baze] Make python interpreter and pip dependencies usable in repository rules Dec 18, 2024
@pamaury pamaury changed the title [baze] Make python interpreter and pip dependencies usable in repository rules [RFC][bazel] Make python interpreter and pip dependencies usable in repository rules Dec 18, 2024
@pamaury
Copy link
Contributor Author

pamaury commented Dec 18, 2024

I should note that although I think this is a nice "hack", I still have mixed feelings about it because it depends on semi-documented repository names used by rules_python. Looking at the code of rules_python, I am not very worried because they have to run python script and run into the same problem: this is why the python_host repository exists in the first place and why there is one repo per package. However, the fact that those repo are visible and the names used does not seem to be documented as stable.

Another remark: with the extension, it's possible to to do several registrations steps so that each "python interpret" has its own set of packages enabled. What this means is that if we want to, we don't have to created one interpreter which has access to the union of all dependencies of all users. Still, the fact that we need to manually list all transitive dependencies makes this process cumbersome anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant