|
| 1 | +# Single dep for spoke repos per `whl_name`. |
| 2 | + |
| 3 | +The goal is to have `whl_library` instances from wheels (where we pass `url` for a URL or we pass |
| 4 | +a `whl_file` label which points to an actual wheel) to be reused across different python versions |
| 5 | +and across different configurations where different extras are requested. This means that |
| 6 | +if the user is using the same wheel in 2 different configurations, the wheel is reused, because |
| 7 | +the extracted contents should not differ. This could be important for: |
| 8 | +* platform-specific wheels for custom platforms defined by the user. Otherwise the |
| 9 | + platform-specific wheel behaviour should remain unchanged. |
| 10 | +* cross-platform wheels for are the main affected item here. |
| 11 | + |
| 12 | +The code should be gated by a feature flag, which can be flipped to be enabled using an |
| 13 | +environmental variable via |
| 14 | + |
| 15 | +The file to modify `python/private/internal_config_repo.bzl`. Use the |
| 16 | +`RULES_PYTHON_WHL_LIBRARY_OPTIMIZED=1` to enable the feature. Add it to the |
| 17 | +`docs/readthedocs_build.sh` file so that we are exercising the code. And document this in |
| 18 | +the `docs/environment-variables.md` file. |
| 19 | +If the flag is off, then it is equivalent to the current |
| 20 | +`main` branch behaviour, if it is on, then it is equivalent to turning all of the behaviour |
| 21 | +implemented here on. |
| 22 | + |
| 23 | +Since this is a refactor for users not using private APIs, the env variable will be switched/removed |
| 24 | +later together with old code. |
| 25 | + |
| 26 | +All of the code should be written in a TDD style where we add a failing test for a feature and then |
| 27 | +we implement the new code. |
| 28 | + |
| 29 | +## Change the naming of the spoke repositories: |
| 30 | + |
| 31 | +> The hub repositories should be named `<hub_name>_<wheel_name_suffix>. |
| 32 | +
|
| 33 | +The names should be changed: |
| 34 | +* from `+pip+dev_pip_314_requests_py3_none_any_2a0d60c1_linux_x86_64_linux_x86_64_freethreaded` |
| 35 | + to `+pip+dev_pip_requests_py3_none_any_2a0d60c1` |
| 36 | +* from `+pip+dev_pip_314_roman_numerals_py3_none_any_647ba99c` |
| 37 | + to `+pip+dev_pip_roman_numerals_py3_none_any_647ba99c` |
| 38 | +* from `+pip+dev_pip_314_markupsafe_cp314_cp314t_manylinux_2_17_x86_64_fed51ac4` |
| 39 | + to `+pip+dev_pip_markupsafe_cp314_cp314t_manylinux_2_17_x86_64_fed51ac4` |
| 40 | + |
| 41 | +So the naming convention after the change is: |
| 42 | +`<prefix>_<name>_<py_tag>_<abi_tag>_<platform_tag>_<sha256[:8]>` |
| 43 | + |
| 44 | +Where `<prefix>` is the hub repository name and the rest of the segments come from the wheel name |
| 45 | +itself. |
| 46 | + |
| 47 | +The sdist building `whl_library` instances should change the naming to: |
| 48 | +`<prefix>_<name>_<sha256[:8]>_<rules_python_target_platform>` |
| 49 | + |
| 50 | +The file to modify `python/private/pypi/whl_repo_name.bzl` |
| 51 | + |
| 52 | +## Change the `whl_library_targets` |
| 53 | + |
| 54 | +Using the `METADATA` file that is parsed from `whl_metadata` function where the `Provides-Extra` is |
| 55 | +retrieved from the file. Right now we generate a single target which includes all of the extras that |
| 56 | +are required. Instead do the following target generation: |
| 57 | +* `pkg` (no extras) - target with no extras, this is the main target that includes the python sources, |
| 58 | + the other targets just include extra dependencies. |
| 59 | +* `pkg__extra` - target for each extra in the provides-extra list. If the target depends on |
| 60 | + `self[another_extra]`, then explode the nodes so that the target only depends on `pkg` target + |
| 61 | + extra dependencies. Use `py_library` for this. Also be smart about generating targets: |
| 62 | + - If the only extra is in the env marker, but the dependency is not in the hub repo, then skip |
| 63 | + generating the whole `pkg__extra` target. |
| 64 | + - Propose any extra ideas here. |
| 65 | + |
| 66 | +Related files: |
| 67 | +* `python/private/pypi/whl_library.bzl` |
| 68 | +* `python/private/pypi/whl_metadata.bzl` |
| 69 | +* `python/private/pypi/whl_library_targets.bzl` |
| 70 | + |
| 71 | +## Change `hub_builder` and `hub_repository` |
| 72 | + |
| 73 | +Instead of passing the `requirement[extra1,extra2]` to the `whl_library`, pass it to the |
| 74 | +`hub_repository` via `whl_config_setting` so that the `render_pkg_aliases` is using the information to alias to the right |
| 75 | +extra target based on what is requested. This should retain the behaviour where different target |
| 76 | +platforms are allowed to target `foo[baz]` and `foo[bar]` and we select the |
| 77 | +`@dev_pip_spoke_repo//:pkg__baz` and `@dev_pip_spoke_repo//:pkg__bar` appropriately. |
| 78 | + |
| 79 | +We should also generate extra targets here: |
| 80 | +* `pkg` should point to the target with all specified extras as passed to the `hub_repository`. This is to keep backwards compatibility with how it used to be done |
| 81 | + previously. This also keeps compatibility with `rules_pycross`. Add this as a comment |
| 82 | + when doing this. The spoke repos should not be used directly, so this difference in |
| 83 | + behaviour is OK. |
| 84 | + This reuses the targets declared in the hub repository described below. |
| 85 | + It is backed by a `py_library` target if there are multiple extras, or an `alias` if it |
| 86 | + is only a single extra. It is the same target as described below. |
| 87 | +* `pkg[]` should point to the target in the spoke without extras |
| 88 | +* `pkg[extra]` should point to the target in the spoke with particular extras. Do this for each |
| 89 | + provided extra. So for `requirements[extra1,extra2]` passed to the hub repo, 2 extra targets will |
| 90 | + be created. If the extras have not been specified for certain platforms via the |
| 91 | + `whl_config_setting`, then the `select` statement should raise an `error` for no match. |
| 92 | + Document the mapping explicitly: hub `pkg[]` → spoke `pkg`, hub `pkg[extra]` → spoke `pkg__extra`. |
| 93 | + The fact that some of the targets in the spoke repos are unreachable is intentional - this is |
| 94 | + because the hub repository contents are created before the `whl_library` downloads the whl and |
| 95 | + inspects the METADATA. |
| 96 | + The reverse direction is not a concern because the requirements file locking results in |
| 97 | + consistent file. If something like this happens, then we should provide a message to the |
| 98 | + user that something went wrong and that they should create a ticket in rules_python bug |
| 99 | + tracker. |
| 100 | +* If there is `requirement[extra1,extra2]` passed, that means that we should create a special alias |
| 101 | + that includes both of the targets at once. For this use `py_library` instead of `alias` and pass |
| 102 | + `:pkg[extra1]` and `:pkg[extra2]` to the `deps` of the `py_library`. This won't cause |
| 103 | + a failure because for a particular platform that is requested this should work by |
| 104 | + construction, because the dependency targets will be also defined for the particular platform. |
| 105 | + |
| 106 | +* If the `requirement` is passed without any extras, then hub `pkg` should alias to `pkg[]`. |
| 107 | +* If the target is `py_library`, then we should name it without `[]` to avoid any aspects traversing |
| 108 | + `py_library` targets changing behaviour. Only alias targets can have `[]`. |
| 109 | + |
| 110 | +Related files: |
| 111 | +* `python/private/pypi/render_pkg_aliases.bzl` |
| 112 | +* `python/private/pypi/hub_builder.bzl` |
| 113 | +* `python/private/pypi/hub_repository.bzl` |
| 114 | +* `python/private/pypi/whl_config_setting.bzl` - consider extending this struct to specify which |
| 115 | + extras are requested for which platform. |
| 116 | + |
| 117 | +## Modify `pip_repository` |
| 118 | + |
| 119 | +Do similar changes to the `WORKSPACE` code to ensure easier maintenance of `whl_library` so that all |
| 120 | +code paths to `whl_library` remain consistent. |
| 121 | + |
| 122 | +Implement this by using the information that we retrieve by parsing the `requirements.txt` files |
| 123 | +using the `parse_requirements` function. Parse the requirement itself using the existing parser in |
| 124 | +the referenced file `pep508_requirement.bzl`. |
| 125 | + |
| 126 | +Related files: |
| 127 | +* `python/private/pypi/pip_repository.bzl` |
| 128 | +* `python/private/pypi/requirements.bzl.tmpl.workspace` |
| 129 | +* `python/private/pypi/pep508_requirement.bzl` |
| 130 | + |
| 131 | +## Modify `unified_hub_repo` |
| 132 | + |
| 133 | +Pass the extras to the `unified_hub_repo` as well so that the extra targets are created. The target |
| 134 | +topology in the unified hub repo should correspond to the hub_repository. |
| 135 | + |
| 136 | +File: |
| 137 | +* `python/private/pypi/unified_hub_repo.bzl` |
0 commit comments