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

feat(pip): support specifying requirements per (os, arch) #1885

Merged
merged 1 commit into from
May 19, 2024

Conversation

aignas
Copy link
Collaborator

@aignas aignas commented May 7, 2024

This PR implements a better way of specifying the requirements files for
different (os, cpu) tuples. It allows for more granular specification than what
is available today and allows for future extension to have all of the sources
in the select statements in the hub repository.

This is replacing the previous selection of the requirements and there are a
few differences in behaviour that should not be visible to the external user.
Instead of selecting the right file which we should then use to create
whl_library instances we parse all of the provided requirements files and
merge them based on the contents. The merging is done based on the blocks
within the requirement file and this allows the starlark code to understand if
we are working with different versions of the same package on different target
platforms.

Fixes #1868
Work towards #1643, #735

@aignas aignas force-pushed the feat/os-arch-requirement-files branch 2 times, most recently from 20d2045 to a1a90d2 Compare May 17, 2024 01:24
@aignas aignas marked this pull request as ready for review May 17, 2024 01:24
@aignas aignas requested a review from rickeylev as a code owner May 17, 2024 01:24
@aignas aignas changed the title feat(bzlmod): support specifying requirements files per (os, arch) feat(pip): support specifying requirements per (os, arch) May 17, 2024
@aignas aignas self-assigned this May 17, 2024
Copy link
Contributor

@rickeylev rickeylev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. I think I see how this is working. Just making sure I got the gist of it:

  • Input is basically a list of (filename, csv-string-platforms)
  • It expands that into (filename, platform-pattern)
  • It reads each file and populates a distribution->list-of-platforms map
  • That map eventually gets to whl_library

There's a bunch of surrounding code to handle some edge cases and error checking, but I think that's the gist of it.

Yeah, this makes sense. Looking at the requirements parser, it's pretty simple and doesn't have any support for env markers, which is...yeah, we'd only be able to do limited support given what env markers can do compared to bazel constraints.

(per-os-arch-requirements)=
## Requirements for a specific OS/Architecture

IN some cases you may need to use different requirements files for different OS, Arch combinations. This is enabled via the `requirements_by_platform` attribute in `pip.parse` extension and the `pip_parse` repository rule. The keys of the dictionary are labels to the file and the values are a list of comma separated target (os, arch) tuples.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
IN some cases you may need to use different requirements files for different OS, Arch combinations. This is enabled via the `requirements_by_platform` attribute in `pip.parse` extension and the `pip_parse` repository rule. The keys of the dictionary are labels to the file and the values are a list of comma separated target (os, arch) tuples.
In some cases you may need to use different requirements files for different OS, Arch combinations. This is enabled via the `requirements_by_platform` attribute in `pip.parse` extension and the `pip_parse` repository rule. The keys of the dictionary are labels to the file and the values are a list of comma separated target (os, arch) tuples.

doc = """\
The requirements files and the comma delimited list of target platforms as values.

The keys are the requirement files and the values are coma-separated platform
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The keys are the requirement files and the values are coma-separated platform
The keys are the requirement files and the values are comma-separated platform


"""Requirements parsing for whl_library creation.

Usecases that the code needs to cover:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Usecases that the code needs to cover:
Use cases that the code needs to cover:

@aignas
Copy link
Collaborator Author

aignas commented May 19, 2024

Yeah, you got the gist correctly. The map of the platforms gets to whl_library right now, but in the generic case the platform will be used like below:

  • Pass it to whl_library so that the cross-platform wheels can generate the right deps selects.
  • Use it to determine what version of the package we need to include for what target platform. This will be done by the pip extension.
  • Setup correct aliases in the render_pkg_aliases function based on the target platforms if multiple versions of the same package are used in a single hub repo.

@aignas aignas enabled auto-merge May 19, 2024 03:06
This change implements the necessary selection of the requirement files
based on the (os, arch) for both - bzlmod and legacy workspace
code paths. As part of this addition I have moved some tests from
integration tests to unit tests as now all of the logic on selecting the
right requirement file, ensuring that there are no duplicate requirement
lines and that the user knows what to provide is done in a single place.

This should be a non-breaking change for most users unless they have
been passing `requirements_linux` together with `extra_pip_args =
["--platform=manylinux_2_4_x86_64"]`, in which case they would have to
change their code to use `requirements_lock` attribute which itself is a
trivial change.
@aignas aignas force-pushed the feat/os-arch-requirement-files branch from dfabf80 to 6e31814 Compare May 19, 2024 03:29
@aignas aignas added this pull request to the merge queue May 19, 2024
Merged via the queue into bazelbuild:main with commit a6cb620 May 19, 2024
4 checks passed
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.

pip.parse crashes with StopIteration if platform restriction prevents package from install
2 participants