-
-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Pex's
--path-mapping
with lockfiles for better local requir…
…ement support (#16584) Closes #16416. As explained in the new docs, some users will be able to use fixed paths, which is the simplest approach. But others may have different paths per machine, e.g. using `/User/<username>` in the path. `--path-mappings` allows lockfile generators & consumers to set this on a per-machine basis. ## Tip to set to a common value If you can use a common location and Pants's interpolation, e.g. `%(buildroot)s/wheels_dir`, then the config only needs to be set once. So, we encourage this approach. This is deemed adequate compared to some other enhancements we could make: - Trying to auto-detect when we can derive a common value. - Eagerly erroring if users haven't appropriately configured `[python-repos].path_mapping`. ## Global option vs. per-resolve The design doc at https://docs.google.com/document/d/1HAvpSNvNAHreFfvTAXavZGka-A3WWvPuH0sMjGUCo48/edit proposed having all resolve-related config be configurable on a per-resolve basis, rather than globally. We implemented this idea for a few options, but decided to not implement it (yet?) for `[python-repos].{find_links,indexes}` #16530. Given that it is only possible to have a global setting for `[python-repos].find_links`, I could not come up with a compelling reason to let you have path mappings be per-resolve. It only complicated the user interface and `help` message because users have to think about resolves. If we do end up implementing #16530, then it would make sense to also have this option be per-resolve. Until then, there is little value.
- Loading branch information
1 parent
e59978a
commit 2f2fcb5
Showing
9 changed files
with
263 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -422,7 +422,7 @@ setuptools | |
mongomock | ||
``` | ||
|
||
### Version control and local requirements | ||
### Version control requirements | ||
|
||
You can install requirements from version control using two styles: | ||
|
||
|
@@ -435,18 +435,6 @@ You can install requirements from version control using two styles: | |
- `Django@ git+https://github.com/django/django.git@stable/2.1.x` | ||
- `Django@ git+https://github.com/django/django.git@fd209f62f1d83233cc634443cfac5ee4328d98b8` | ||
|
||
You can also install from local files using [PEP 440 direct references](https://www.python.org/dev/peps/pep-0440/#direct-references). You must use an absolute path to the file, and you should ensure that the file exists on your machine. | ||
|
||
``` | ||
Django @ file:///Users/pantsbuild/prebuilt_wheels/django-3.1.1-py3-none-any.whl | ||
``` | ||
|
||
> 🚧 Local file requirements do not yet work with lockfiles | ||
> | ||
> Pex lockfiles will soon support local file requirements. | ||
> | ||
> In the meantime, the workaround is to host the files in a private repository / index and load it with `[python-repos]`. | ||
> 📘 Version control via SSH | ||
> | ||
> When using version controlled direct references hosted on private repositories with SSH access: | ||
|
@@ -500,7 +488,7 @@ option, you can either use: | |
|
||
* a URL to an HTML file with links to wheel and/or sdist files, or | ||
* a `file://` absolute path to an HTML file with links, or to a local directory with wheel and/or | ||
sdist files. | ||
sdist files. See the section on local requirements below. | ||
|
||
```toml | ||
[python-repos] | ||
|
@@ -533,6 +521,104 @@ the user: | |
indexes.add = ["http://$USERNAME:[email protected]/index"] | ||
``` | ||
|
||
### Local requirements | ||
|
||
There are two ways to specify local requirements from the filesystem: | ||
|
||
* [PEP 440 direct references](https://www.python.org/dev/peps/pep-0440/#direct-references) | ||
|
||
```python 3rdparty/python | ||
python_requirement( | ||
name="django", | ||
# Use an absolute path to a .whl or sdist file. | ||
requirements=["Django @ file:///Users/pantsbuild/prebuilt_wheels/django-3.1.1-py3-none-any.whl"], | ||
) | ||
|
||
# Reminder: we could also put this requirement string in requirements.txt and use the | ||
# `python_requirements` target generator. | ||
``` | ||
|
||
* The option `[python-repos].find_links` | ||
|
||
```toml pants.toml | ||
[python-repos] | ||
# Use an absolute path to a directory containing `.whl` and/or sdist files. | ||
find_links = ["file:///Users/pantsbuild/prebuilt_wheels"] | ||
``` | ||
```shell | ||
❯ ls /Users/pantsbuild/prebuilt_wheels | ||
ansicolors-1.1.8-py2.py3-none-any.whl | ||
django-3.1.1-py3-none-any.whl | ||
``` | ||
```python 3rdparty/BUILD | ||
# Use normal requirement strings, i.e. without file paths. | ||
python_requirement(name="ansicolors", requirements=["ansicolors==1.1.8"]) | ||
python_requirement(name="django", requirements=["django>=3.1,<3.2"]) | ||
|
||
# Reminder: we could also put these requirement strings in requirements.txt and use the | ||
# `python_requirements` target generator | ||
``` | ||
|
||
Unlike PEP 440 direct references, `[python-repos].find_links` allows you to use multiple artifacts | ||
for the same project name. For example, you can include multiple `.whl` and sdist files for the same | ||
project in the directory; if `[python-repos].indexes` is still set, then Pex/pip may use | ||
artifacts both from indexes like PyPI and from your local `--find-links`. | ||
|
||
Both approaches require using absolute paths, and the files must exist on your machine. This is | ||
usually fine when locally iterating and debugging. This approach also works well if your entire | ||
team can use the same fixed location. Otherwise, see the below section. | ||
|
||
#### Working around absolute paths | ||
|
||
If you need to share the lockfile on different machines, and you cannot use the same | ||
absolute path, then you can use the option | ||
`[python-repos].path_mappings` along with `[python-repos].find_links`. (`path_mappings` is not | ||
intended for PEP 440 direct requirements.) | ||
|
||
The `path_mappings` option allows you to substitute a portion of the absolute path with a logical | ||
name, which can be set to a different value than your | ||
teammates. For example, the path | ||
`file:///Users/pantsbuild/prebuilt_wheels/django-3.1.1-py3-none-any.whl` could become | ||
`file://${WHEELS_DIR}/django-3.1.1-py3-none-any.whl`, where each Pants user defines what | ||
`WHEELS_DIR` should be on their machine. | ||
|
||
This feature only works when using Pex lockfiles via `[python].resolves` and for tool lockfiles | ||
like Pytest and Black. | ||
|
||
`[python-repos].path_mappings` expects values in the form `NAME|PATH`, e.g. | ||
`WHEELS_DIR|/Users/pantsbuild/prebuilt_wheels`. Also, still use an absolute path for | ||
`[python-repos].find_links`. | ||
|
||
If possible, we recommend using a common file location for your whole team, and leveraging [Pants's | ||
interpolation](doc:options#config-file-interpolation), so that you avoid each user needing to | ||
manually configure `[python-repos].path_mappings` and `[python-repos].find_links`. | ||
For example, in `pants.toml`, you could set `[python-repos].path_mappings` to | ||
`WHEELS_DIR|%(buildroot)s/python_wheels` and `[python-repos].find_links` to | ||
`%(buildroot)s/python_wheels`. Then, as long as every user has the folder `python_wheels` in the | ||
root of the repository, things will work without additional configuration. Or, you could use a | ||
value like `%(env.HOME)s/pants_wheels` for the path `~/pants_wheels`. | ||
|
||
```toml pants.toml | ||
[python-repos] | ||
# No one needs to change these values, as long as they can use the same shared location. | ||
find_links = ["file://%(buildroot)s/prebuilt_wheels"] | ||
path_mappings = ["WHEELS_DIR|%(buildroot)s/prebuilt_wheels"] | ||
``` | ||
|
||
If you cannot use a common file location via interpolation, then we recommend setting these options | ||
in a [`.pants.rc` file](doc:options#pantsrc-file). Every teammate will need to set this up for their | ||
machine. | ||
|
||
```toml .pants.rc | ||
[python-repos] | ||
# Each user must set both of these to the absolute paths on their machines. | ||
find_links = ["file:///Users/pantsbuild/prebuilt_wheels"] | ||
path_mappings = ["WHEELS_DIR|/Users/pantsbuild/prebuilt_wheels"] | ||
``` | ||
|
||
After initially setting up `[python-repos].path_mappings` and `[python-repos].find_links`, run | ||
`./pants generate-lockfiles` or `./pants generate-lockfiles --resolve=<resolve-name>`. You | ||
should see the `path_mappings` key set in the lockfile's JSON. | ||
|
||
### Constraints files | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.